How to validate JSON Schema according to Draft v4

2019-08-23 11:58发布

问题:

I have been looking into both JSON.NET Schema and NJsonSchema ... Both dont seem to have any propert / method that identifies if JSON Schema is a valid JSON Schema and in accordance with draft v4 Compatible.

Is it that only an exception that will identify if a schema is valid, and even if its vaid, how will i check that its draft v4 Comatible?

回答1:

You can use a JSON schema that describes JSON schema and use that to validate the JSON.

You can find a copy here - http://www.jsonschemavalidator.net/api/jsonschemastore/schema?schemaUrl=schema-draft-v4

string draftV4SchemaJson = @"{}"; // replace with content from http://www.jsonschemavalidator.net/api/jsonschemastore/schema?schemaUrl=schema-draft-v4

JSchema draftV4Schema = JSchema.Parse(draftV4SchemaJson);

JObject yourSchemaJson = JObject.Parse(@"{}"); // your schema

bool valid = yourSchemaJson.IsValid(draftV4Schema);