Reputation: 31
I am writing a program with Python/Flask and am using a package called flask-jsonschema-validator for JSON validation. When I validate my JSON I get the following error:
jsonschema.exceptions.SchemaError: [{'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'type': 'string'}]}, {'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'valid-values': {'type': 'string'}}, {'invalid-values': {'type': 'string'}}, {'isinteger': {'type': 'string'}}, {'nullable': {'type': 'string'}}]}] is not of type 'object', 'boolean'
Here is the JSON schema I am using
{
"validate": {
"type": "object",
"properties": {
"_id": { "type": "string", "minLength": 2, "maxLength": 100 },
"name": { "type": "string", "minLength": 2, "maxLength": 100 },
"type": { "type": "string", "minLength": 2, "maxLength": 100 },
"subtype": { "type": "string", "minLength": 2, "maxLength": 100 },
"domain-data-version": {"type": "string"},
"description": { "type": "string", "minLength": 2, "maxLength": 100 },
"created" : {"type": "string"},
"owner-org": {"type": "string"},
"domain-data":[
{
"fieldname": {"type": "string"},
"type": {"type": "string"},
"description": {"type": "string"},
"default-value": {"type": "string"},
"validation": [{"type": "string"}]},
{"fieldname": {"type": "string"},
"type": {"type": "string"},
"description": {"type": "string"},
"default-value": {"type": "string"},
"validation": [ {"valid-values": {"type": "string"}},
{"invalid-values": {"type": "string"}},
{"isinteger": {"type": "string"}},
{"nullable": {"type": "string"}}]
}]
},
"required": []
}
}
Here is the JSON I would use to validate
{"name": "PHARMACY-CLAIM", "type": "Pharmacy", "subtype": "Prescription Filled", "domain-data-version": "1", "domain-data": [{"fieldname": "claim-id", "type": "string", "description": "The Insurance claim ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "member-gen-key", "type": "string", "description": "The unique insurance Member ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "ndc", "type": "string", "description": "The National Drug Code value for the medication prescription filled.", "default-value": "null", "validation": [{"nullable": "false"}]}]}
When I remove the "domain-data portion of the schema, the JSON will validate. I think the problem comes from the fact the domian-data is an array of objects but I am not sure what to do about it. Thanks for the help.
Upvotes: 2
Views: 7160
Reputation: 61
for anyone else reading this, and you are using array, try "prefixItems" instead of "items". Check out this link to get more of a feel. https://json-schema.org/understanding-json-schema/reference/array.html
Upvotes: 4
Reputation: 960
I had the same problem as you. Although it seems to be a schema error, it was a problem with JSON manipulation for me. I was working with a string content and not with a JSON object. I've built your schema and it succeeded. I put your schema in a file called teste.json.
>>> from jsonschema import Draft7Validator
>>> import json
>>> Draft7Validator.check_schema(json.load(open('teste.json')))
Probably you are not loading this content as a JSON object (json.load()). FYI, Draft7Validator is an implementation class for validating 7th version of JSON schema. Use the one that suits your needs.
About your schema, I suggest you to follow JSON Schema documentation. I couldn't validate your schema and instance without using definitions. It seems that you have two kinds of validation for your domain data. Then using extension will avoid code repetition, as illustrated here. I refactored your schema and it seems to work as expected.
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Validate",
"definitions": {
"Validate": {
"type": "object",
"additionalProperties": false,
"properties": {
"_id": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"name": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"type": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"subtype": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"domain-data-version": {
"type": "string"
},
"description": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"created": {
"type": "string"
},
"owner-org": {
"type": "string"
},
"domain-data": {
"type": "array",
"items": {
"$ref": "#/definitions/Domain-data"
}
}
}
},
"Domain-data": {
"type": "object",
"additionalProperties": false,
"properties": {
"fieldname": {
"type": "string"
},
"type": {
"type": "string"
},
"description": {
"type": "string"
},
"default-value": {
"type": "string"
},
"validation": {
"oneOf": [
{ "$ref": "#/definitions/DomainDataV1" },
{ "$ref": "#/definitions/DomainDataV2" }
]
}
}
},
"DomainDataV1": {
"type": "array",
"items": {
"type": "string"
}
},
"DomainDataV2": {
"type": "array",
"items": {
"type": "object",
"properties": {
"valid-values": {
"type": "string"
},
"invalid-values": {
"type": "string"
},
"isinteger": {
"type": "string"
},
"nullable": {
"type": "string"
}
}
}
}
}
}
DomainDataV1 and DomainDataV2 are the two kind of validation I regarded. When additionalProperties is set to false, any additional property is prohibited. I hope it helps! ;)
Upvotes: 1
Reputation: 635
The first hint here is that the error is a SchemaError
, which means there's a problem with how you've written your schema, the validator hasn't even gotten around to looking at the instance value yet.
The second hint is the error message lists the value of the "domain-data" property in your schema, then says ... is not of type 'object', 'boolean'
. Whereas you specified an array, JSON Schema only allows schemas as values in a "properties" keyword — which requires an object (or a boolean).
The solution is to fix the "domain-data" property in your schema, to be a valid subschema.
If you intend for the property to validate arrays where all the items follow the same schema, use:
{ "type": "array", "items": {...} }
If you want the array to be a tuple, where different positions in the array take different schemas, use:
{ "type": "array", "items": [ {first}, {second}, ... ] }
Consult the JSON Schema documentation on the difference between "items" and "additionalItems" if you do this.
Upvotes: 0