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 37 3/22/2024
1.0.47 253 1/24/2024
1.0.46 37 1/24/2024
1.0.44 88 9/16/2023
1.0.43 97 3/21/2023
1.0.40 84 3/14/2023
1.0.0 191 3/14/2023
0.2.20 284 9/30/2020
0.2.19 221 9/30/2020
0.2.16 983 5/7/2020
0.2.15 647 11/3/2017
0.2.14 963 8/30/2017
0.2.13-sha.7f7db56 566 8/28/2017
0.2.11-sha.1c80dca 556 8/23/2017
0.2.10-sha.0f8f070 568 8/23/2017
0.2.9-sha.ba7a076 563 8/23/2017
0.2.8-sha.920a337 557 8/23/2017
0.2.7-sha.178dfe8 571 8/23/2017
0.2.3 954 8/22/2017
0.1.3 1,036 5/9/2017
0.1.2 957 4/3/2017
0.0.84 952 4/3/2017
0.0.83 947 4/3/2017
0.0.82 1,013 2/23/2017
0.0.81 969 2/20/2017
0.0.80 1,042 2/14/2017
0.0.76 980 2/14/2017
0.0.75 954 1/24/2017
0.0.74 963 1/16/2017
0.0.73 969 1/16/2017
0.0.71 989 1/16/2017
0.0.70 1,021 1/10/2017
0.0.69 945 1/6/2017
0.0.68 995 1/5/2017
0.0.66 1,012 1/5/2017
0.0.65 967 1/5/2017
0.0.64 967 1/5/2017
0.0.63 989 1/5/2017
0.0.62 1,005 1/4/2017
0.0.61 964 1/4/2017
0.0.60 993 1/4/2017
0.0.59 983 1/1/2017
0.0.58 973 12/21/2016
0.0.57 989 12/20/2016
0.0.56 994 12/20/2016
0.0.55 991 12/20/2016
0.0.54 988 12/20/2016
0.0.53 992 12/20/2016
0.0.52 998 12/20/2016
0.0.51 1,007 12/19/2016
0.0.50 1,027 12/19/2016
0.0.49 992 12/17/2016
0.0.48 1,004 12/17/2016
0.0.47 989 12/16/2016
0.0.46 1,008 12/16/2016
0.0.45 973 12/16/2016
0.0.44 965 12/15/2016
0.0.43 991 12/15/2016
0.0.42 999 12/14/2016
0.0.41 957 12/14/2016
0.0.33 972 12/2/2016
0.0.32 1,015 10/25/2016
0.0.27 1,049 2/18/2016
0.0.26 1,052 2/11/2016
0.0.24 1,140 1/7/2016
0.0.23 1,055 1/6/2016
0.0.22 1,038 1/6/2016
0.0.21 1,028 1/6/2016
0.0.20 1,000 1/6/2016
0.0.18 1,051 1/6/2016
0.0.17 1,000 1/6/2016