This project provides a simple way to validate JSON objects in dotnet. The main use for the tool is in integration tests.
$ dotnet add package JsonValidator.FluentAssertions{✓} JSON Validator for Fluent Assertions[!warning] Due to licencing changes, the FluentAssertions library won't be updated above v7.1 (the last truely free version). We recommend using Shouldly and our Shouldly extension instead.
This project provides a simple way to validate JSON objects in dotnet. The main use for the tool is in integration tests where you would — ideally — validate an API JSON response manually instead of using the model classes.
JsonValidator.FluentAssertions package depends on and uses both FluentAssertions and the basic JsonValidator package.Should() method.System.Text.Json.JsonDocument: Match(...) checks whether the JSON document matches the input object in both structure and values.System.Net.Http.HttpResponseMessage: HaveJsonBody(...) checks whether the HTTP response's body as a JSON string matches the input object in both structure and values.For JsonDocument:
var json = JsonDocument.Parse(myJsonString);
json.Should().Match(
new
{
name = "Anna Smith",
level = 2,
},
"Because...");
For HttpResponseMessage:
HttpResponseMessage response = await _client.SendAsync(request);
response.Should().HaveJsonBody(
new
{
name = "Anna Smith",
level = 2,
},
"Because...");