Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 762
ashwani kumar Avatar asked Oct 22 '25 07:10

ashwani kumar


2 Answers

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

like image 136
Jason Desrosiers Avatar answered Oct 23 '25 19:10

Jason Desrosiers


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.

like image 45
Robby Cornelissen Avatar answered Oct 23 '25 21:10

Robby Cornelissen