DotJEM.Json.Validation 1.0.48

This package has a SemVer 2.0.0 package version: 1.0.48+sha.aeecc92.
dotnet add package DotJEM.Json.Validation --version 1.0.48                
NuGet\Install-Package DotJEM.Json.Validation -Version 1.0.48                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotJEM.Json.Validation" Version="1.0.48" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotJEM.Json.Validation --version 1.0.48                
#r "nuget: DotJEM.Json.Validation, 1.0.48"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DotJEM.Json.Validation as a Cake Addin
#addin nuget:?package=DotJEM.Json.Validation&version=1.0.48

// Install DotJEM.Json.Validation as a Cake Tool
#tool nuget:?package=DotJEM.Json.Validation&version=1.0.48                

Build status FOSSA Status

json-validation

While JsonSchema largely allows to validate Json objects, the seemed to be dificult to figure out especially when it comes to cross validation and conditional validation. For obvious reasons validations in a schema is also more static in nature which could mean that these validation schemas would become obsolete faster than they where generated.

This framwork tries to make it easy to write validators in C# that in a fluent syntax which is easy to understand.

Highlights:

  • Provides a straigt forward fluent syntax for writing validation rules.
  • Provides an extensible API which allows developers to add aditional features to the framework fast and easy, such constraints could be very domain specific and even interact with configurable sources.
  • Provides a way to generate meaningfull descriptions of the validations both for human and machine. Hereunder allow for decorating JsonSchemas with the constraints that can be expressed.
  • Provides a way to generate meaningfull descriptions of validation errors for both human and machine.

Example:

Given the user validator below.

    public class UserValidator : JsonValidator
    {
        public UserValidator()
        {
            When(Any)
                .Then(Field("id", Is.Required() & Must.Be.Number() & Must.Be.GreaterThan(0))
                    & Field("username", Is.Required() & Must.Be.String() & Must.Have.MinLength(2))
                    & Field("email", Is.Required() & Must.Match(@"^[^@]+@[^@]+\.[^@]+$")));

            When("name", Is.Defined())
                .Then(It, Must.Be.String() & Have.MaxLength(256));

            When(Field("company", Is.Defined()) | Field("address", Is.Defined()))
                .Then("address", Is.Required());

            When("address", Is.Defined() & Is.Object())
                .Use<AddressValidator>()
                .For(It);
            
            When("company", Is.Defined())
                .Then("company.name", Is.Required() & Must.Be.String() & Have.LengthBetween(3, 256));
        }
    }

The fluent syntax stays very close to how one would express the rules in natural english which makes the rules easy to read and understand.

If the following JSON is passed though the validator:

{
  "name": null, "company": { }
}

As so:

 validator.Validate(JObject.Parse("{ "name": null, "company": { } }"), null);

And then later described with an example implementation of a descriptor, the output looks like this:

When
    ANY
Then
    (
        id
        (
            is required - actual value was: NULL
            AND
            must be a number (strict: True) - actual value was: NULL
            AND
            must be greather than 0 - actual value was: NULL
        )
        AND
        username
        (
            is required - actual value was: NULL
            AND
            must be a string - actual value was: NULL
            AND
            must have length more than or equal to '2' - actual value was: NULL
        )
        AND
        email
        is required - actual value was: NULL
    )

When
    name
    is defined
Then
    name
    must be a string - actual value was: 

When
    (
        company
        is defined
        OR
        address
        is defined
    )
Then
    address
    is required - actual value was: NULL

When
    company
    is defined
Then
    company.name
    (
        is required - actual value was: NULL
        AND
        must be a string - actual value was: NULL
        AND
        have length from '3' to '256' - actual value was: NULL
    )

This is a bit verbose, but shows that the result of the validation can be converted to something that is fairly readable by an average user. By using the fluent syntax, much of the information we put into the validator as pure code is preserved and can be used to generate an output.

By implementing custom descriptors, developers can build their own output.

Extending with new constraints

