I would like to declare and define an array of my own objects but I am having problems validating the instance the way I expect.
My $ref points to an object in the same schema (as specified here you can only point to a schema; Can JSON integer attributes be referenced?)
I have followed this link for guidance; https://spacetelescope.github.io/understanding-json-schema/structuring.html
I am validating here; http://json-schema-validator.herokuapp.com/
I would like to make the schema stricter by not allowing further elements of other kinds in the feeder_tx array but can't seem to get the syntax right.
This is the schema:
{
"id": "params-schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "my schema",
"definitions": {
"tx": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"max_channels": {
"type": "integer",
"minimum" : 0,
"maximum" : 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": ["max_channels"]
}
},
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"info": {
"$ref": "#/definitions/tx",
"additionalProperties": false
}
}
}
}
},
"required": ["feeder_tx"]
}
This is the instance:
{
"feeder_tx": [
{"lala": 25, "max_channels": 1499}
]
}
Which I would like to fail because of the addition of lala but instead it validates successfully.
If I add an "additionalProperties": false at the end of the "properties" section of items the validator complains about both "lala" and "max_channels".
This makes sense because the next level is "info"
[ {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/feeder_tx/items"
},
"instance" : {
"pointer" : "/feeder_tx/0"
},
"domain" : "validation",
"keyword" : "additionalProperties",
"message" : "object instance has properties which are not allowed by the schema: [\"lala\",\"max_channels\"]",
"unwanted" : [ "lala", "max_channels" ]
} ]
If I try to make the instance refer to "info" the following error occurs:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
for this instance data:
{
"feeder_tx": [
{"info": { "max_channels": 1499}}
]
}
In fact, I do not see why I need to have a feeder_tx/items/info object as the thing I am $refing is an object.
So I revert the instance data back and remove this object. The following error occurs:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
I.e the schema and instance becomes this:
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"$ref": "#/definitions/tx"
}
}
},
Can someone please explain what is happening here, and the correct way to do it?
Restructuring the schema to not use $ref can solve the problem but I would like to know how to do it with references.
{
"id": "params-schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "parameters schema",
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"max_channels": {
"type": "integer",
"minimum" : 0,
"maximum" : 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": ["max_channels"]
}
}
},
"required": ["feeder_tx"]
}
gives the correct validation error:
[ {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/feeder_tx/items"
},
"instance" : {
"pointer" : "/feeder_tx/0"
},
"domain" : "validation",
"keyword" : "additionalProperties",
"message" : "object instance has properties which are not allowed by the schema: [\"lala\"]",
"unwanted" : [ "lala" ]
} ]
Thanks.
This works:
{
"id": "#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "parameters schema",
"definitions": {
"item": {
"type": "object",
"properties": {
"comment": {
"type": "string"
},
"max_channels": {
"type": "integer",
"minimum": 0,
"maximum": 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": [
"max_channels"
]
}
},
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"$ref": "#/definitions/item"
}
}
},
"required": [
"feeder_tx"
]
}
Tried using:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With