问题
I am designing the the json scheme. and I am facing some issues while designing the schema.
Here is the problem.
I have an array of group objects. and I want this array should contain unique group objects. I want to make them unique based on object id ( ex group.id)
The groups array is not unique if (groups[0].id == groups[1].id)
, I want to make unique only based on group id, Below is my Json structure.
"groups": {
"type": "array",
"items": {"$ref": "#/group"},
"uniqueItems":true
},
"group": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"type": {
"type": "string",
"enum": [
"a",
"b"
]
},
"command": {
"type": "string",
"enum": [
"add",
"modify"
]
}
}
},
回答1:
Well, there is no magic bullet here. Remind Json-Schema is intended for defining structure of Json Data (not values).
One option would be not consider your groups node an "array" but instead an "object", and use additionalProperties to express that all additional properties should contain "type" and "command" properties.
Then you would use the name of each property in groups as id, so it would be unique.
The problem with this approach is that you do not restrict this id to be numeric (It might not be acceptable in your context). Even you could use patternProperties to match the "type,command" schema just to numeric "id's".
来源:https://stackoverflow.com/questions/16475427/need-help-json-schema-design