DotJEM.Json.Validation
1.0.48
dotnet add package DotJEM.Json.Validation --version 1.0.48
NuGet\Install-Package DotJEM.Json.Validation -Version 1.0.48
<PackageReference Include="DotJEM.Json.Validation" Version="1.0.48" />
paket add DotJEM.Json.Validation --version 1.0.48
#r "nuget: DotJEM.Json.Validation, 1.0.48"
// 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
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
Product | Versions 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. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
- Newtonsoft.Json (>= 13.0.3)
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 | 70 | 3/22/2024 |
1.0.47 | 537 | 1/24/2024 |
1.0.46 | 51 | 1/24/2024 |
1.0.44 | 109 | 9/16/2023 |
1.0.43 | 108 | 3/21/2023 |
1.0.40 | 89 | 3/14/2023 |
1.0.0 | 197 | 3/14/2023 |
0.2.20 | 290 | 9/30/2020 |
0.2.19 | 232 | 9/30/2020 |
0.2.16 | 997 | 5/7/2020 |
0.2.15 | 660 | 11/3/2017 |
0.2.14 | 973 | 8/30/2017 |
0.2.13-sha.7f7db56 | 574 | 8/28/2017 |
0.2.11-sha.1c80dca | 560 | 8/23/2017 |
0.2.10-sha.0f8f070 | 571 | 8/23/2017 |
0.2.9-sha.ba7a076 | 566 | 8/23/2017 |
0.2.8-sha.920a337 | 566 | 8/23/2017 |
0.2.7-sha.178dfe8 | 574 | 8/23/2017 |
0.2.3 | 962 | 8/22/2017 |
0.1.3 | 1,052 | 5/9/2017 |
0.1.2 | 966 | 4/3/2017 |
0.0.84 | 962 | 4/3/2017 |
0.0.83 | 955 | 4/3/2017 |
0.0.82 | 1,024 | 2/23/2017 |
0.0.81 | 977 | 2/20/2017 |
0.0.80 | 1,058 | 2/14/2017 |
0.0.76 | 994 | 2/14/2017 |
0.0.75 | 963 | 1/24/2017 |
0.0.74 | 971 | 1/16/2017 |
0.0.73 | 977 | 1/16/2017 |
0.0.71 | 1,003 | 1/16/2017 |
0.0.70 | 1,032 | 1/10/2017 |
0.0.69 | 954 | 1/6/2017 |
0.0.68 | 1,003 | 1/5/2017 |
0.0.66 | 1,030 | 1/5/2017 |
0.0.65 | 974 | 1/5/2017 |
0.0.64 | 975 | 1/5/2017 |
0.0.63 | 998 | 1/5/2017 |
0.0.62 | 1,012 | 1/4/2017 |
0.0.61 | 979 | 1/4/2017 |
0.0.60 | 1,002 | 1/4/2017 |
0.0.59 | 991 | 1/1/2017 |
0.0.58 | 990 | 12/21/2016 |
0.0.57 | 999 | 12/20/2016 |
0.0.56 | 1,003 | 12/20/2016 |
0.0.55 | 1,004 | 12/20/2016 |
0.0.54 | 998 | 12/20/2016 |
0.0.53 | 1,000 | 12/20/2016 |
0.0.52 | 1,008 | 12/20/2016 |
0.0.51 | 1,014 | 12/19/2016 |
0.0.50 | 1,039 | 12/19/2016 |
0.0.49 | 1,000 | 12/17/2016 |
0.0.48 | 1,018 | 12/17/2016 |
0.0.47 | 1,004 | 12/16/2016 |
0.0.46 | 1,016 | 12/16/2016 |
0.0.45 | 983 | 12/16/2016 |
0.0.44 | 978 | 12/15/2016 |
0.0.43 | 1,005 | 12/15/2016 |
0.0.42 | 1,008 | 12/14/2016 |
0.0.41 | 965 | 12/14/2016 |
0.0.33 | 980 | 12/2/2016 |
0.0.32 | 1,024 | 10/25/2016 |
0.0.27 | 1,059 | 2/18/2016 |
0.0.26 | 1,062 | 2/11/2016 |
0.0.24 | 1,155 | 1/7/2016 |
0.0.23 | 1,064 | 1/6/2016 |
0.0.22 | 1,050 | 1/6/2016 |
0.0.21 | 1,042 | 1/6/2016 |
0.0.20 | 1,010 | 1/6/2016 |
0.0.18 | 1,061 | 1/6/2016 |
0.0.17 | 1,015 | 1/6/2016 |