0

I am using the jsonschema library (http://python-jsonschema.readthedocs.io/en/latest/validate/) in my Django application for server-side validation and am trying to do server-side validation of JSON with a provided schema. However, I get the error that the schema "is not valid under any of the given schemas."

Here is my schema (it is the "scores_ap" property of the "schema" property of the class):

class JSONListFieldSchemas: """ Schemas for all the JSON List Fields. Each key represents the field name. """ schema = { "scores_ap": { "$schema": "http://json-schema.org/draft-06/schema#", "title": "AP Scores", "type": "array", "items": { "type": "object", "properties": { "exam": { "type": "string" }, "score": { "type": "integer", "minimum": "1", "maximum": "5", "required": False } } } } } 

I am getting this error:

{'type': 'object', 'properties': {'score': {'minimum': '1', 'type': 'integer', 'ma ximum': '5', 'required': False}, 'exam': {'type': 'string'}}} is not valid under a ny of the given schemas Failed validating u'anyOf' in schema[u'properties'][u'items']: {u'anyOf': [{u'$ref': u'#'}, {u'$ref': u'#/definitions/schemaArray'}], u'default': {}} On instance[u'items']: {'properties': {'exam': {'type': 'string'}, 'score': {'maximum': '5', 'minimum': '1', 'required': False, 'type': 'integer'}}, 'type': 'object'} 

I am using the schema as follows:

from jsonschema import validate from .schemas import JSONListFieldSchemas raw_value = [{"score": 1, "exam": "a"}] validate(raw_value, JSONListFieldSchemas.schema['scores_ap']) 

1 Answer 1

1

From draft 4 and on, "required" should be an array not a boolean. Also "maximum" and "minimum" should be integers, not strings.

Try like this:

{ "$schema": "http://json-schema.org/draft-06/schema#", "title": "AP Scores", "type": "array", "items": { "type": "object", "properties": { "exam": { "type": "string" }, "score": { "type": "integer", "minimum": 1, "maximum": 5 } }, "required": [ "exam" ] } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! The data in my code was an array though -- or am I declaring it wrong?
Yes, sorry, my mistake, didn't look closely. I'll update my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.