RSCG – validly
| name | validly |
| nuget | https://www.nuget.org/packages/validly/ |
| link | https://github.com/Hookyns/validly |
| author | Roman Jambor |
Generates validation code for C# classes based on attributes.
This is how you can use validly .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="Validly" Version="1.1.5" /> <PackageReference Include="Validly.Extensions.Validators" Version="1.1.3" /> <PackageReference Include="Validly.SourceGenerator" Version="1.1.5" /> </ItemGroup> </Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information
using Valid;
Console.WriteLine("Hello, World!");
Person p= new (){ Age = 55, Name = "Andrei" };
p.Validate();
Console.WriteLine("Person is valid");
using System.ComponentModel.DataAnnotations;
using Validly;
namespace Valid;
[Validatable]
public partial class Person
{
[Range(18, 199)]
public int Age { get; set; }
<pre><code>[Required]
[MinLength(3)]
public string Name { get; set; } = string.Empty;</code></pre>
}
The code that is generated is
// <auto-generated/>
<h1>nullable enable</h1>
namespace Valid
{
#pragma warning disable CS0105
using System.ComponentModel.DataAnnotations;
using Validly;
#pragma warning restore CS0105
<pre><code>public partial class Person
: global::Validly.IValidatable, global::Validly.Validators.IInternalValidationInvoker
{
/// <inheritdoc />
ValueTask<global::Validly.ValidationResult> global::Validly.Validators.IInternalValidationInvoker.ValidateAsync(
global::Validly.ValidationContext context,
global::System.IServiceProvider? serviceProvider
)
{
var result = (global::Validly.IInternalValidationResult)global::Validly.ExtendableValidationResult.Create(2);
global::Validly.IExpandablePropertyValidationResult propertyResult;
// Validate "Name" property
context.SetProperty("Name");
propertyResult = result.InitProperty("Name");
propertyResult.Add(PersonRules.NameRule.Item1.IsValid(Name));
return new ValueTask<global::Validly.ValidationResult>((global::Validly.ValidationResult)result);
}
/// <inheritdoc />
async ValueTask<global::Validly.ValidationResult> global::Validly.IValidatable.ValidateAsync(IServiceProvider serviceProvider){
using var validationContext = global::Validly.ValidationContext.Create(this);
return await ((global::Validly.Validators.IInternalValidationInvoker)this).ValidateAsync(validationContext, serviceProvider);
}
/// <summary>
/// Validate the object and get the result with error messages.
/// </summary>
/// <returns>Returns disposable ValidationResult.</returns>
public virtual async ValueTask<global::Validly.ValidationResult> ValidateAsync()
{
using var validationContext = global::Validly.ValidationContext.Create(this);
return await ((global::Validly.Validators.IInternalValidationInvoker)this).ValidateAsync(validationContext, null);
}
/// <summary>
/// Validate the object and get the result with error messages.
/// </summary>
/// <returns>Returns disposable ValidationResult.</returns>
public virtual global::Validly.ValidationResult Validate()
{
using var validationContext = global::Validly.ValidationContext.Create(this);
var task = ((global::Validly.Validators.IInternalValidationInvoker)this).ValidateAsync(validationContext, null);
if (!task.IsCompletedSuccessfully)
{
#if NET7_0_OR_GREATER
throw new global::System.Diagnostics.UnreachableException("The task should be completed synchronously but was not.");
#else
throw new global::System.InvalidOperationException("The task should be completed synchronously but was not.");
#endif
}
return task.Result;
}
}
file static class PersonRules
{
internal static readonly ValueTuple<
global::Validly.Extensions.Validators.Common.RequiredAttribute,
object?
> NameRule = (
new global::Validly.Extensions.Validators.Common.RequiredAttribute(allowEmptyStrings: true),
null
);
}</code></pre>
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/validly