Reputation: 257
I have the following schema and I'm having a hard time trying to understand why it is not working.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://yourdomain.com/schemas/myschema.json",
"title": "Product",
"description": "A product in the catalog",
"type": "object",
"properties": {
"transitions" : {
"description": "defines the transitions to another state",
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"description": "defines a transition for an specific node",
"type":"object",
"properties": {
"next": {
"description": "id of the next transition",
"oneOf": [
{
"type":"string"
},
{
"type": "object",
"patternProperties": {
"^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
"type":"string"
}
}
},
{
"type": "object",
"patternProperties": {
"^\\w+( +\\w+)*$" : {
"type":"string"
}
}
}
]
},
"action": {
"description": "id of the transitions action",
"type":"string"
}
}
}
},
"additionalProperties": false
}
},
"required": ["transitions"]
}
For example, as you can see in the schema, this particular json instance should comply with one of next
property definitions
"transitions": {
"1": {
"action": "1_input",
"next": {
"test de una novedad": "2"
}
}
}
}
schema for property
{
"type": "object",
"patternProperties": {
"^\\w+( +\\w+)*$" : {
"type":"string"
}
}
}
But I got these error messages in an online validator https://www.jsonschemavalidator.net/,
Message:
JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 1, 2.
Schema path:
http://yourdomain.com/schemas/myschema.json#/properties/transitions/patternProperties/^[0-9]+$/properties/next/oneOf
Message:
Invalid type. Expected String but got Object.
Schema path:
http://yourdomain.com/schemas/myschema.json#/properties/transitions/patternProperties/^[0-9]+$/properties/next/oneOf/0/type
I don't see how it can be a match with two of the definitions.
Has anyone had a similar situation?
Thanks in advance
Upvotes: 0
Views: 326
Reputation: 1598
Your problem is with the definition of the oneOf
"oneOf": [
{
"type":"string"
},
{
"type": "object",
"patternProperties": {
"^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
"type":"string"
}
}
},
{
"type": "object",
"patternProperties": {
"^\\w+( +\\w+)*$" : {
"type":"string"
}
}
}
]
For "test de una novedad": "2"
both second and third element are objects with no property restriction, so it matches both.
To fix this you could try merging both if you don't mind possibly having an object validated with both properties:
"oneOf": [
{
"type":"string"
},
{
"type": "object",
"patternProperties": {
"^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
"type":"string"
},
"^\\w+( +\\w+)*$" : {
"type":"string"
}
}
}
]
Or you could add additionalProperties: false
in one or both.
"oneOf": [
{
"type":"string"
},
{
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
"type":"string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^\\w+( +\\w+)*$" : {
"type":"string"
}
}
}
]
Upvotes: 1