DotJEM.Json.Validation
1.0.47
See the version list below for details.
dotnet add package DotJEM.Json.Validation --version 1.0.47
NuGet\Install-Package DotJEM.Json.Validation -Version 1.0.47
<PackageReference Include="DotJEM.Json.Validation" Version="1.0.47" />
paket add DotJEM.Json.Validation --version 1.0.47
#r "nuget: DotJEM.Json.Validation, 1.0.47"
// Install DotJEM.Json.Validation as a Cake Addin #addin nuget:?package=DotJEM.Json.Validation&version=1.0.47 // Install DotJEM.Json.Validation as a Cake Tool #tool nuget:?package=DotJEM.Json.Validation&version=1.0.47
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 | 112 | 3/22/2024 |
1.0.47 | 847 | 1/24/2024 |
1.0.46 | 64 | 1/24/2024 |
1.0.44 | 124 | 9/16/2023 |
1.0.43 | 119 | 3/21/2023 |
1.0.40 | 98 | 3/14/2023 |
1.0.0 | 223 | 3/14/2023 |
0.2.20 | 310 | 9/30/2020 |
0.2.19 | 246 | 9/30/2020 |
0.2.16 | 1,010 | 5/7/2020 |
0.2.15 | 674 | 11/3/2017 |
0.2.14 | 1,005 | 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 | 990 | 8/22/2017 |
0.1.3 | 1,082 | 5/9/2017 |
0.1.2 | 997 | 4/3/2017 |
0.0.84 | 988 | 4/3/2017 |
0.0.83 | 990 | 4/3/2017 |
0.0.82 | 1,050 | 2/23/2017 |
0.0.81 | 1,005 | 2/20/2017 |
0.0.80 | 1,085 | 2/14/2017 |
0.0.76 | 1,022 | 2/14/2017 |
0.0.75 | 989 | 1/24/2017 |
0.0.74 | 997 | 1/16/2017 |
0.0.73 | 1,004 | 1/16/2017 |
0.0.71 | 1,025 | 1/16/2017 |
0.0.70 | 1,057 | 1/10/2017 |
0.0.69 | 986 | 1/6/2017 |
0.0.68 | 1,030 | 1/5/2017 |
0.0.66 | 1,050 | 1/5/2017 |
0.0.65 | 1,002 | 1/5/2017 |
0.0.64 | 1,002 | 1/5/2017 |
0.0.63 | 1,026 | 1/5/2017 |
0.0.62 | 1,040 | 1/4/2017 |
0.0.61 | 999 | 1/4/2017 |
0.0.60 | 1,022 | 1/4/2017 |
0.0.59 | 1,012 | 1/1/2017 |
0.0.58 | 1,013 | 12/21/2016 |
0.0.57 | 1,037 | 12/20/2016 |
0.0.56 | 1,026 | 12/20/2016 |
0.0.55 | 1,024 | 12/20/2016 |
0.0.54 | 1,038 | 12/20/2016 |
0.0.53 | 1,022 | 12/20/2016 |
0.0.52 | 1,028 | 12/20/2016 |
0.0.51 | 1,041 | 12/19/2016 |
0.0.50 | 1,059 | 12/19/2016 |
0.0.49 | 1,024 | 12/17/2016 |
0.0.48 | 1,040 | 12/17/2016 |
0.0.47 | 1,024 | 12/16/2016 |
0.0.46 | 1,045 | 12/16/2016 |
0.0.45 | 1,003 | 12/16/2016 |
0.0.44 | 998 | 12/15/2016 |
0.0.43 | 1,029 | 12/15/2016 |
0.0.42 | 1,039 | 12/14/2016 |
0.0.41 | 987 | 12/14/2016 |
0.0.33 | 1,002 | 12/2/2016 |
0.0.32 | 1,052 | 10/25/2016 |
0.0.27 | 1,081 | 2/18/2016 |
0.0.26 | 1,084 | 2/11/2016 |
0.0.24 | 1,180 | 1/7/2016 |
0.0.23 | 1,086 | 1/6/2016 |
0.0.22 | 1,070 | 1/6/2016 |
0.0.21 | 1,063 | 1/6/2016 |
0.0.20 | 1,035 | 1/6/2016 |
0.0.18 | 1,081 | 1/6/2016 |
0.0.17 | 1,046 | 1/6/2016 |