RSCG – validly

RSCG – validly
 
 

namevalidly
nugethttps://www.nuget.org/packages/validly/
linkhttps://github.com/Hookyns/validly
authorRoman 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
{
    /// &lt;inheritdoc /&gt;
    ValueTask&lt;global::Validly.ValidationResult&gt; 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&lt;global::Validly.ValidationResult&gt;((global::Validly.ValidationResult)result);
    }

    /// &lt;inheritdoc /&gt;
    async ValueTask&lt;global::Validly.ValidationResult&gt; global::Validly.IValidatable.ValidateAsync(IServiceProvider serviceProvider){
        using var validationContext = global::Validly.ValidationContext.Create(this);
        return await ((global::Validly.Validators.IInternalValidationInvoker)this).ValidateAsync(validationContext, serviceProvider);
    }

    /// &lt;summary&gt;
    /// Validate the object and get the result with error messages.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;Returns disposable ValidationResult.&lt;/returns&gt;
    public virtual async ValueTask&lt;global::Validly.ValidationResult&gt; ValidateAsync()
    {
        using var validationContext = global::Validly.ValidationContext.Create(this);
        return await ((global::Validly.Validators.IInternalValidationInvoker)this).ValidateAsync(validationContext, null);
    }


    /// &lt;summary&gt;
    /// Validate the object and get the result with error messages.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;Returns disposable ValidationResult.&lt;/returns&gt;
    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&lt;
        global::Validly.Extensions.Validators.Common.RequiredAttribute,
        object?
    &gt; 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


Posted

in

, ,

by

Tags: