Reputation: 43
I've been reading up on how to define json schema to hold a list of different shaped objects: person, address, vehicle. Part of the research led me to posts where helpful additions were suggested. e.g. adding "uniqueitems": true so that the list doesn't contain duplicates.
I've found the site http://www.jsonschemavalidator.net/ extremely useful in validating my schema and json data but I can't figure out what I'm doing wrong with both
"additionalProperties": false
and
"uniqueitems": true
Here is my sample schema and data:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "an array of Insurable Items",
"type": "array",
"items": {
"type": "object",
"OneOf": [
{
"type": "object",
"description": "A Person",
"properties": {
"person": {
"type": "object",
"$ref": "#/definitions/person"
}
},
"required": [
"person"
],
"additionalProperties": false
},
{
"type": "object",
"description": "An Address",
"properties": {
"address": {
"type": "object",
"$ref": "#/definitions/address"
}
},
"required": [
"address"
],
"additionalProperties": false
},
{
"type": "object",
"description": "A Vehicle",
"properties": {
"vehicle": {
"type": "object",
"$ref": "#/definitions/vehicle"
}
},
"required": [
"vehicle"
],
"additionalProperties": false
}
],
"uniqueitems": true
},
"definitions": {
"person": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"title": {
"type": "string"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"dateofbirth": {
"type": "string"
},
"employmentstatus": {
"type": "string"
},
"occupation": {
"type": "string"
}
},
"additionalProperties": false
},
"address": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"line1": {
"type": "string"
},
"line2": {
"type": "string"
},
"line3": {
"type": "string"
},
"line4": {
"type": "string"
},
"line5": {
"type": "string"
},
"postcode": {
"type": "string"
}
},
"additionalProperties": false
},
"vehicle": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"vehiclecode": {
"type": "string"
},
"registrationyear": {
"type": "string"
},
"registrationletter": {
"type": "string"
},
"registrationnumber": {
"type": "string"
},
"description": {
"type": "string"
}
},
"additionalProperties": false
}
}
}
Data:
[
{
"person": {
"id": "123456789-01",
"title": "Mr",
"firstname": "Joe",
"lastname": "blogs"
},
"badadditional": "thing"
},
{
"address": {
"id": "123456789-A",
"line1": "1 The Mall",
"line2": "Westminster",
"line3": "London",
"postcode": "SW1A 1AA"
}
},
{
"address": {
"id": "123456789-A",
"line1": "1 The Mall",
"line2": "Westminster",
"line3": "London",
"postcode": "SW1A 1AA"
}
},
{
"vehicle": {
"id": "123456789-01-01",
"vehiclecode": "string",
"registrationyear": "string",
"registrationletter": "string",
"registrationnumber": "string",
"description": "string",
"badadditional": "other thing"
}
}
]
If you copy these into the http://www.jsonschemavalidator.net/ schema validator, it reports no problem despite the presence of
"badadditional": "thing"
and
"badadditional": "other thing"
and the duplicate address object.
I've been searching stackoverflow [tagged with json.net], reading (amongst others), http://grokbase.com, http://json-schema.org and I'm working to draft-04.
I've also tried https://jsonschemalint.com/#/version/draft-04/markup/json but that also doesn't complain either.
Can anyone point me towards other validators, json schema documentation and examples or let me know what I'm doing wrong if it's obvious?
Upvotes: 0
Views: 138
Reputation: 24489
additionalProperties
works just fine. The problem is that you have used OneOf
instead of oneOf
. JSON Schema keywords are case sensitive, so the validator didn't recognize this keyword and ignored it and everything it defined.
You have two problems with uniqueItems
. The first is another case sensitivity issue. You have used uniqueitems
. The other problem is that it is in the wrong place. It is part of the items
schema (which is an object) when it should be part of the root schema (which is an array).
So, fix your capitalization and move uniqueItems
up one level and it should all work as expected.
Upvotes: 2