ashwani kumar
ashwani kumar

Reputation: 147

How to check if a string is a valid Json Schema?

If I have a string = "{}", which is not a valid Json Schema, how do I check it using jackson whether it is a valid Json Schema or not?

Upvotes: 4

Views: 4620

Answers (2)

Jason Desrosiers
Jason Desrosiers

Reputation: 24399

Draft 2019-09 defines some new keywords that allow you to declare that the contents of a string is JSON and conforms to a given schema. However, these keywords are informative only. No assertions are enforced. You would have to extract that information from the schema and do the validation separately.

It would look something like this.

{
  "type": "string",
  "contentMediaType": "application/schema+json",
  "contentSchema": { "$ref": "https://json-schema.org/draft/2019-09" }
}

Also keep in mind that draft 2019-09 doesn't have a lot of implementations at this point, so you might have difficultly finding tools that understand the new keywords. We've seen quite a bit of progress on that front recently, so hopefully that won't be the case for too much longer.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-02#section-8

Upvotes: 3

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

Because a JSON schema is itself a JSON document, you can validate it using a... JSON schema.

There are JSON meta-schemas available that can be used to validate your schema against on the JSON Schema website , so to check whether your schema is valid, it's sufficient to validate it against the correct meta-schema.

Meta-schemas can be found here, and a list of libraries to perform the actual validation can be found here.

Upvotes: 3

Related Questions