Reputation: 8364
I have a schema definition that is not doing a proper validation. Basically, it doesn't inspect anything inside the array and accepts any properties/values in it. I'm new to JSON validation, so I might be missing something.
This is the schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "JSON Validator",
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "object",
"properties": {
"hash": {
"type": "string"
},
"date": {
"type": "string"
},
"uuid": {
"type": "string"
},
"task": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"order": {
"type": "integer"
},
"step": {
"type": "integer"
}
}
},
"meta": {
"type": "string"
},
"additionalProperties": false,
}
},
"required": [
"hash"
]
}
}
A test JSON would be this:
{
"task_N": [
{
"uuid": "asdfsdafa",
"author": {
"id": 1,
"email": "asdfasdd",
"name": "dfasd"
},
"ip": "245245",
"message": "asdfasd",
"step": "",
"is_archived": false,
"creation_date": "34332423",
"related_field": ""
},
{
"uuid": "asdfsdafa",
"author": {
"id": 1,
"email": "asdfasdd",
"name": "dfasd"
},
"ip": "245245",
"message": "asdfasd",
"step": "",
"is_archived": false,
"creation_date": "34332423",
"related_field": ""
}
]
}
As you can see, there is no single match in the array properties, yet both the python library jsonschema and http://jsonschemavalidator.net give the the JSON as valid against the schema. I've been scratching my head for a few hours, does anyone have a clue?
Upvotes: 0
Views: 2149
Reputation: 8364
Found the solution. It seems that I need to set not only the array properties to false, but the parent as well. Also, I needed to set it up as "patternProperties" with the proper regex. The finished schema is this:
{
'$schema': 'http://json-schema.org/draft-07/schema#',
'$id': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'patternProperties': {
'^[a-zA-Z0-9]*$': {
'type': 'array',
'items': {
'type': 'object',
'required': [
'hash',
'date',
'uuid',
'task',
'author'
],
'properties': {
'hash': {
'type': 'string'
},
'date': {
'type': 'string'
},
'uuid': {
'type': 'string'
},
'task': {
'type': 'object',
'properties': {
'name': {
'type': 'string'
},
'order': {
'type': 'integer'
},
'step': {
'type': 'integer'
}
}
},
'author': {
'type': 'object',
'properties': {
'id': {
'type': 'integer'
},
'name': {
'type': 'string'
},
'email': {
'type': 'string'
}
}
},
'meta': {
'type': 'string'
}
},
'additionalProperties': false
}
}
},
'additionalProperties': false
}
Upvotes: 0
Reputation: 118
This is happening for a few reasons, JSON schema requires your whole example to match the schema so you would need to have "task_N" as part of the schema.
Also, your schema is defined in additionalProperties
, it should be properties
.
Try this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "JSON Validator",
"type": "object",
"properties": {
"task_N": {
"type": "array",
"items": {
"type": "object",
"required": [
"hash"
],
"properties": {
"hash": {
"type": "string"
},
"date": {
"type": "string"
},
"uuid": {
"type": "string"
},
"task": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"order": {
"type": "integer"
},
"step": {
"type": "integer"
}
}
},
"meta": {
"type": "string"
},
"additionalProperties": false
}
}
}
}
}
Upvotes: 1