django postgresql json field schema validation

I wrote a custom validator using jsonschema in order to do this. project/validators.py import django from django.core.validators import BaseValidator import jsonschema class JSONSchemaValidator(BaseValidator): def compare(self, value, schema): try: jsonschema.validate(value, schema) except jsonschema.exceptions.ValidationError: raise django.core.exceptions.ValidationError( ‘%(value)s failed JSON schema check’, params={‘value’: value} ) project/app/models.py from django.db import models from project.validators import JSONSchemaValidator MY_JSON_FIELD_SCHEMA = { ‘schema’: … Read more

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

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)

How to represent sum/union types in in json schema

Use anyOf to assert that the property must conform to one or another schema. { “type”: “object”, “properties”: { “tracking_number”: { “anyOf”: [ { “$ref”: “#/definitions/tracking_number” }, { “type”: “array”, “items”: { “$ref”: “#/definitions/tracking_number” } ] }, “definitions”: { “tracking_number”: { “type”: “integer” } } }

How to create JSON Schema for Name/Value structure?

Assuming your validator supports it you can use patternProperties. For the schema… { “title”: “Map<String,String>”, “type”: “object”, “patternProperties”: { “.{1,}”: { “type”: “string” } } } …and the document… { “foo”:”bar”, “baz”:1 } …the value of property foo is valid because it is a string but baz fails validation because it is a number.

JSON schema validation

Is there a stable library that can validate JSON against a schema? I found a couple hits on google: From the Chromium project: http://aaronboodman-com-v1.blogspot.com/2010/11/c-version-of-json-schema.html http://avro.apache.org/docs/1.4.1/api/cpp/html/index.html You could also plug a Python or Javascript interpreter into your app, and simply run the native version of those validator implementations that you’ve already found. Is there a reason … Read more

JSON Schema: validate a number-or-null value

In draft-04, you would use the anyOf directive: { “anyOf”: [ { “type”: “number”, “minimum”: 0, “maximum”: 360, “exclusiveMaximum”: true }, { “type”: “null” } ] } You could also use “type”: [“number”, “null”] as Adam suggests, but I think anyOf is cleaner (as long as you use a draft-04 implementation), and ties the minimum … Read more