-
-
Notifications
You must be signed in to change notification settings - Fork 534
JsonSchema
Rico Suter edited this page Jan 17, 2020
·
4 revisions
- Package: NJsonSchema
- Type: NJsonSchema.JsonSchema
The JsonSchema
class can be used as follows:
var schema = JsonSchema.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 = await JsonSchema.FromJsonAsync(schemaData);
To customize the JSON Schema generation, use the JsonSchemaGenerator.
Any property which is not specified in JSON Schema will be serialized and deserialized from the ExtensionData
property on the JsonSchema
class.
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.
Type inheritance (required for C# and TypeScript generation) is described via the allOf
field with the following rules:
- Schemas in
allOf
with a type ofObject
are treated as inherited schemas (exposed via theInheritedSchemas
property) - Schemas in
allOf
with no type (i.e. typeNone
) are merged into the parent schema (i.e. properties are combined and exposed via theActualProperties
property)