Skip to content
Rico Suter edited this page Aug 17, 2016 · 14 revisions

Package: NJsonSchema

The JsonSchema4 class can be used as follows:

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

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

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

schema = JsonSchema4.FromJson(schemaData);

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 AllProperties property)
Clone this wiki locally