Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate a JSON Schema schema, in Python?

I am programmatically generating a JSON-Schema schema. I wish to ensure that the schema is valid. Is there a schema I can validate my schema against?

Please note my use of schema twice in that sentence and the title. I don't want to validate data against my schema, I want to validate my schema.

like image 292
GDorn Avatar asked Dec 11 '12 01:12

GDorn


People also ask

How do I check if a JSON Schema is valid?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

How do you validate schemas?

The Schema Validation filter can be found under the "Content Filtering" category of filters in the Policy Studio. Drag and drop the filter onto the policy where you want to perform schema validation. The Schema Validation filter dialog has 3 tabs, each of which will be explained in the following sections.

What is schema validation in Python?

schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.

How do you validate a JSON string in Python?

You can try to do json. loads() , which will throw a ValueError if the string you pass can't be decoded as JSON. In general, the "Pythonic" philosophy for this kind of situation is called EAFP, for Easier to Ask for Forgiveness than Permission.


1 Answers

Using jsonschema, you can validate a schema against the meta-schema. The core meta-schema is here, but jsonschema bundles it so downloading it is unnecessary.

from jsonschema import Draft3Validator my_schema = json.loads(my_text_file) #or however else you end up with a dict of the schema Draft3Validator.check_schema(my_schema) 
like image 152
GDorn Avatar answered Oct 14 '22 01:10

GDorn