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 | 126 | 3/22/2024 |
1.0.47 | 870 | 1/24/2024 |
1.0.46 | 66 | 1/24/2024 |
1.0.44 | 128 | 9/16/2023 |
1.0.43 | 121 | 3/21/2023 |
1.0.40 | 100 | 3/14/2023 |
1.0.0 | 225 | 3/14/2023 |
0.2.20 | 312 | 9/30/2020 |
0.2.19 | 248 | 9/30/2020 |
0.2.16 | 1,012 | 5/7/2020 |
0.2.15 | 680 | 11/3/2017 |
0.2.14 | 1,011 | 8/30/2017 |
0.2.13-sha.7f7db56 | 591 | 8/28/2017 |
0.2.11-sha.1c80dca | 577 | 8/23/2017 |
0.2.10-sha.0f8f070 | 589 | 8/23/2017 |
0.2.9-sha.ba7a076 | 585 | 8/23/2017 |
0.2.8-sha.920a337 | 583 | 8/23/2017 |
0.2.7-sha.178dfe8 | 593 | 8/23/2017 |
0.2.3 | 996 | 8/22/2017 |
0.1.3 | 1,088 | 5/9/2017 |
0.1.2 | 1,003 | 4/3/2017 |
0.0.84 | 994 | 4/3/2017 |
0.0.83 | 996 | 4/3/2017 |
0.0.82 | 1,056 | 2/23/2017 |
0.0.81 | 1,011 | 2/20/2017 |
0.0.80 | 1,091 | 2/14/2017 |
0.0.76 | 1,028 | 2/14/2017 |
0.0.75 | 995 | 1/24/2017 |
0.0.74 | 1,003 | 1/16/2017 |
0.0.73 | 1,010 | 1/16/2017 |
0.0.71 | 1,031 | 1/16/2017 |
0.0.70 | 1,063 | 1/10/2017 |
0.0.69 | 992 | 1/6/2017 |
0.0.68 | 1,036 | 1/5/2017 |
0.0.66 | 1,056 | 1/5/2017 |
0.0.65 | 1,008 | 1/5/2017 |
0.0.64 | 1,008 | 1/5/2017 |
0.0.63 | 1,032 | 1/5/2017 |
0.0.62 | 1,046 | 1/4/2017 |
0.0.61 | 1,005 | 1/4/2017 |
0.0.60 | 1,028 | 1/4/2017 |
0.0.59 | 1,018 | 1/1/2017 |
0.0.58 | 1,019 | 12/21/2016 |
0.0.57 | 1,043 | 12/20/2016 |
0.0.56 | 1,032 | 12/20/2016 |
0.0.55 | 1,030 | 12/20/2016 |
0.0.54 | 1,044 | 12/20/2016 |
0.0.53 | 1,028 | 12/20/2016 |
0.0.52 | 1,034 | 12/20/2016 |
0.0.51 | 1,047 | 12/19/2016 |
0.0.50 | 1,065 | 12/19/2016 |
0.0.49 | 1,030 | 12/17/2016 |
0.0.48 | 1,046 | 12/17/2016 |
0.0.47 | 1,030 | 12/16/2016 |
0.0.46 | 1,051 | 12/16/2016 |
0.0.45 | 1,009 | 12/16/2016 |
0.0.44 | 1,004 | 12/15/2016 |
0.0.43 | 1,035 | 12/15/2016 |
0.0.42 | 1,045 | 12/14/2016 |
0.0.41 | 993 | 12/14/2016 |
0.0.33 | 1,008 | 12/2/2016 |
0.0.32 | 1,058 | 10/25/2016 |
0.0.27 | 1,087 | 2/18/2016 |
0.0.26 | 1,090 | 2/11/2016 |
0.0.24 | 1,186 | 1/7/2016 |
0.0.23 | 1,092 | 1/6/2016 |
0.0.22 | 1,076 | 1/6/2016 |
0.0.21 | 1,071 | 1/6/2016 |
0.0.20 | 1,041 | 1/6/2016 |
0.0.18 | 1,087 | 1/6/2016 |
0.0.17 | 1,052 | 1/6/2016 |