Rico Suter edited this page Apr 25, 2018 · 13 revisions

Package: NJsonSchema

The JsonSchema4 class can be used as follows:

var schema = await JsonSchema4.FromTypeAsync<Person>();
var schemaData = schema.ToJson();

var jsonData = "{...}";
var errors = schema.Validate(jsonData);

foreach (var error in errors)
    Console.WriteLine(error.Path + ": " + error.Kind);

schema = await JsonSchema4.FromJsonAsync(schemaData);

To customize the JSON Schema generation, use the JsonSchemaGenerator.

ExtensionData property

Any property which is not specified in JSON Schema will be serialized and deserialized from the ExtensionData property on the JsonSchema4 class.

Validate(string) vs Validate(JToken)

You can either validate a JSON string or an already deserialized JToken. NJsonSchema cannot retrieve the initial date/time values from a JToken and thus may have problems when validating against string patterns. This is why you should prefer the Validate(string) method which handles date/time handling with the correct serializer settings.

Inheritance and allOf handling

Type inheritance (required for C# and TypeScript generation) is described via the allOf field with the following rules:

  • Schemas in allOf with a type of Object are treated as inherited schemas (exposed via the InheritedSchemas property)
  • Schemas in allOf with no type (i.e. type None) are merged into the parent schema (i.e. properties are combined and exposed via the ActualProperties property)
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.