It is easy to extend the framework with new constrains, an example of this could be an EmailConstraint which validates a string as an email

To do this, first implement the actual constraint:

[JsonConstraintDescription("valid email")]
public class ValidEmailConstraint : JsonConstraint
{
    private static readonly Regex pattern = new Regex("emailpattern", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    public override bool Matches(JToken token, IJsonValidationContext context)
        => token?.Type == JTokenType.String && pattern.IsMatch((string)token);
}

Then add an extension method that targets the appropirate interface, in this case "must be valid email" sounds right so we targets the "IBeConstraintFactory":

public static CapturedConstraint ValidEmail(this IBeConstraintFactory self)
    => self.Capture(new ValidEmailConstraint());

It is now possible to use the new constraint as:

    public class UserValidator : JsonValidator
    {
        public UserValidator()
        {
            When("email", Is.Defined())
                .Then(It, Must.Be.ValidEmail());
        }
    }

License

FOSSA Status

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.48 110 3/22/2024
1.0.47 845 1/24/2024
1.0.46 62 1/24/2024
1.0.44 122 9/16/2023
1.0.43 117 3/21/2023
1.0.40 96 3/14/2023
1.0.0 221 3/14/2023
0.2.20 308 9/30/2020
0.2.19 244 9/30/2020
0.2.16 1,008 5/7/2020
0.2.15 672 11/3/2017
0.2.14 1,003 8/30/2017
0.2.13-sha.7f7db56 585 8/28/2017
0.2.11-sha.1c80dca 571 8/23/2017
0.2.10-sha.0f8f070 583 8/23/2017
0.2.9-sha.ba7a076 579 8/23/2017
0.2.8-sha.920a337 577 8/23/2017
0.2.7-sha.178dfe8 587 8/23/2017
0.2.3 988 8/22/2017
0.1.3 1,080 5/9/2017
0.1.2 995 4/3/2017
0.0.84 986 4/3/2017
0.0.83 988 4/3/2017
0.0.82 1,048 2/23/2017
0.0.81 1,003 2/20/2017
0.0.80 1,083 2/14/2017
0.0.76 1,020 2/14/2017
0.0.75 987 1/24/2017
0.0.74 995 1/16/2017
0.0.73 1,002 1/16/2017
0.0.71 1,023 1/16/2017
0.0.70 1,055 1/10/2017
0.0.69 984 1/6/2017
0.0.68 1,028 1/5/2017
0.0.66 1,048 1/5/2017
0.0.65 1,000 1/5/2017
0.0.64 1,000 1/5/2017
0.0.63 1,024 1/5/2017
0.0.62 1,038 1/4/2017
0.0.61 997 1/4/2017
0.0.60 1,020 1/4/2017
0.0.59 1,010 1/1/2017
0.0.58 1,011 12/21/2016
0.0.57 1,035 12/20/2016
0.0.56 1,024 12/20/2016
0.0.55 1,022 12/20/2016
0.0.54 1,036 12/20/2016
0.0.53 1,020 12/20/2016
0.0.52 1,026 12/20/2016
0.0.51 1,039 12/19/2016
0.0.50 1,057 12/19/2016
0.0.49 1,022 12/17/2016
0.0.48 1,038 12/17/2016
0.0.47 1,022 12/16/2016
0.0.46 1,043 12/16/2016
0.0.45 1,001 12/16/2016
0.0.44 996 12/15/2016
0.0.43 1,027 12/15/2016
0.0.42 1,037 12/14/2016
0.0.41 985 12/14/2016
0.0.33 1,000 12/2/2016
0.0.32 1,050 10/25/2016
0.0.27 1,079 2/18/2016
0.0.26 1,082 2/11/2016
0.0.24 1,178 1/7/2016
0.0.23 1,084 1/6/2016
0.0.22 1,068 1/6/2016
0.0.21 1,061 1/6/2016
0.0.20 1,033 1/6/2016
0.0.18 1,079 1/6/2016
0.0.17 1,044 1/6/2016