-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation with result error pattern
- Loading branch information
1 parent
30ce544
commit dd572da
Showing
6 changed files
with
125 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
using FluentValidation.Results; | ||
|
||
namespace WomensWiki; | ||
|
||
public class Error { | ||
public string Code { get; private set; } = null!; | ||
public string? Message { get; private set; } | ||
|
||
public Error(string code, string? message = null) { | ||
Code = code; | ||
Message = message; | ||
} | ||
} | ||
|
||
public static class ErrorMapper { | ||
public static List<Error> Map(ValidationResult validationResult) { | ||
return validationResult.Errors.Select(e => new Error(e.ErrorCode, e.ErrorMessage)).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace WomensWiki; | ||
|
||
public static class Result { | ||
public static Result<T> Success<T>(T data) => Result<T>.Success(data); | ||
public static Result<T> Failure<T>(List<Error> errors) => Result<T>.Failure(errors); | ||
} | ||
|
||
public class Result<T> { | ||
public bool IsSuccess { get; private set; } | ||
public bool IsFailure => !IsSuccess; | ||
|
||
public T? Data { get; private set; } | ||
public List<Error>? Errors { get; private set; } | ||
|
||
private Result(bool isSuccess, T? data, List<Error>? errors = null) { | ||
IsSuccess = isSuccess; | ||
Data = data; | ||
Errors = errors; | ||
} | ||
|
||
public static Result<T> Success(T data) => new(true, data, null); | ||
public static Result<T> Failure(List<Error> errors) => new(false, default, errors); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
|
||
namespace WomensWiki.Domain.Articles; | ||
|
||
public static class ArticleValidators { | ||
public static IRuleBuilderOptions<T, Guid> ArticleExists<T>(this IRuleBuilder<T, Guid> ruleBuilder) { | ||
return (IRuleBuilderOptions<T, Guid>)ruleBuilder.Custom((value, context) => { | ||
if (context.RootContextData["Article"] == null) { | ||
var failure = new ValidationFailure("ArticleId", "Article with that ID does not exist"); | ||
failure.ErrorCode = "ArticleNotFound"; | ||
context.AddFailure(failure); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters