-
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.
Merge pull request #60 from Takasaki-Studio/new-validator
New validator
- Loading branch information
Showing
20 changed files
with
218 additions
and
184 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
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
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,77 @@ | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
using TakasakiStudio.Lina.Common.Interfaces; | ||
|
||
namespace TakasakiStudio.Lina.Common; | ||
|
||
/// <summary> | ||
/// Base class to view model with basic validation using <a href="https://docs.fluentvalidation.net/en/latest/">Fluent Validation</a> | ||
/// </summary> | ||
/// <typeparam name="TModel">Self ref class</typeparam> | ||
public abstract class BaseValidated<TModel> : IValidate | ||
{ | ||
private LinaAbstractValidator<TModel>? _validator; | ||
|
||
protected BaseValidated() | ||
{ | ||
InstanceValidator(); | ||
} | ||
|
||
/// <summary> | ||
/// Validate view model with class validation and throw exception if failure | ||
/// </summary> | ||
/// <exception cref="ValidationException">Failure validation information</exception> | ||
public virtual async ValueTask Validate() | ||
{ | ||
await _validator!.ValidateAndThrowAsync(GetClassInstance()); | ||
} | ||
|
||
/// <summary> | ||
/// Get validation errors | ||
/// </summary> | ||
/// <returns>Failure validation information</returns> | ||
public virtual async Task<ValidationResult> GetErrors() | ||
{ | ||
return await _validator!.ValidateAsync(GetClassInstance()); | ||
} | ||
|
||
/// <summary> | ||
/// Verify if validation pass | ||
/// </summary> | ||
/// <returns>If valid</returns> | ||
public virtual async ValueTask<bool> IsValid() | ||
{ | ||
return (await GetErrors()).IsValid; | ||
} | ||
|
||
/// <summary> | ||
/// Setups the validator rules | ||
/// </summary> | ||
/// <param name="rules">The validator instance, used to configure the validation rules</param> | ||
/// <see href="https://docs.fluentvalidation.net/en/latest/index.html">Fluent Validation documentation</see> | ||
/// <example> | ||
/// <code language="cs"> | ||
/// public class ExampleModel : BaseValidated<ExampleModel> | ||
/// { | ||
/// public required string Test { get; set; } | ||
/// | ||
/// protected override void SetupValidator(LinaAbstractValidator<ExampleModel> rules) | ||
/// { | ||
/// rules.RuleFor(x => x.Test).NotEmpty().NotNull(); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </example> | ||
protected abstract void SetupValidator(LinaAbstractValidator<TModel> rules); | ||
|
||
private void InstanceValidator() | ||
{ | ||
var setupValidator = (LinaAbstractValidator<TModel>.ValidationBuilder)SetupValidator; | ||
_validator = new LinaAbstractValidator<TModel>(setupValidator); | ||
} | ||
|
||
private TModel GetClassInstance() | ||
{ | ||
return (TModel)(object)this; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
using FluentValidation; | ||
|
||
namespace TakasakiStudio.Lina.Common; | ||
|
||
public class LinaAbstractValidator<TModel> : AbstractValidator<TModel> | ||
{ | ||
public delegate void ValidationBuilder(LinaAbstractValidator<TModel> builder); | ||
internal LinaAbstractValidator(ValidationBuilder constructorBuilder) | ||
{ | ||
constructorBuilder(this); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace TakasakiStudio.Lina.Database.Interfaces; | ||
|
||
public interface IBaseEntity<TPkType> | ||
{ | ||
/// <summary> | ||
/// Entity id | ||
/// </summary> | ||
public TPkType Id { get; set; } | ||
|
||
/// <summary> | ||
/// Create a clone of value | ||
/// </summary> | ||
/// <typeparam name="T">Value type</typeparam> | ||
/// <returns></returns> | ||
public T Clone<T>(); | ||
} |
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
Oops, something went wrong.