Skip to content

Commit

Permalink
Merge pull request #36 from Takasaki-Studio/dev
Browse files Browse the repository at this point in the history
rename BaseViewModel.cs to BaseValidationRecord.cs
  • Loading branch information
Takasakiii authored Nov 10, 2023
2 parents 98f23c7 + 102a706 commit 1883695
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Lina/Lina.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.2.5</Version>
<Version>1.2.6</Version>
<PackageId>TakasakiStudio.Lina</PackageId>
<Authors>TakasakiStudio</Authors>
<RepositoryUrl>https://github.com/Takasaki-Studio/Lina</RepositoryUrl>
Expand Down
43 changes: 43 additions & 0 deletions Lina/ViewModels/BaseValidationRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentValidation;
using FluentValidation.Results;
using Lina.Common.Interfaces;

namespace Lina.ViewModels;

/// <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>
/// <typeparam name="TValidationClass">Validation class implementation</typeparam>
public abstract record BaseValidationRecord<TModel, TValidationClass> : IValidate, IBaseValidate<TModel, TValidationClass>
where TValidationClass : IValidator<TModel>
{
/// <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 GetBaseImplementationInstance().BaseValidate();
}

/// <summary>
/// Get validation errors
/// </summary>
/// <returns>Failure validation information</returns>
public virtual async Task<ValidationResult> GetErrors()
{
return await GetBaseImplementationInstance().BaseGetErrors();
}

/// <summary>
/// Verify if validation pass
/// </summary>
/// <returns>If valid</returns>
public virtual async ValueTask<bool> IsValid()
{
return await GetBaseImplementationInstance().BaseIsValid();
}

private IBaseValidate<TModel, TValidationClass> GetBaseImplementationInstance() => this;
}
4 changes: 3 additions & 1 deletion Lina/ViewModels/BaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Lina.ViewModels;


/// <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>
/// <typeparam name="TValidationClass">Validation class implementation</typeparam>
[Obsolete("Renamed to BaseValidationRecord")]
public abstract record BaseViewModel<TModel, TValidationClass> : IValidate, IBaseValidate<TModel, TValidationClass>
where TValidationClass : IValidator<TModel>
where TValidationClass : IValidator<TModel>
{
/// <summary>
/// Validate view model with class validation and throw exception if failure
Expand Down

0 comments on commit 1883695

Please sign in to comment.