I need to express an array of different objects in a schema. The array, called contents
, can contain any number of elements, but they must be one of two types of object : One type of object represents a piece of text, the other type of object represents an image.
So far, I've not been able to find a way to enforce the validation correctly. It seems (nested) required
inside a oneOf
doesn't work, so I tried using definitions
but that doesn't seem to fit.
I've tried a few online validators, but they seem happy for me to add illegal values in item-2
value
object. Its the value
property that seems to be the biggest problem. Unfortunately, due to legacy issues, I'm stuck with this being an object in an array.
Is it possible to validate and enforce correct type/requirements for this object?
(this is the data, not a schema. Unfortunately, we also used the keyword type
when we design the original json layout!)
{
"uuid":"780aa509-6b40-4cfe-9620-74a9659bfd59",
"contents":
[
{
"name":"item-1",
"label":"My editable text Label",
"value":"This text is editable",
"type":"text"
},
{
"name":"item-2",
"label":"My editable image label",
"index":0,
"type":"image",
"value":
[
{
"name":"1542293213356.png",
"rect":[0,0,286,286]
}
]
}
],
"version":"2.0"
}
Well, I think this is it, though the online validator doesn't seem 100% reliable. Editing the values isn't always invalidating the object.
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"uuid": { "type": "string" },
"version": { "type": "string" },
"contents": {
"$ref": "#/definitions/contents"
},
},
"required": ["uuid", "version"],
"definitions": {
"image": {
"type": "object",
"properties": {
"name": { "type": "string" },
"label": { "type": "string" },
"type": { "enum": ["image", "text"] },
"value": { "type": "object" }
},
"required": ["name", "label", "type", "value"]
},
"text": {
"type": "object",
"properties": {
"name": { "type": "string" },
"label": { "type": "string" },
"type": { "enum": ["image", "text"] },
"value": { "type": "string" }
},
"required": ["name", "label", "type", "value"]
},
"contents": {
"type": "array",
"contains": {
"oneOf": [
{ "$ref": "#/definitions/image" },
{ "$ref": "#/definitions/text" }
]
},
},
},
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.