Replies: 2 comments
-
JSON Schema doesn't provide a way to reference data like this out of the box. You can do it with my Data vocab extension, though. AJV has their own ( To do this with off-the-shelf JSON Schema, you just need to approach the problem differently. It can be solved by expanding it out a bit. The approach you need to take is: {
"type": "object",
"anyOf": [
{
"properties": {
"prop1": { "enum": [ "val1", "val2" ] },
"prop2": { "const": "" }
}
},
{
"properties": {
"prop1": { "const": "" },
"prop2": { "enum": [ "value1", "value2" ] }
}
}
]
} As you add more data dependencies, this will grow, but it can usually be expanded like this. If you find that you have a situation where you need to validate something like "prop1 mod prop2 = 0", then you'll need the data vocab. |
Beta Was this translation helpful? Give feedback.
-
Expressing that only one of two properties is present is fairly easy, {
"type": "object",
"properties": {
"property1": { "enum": ["val1", "va2"],
"proprety2": { "enum": ["value1", "value2"]
},
"oneOf": [
{ "required": ["property1"] },
{ "required": ["property2"] }
]
} However, you seem to be using the empty string ( {
"type": "object",
"properties": {
"property1": { "enum": ["val1", "va2"] },
"proprety2": { "enum": ["value1", "value2"] }
},
"oneOf": [
{
"required": ["property1"],
"property2": { "const": "" }
},
{
"required": ["property2"],
"property1": { "const": "" }
}
]
} |
Beta Was this translation helpful? Give feedback.
-
I have a specific requirement in schema that with respect to 2 properties
schema: {
property1: {type: string, enum: ["val1", "val2", ""]}
property2: {type: string, enum: ["value1", "value2", ""]}
...//other properties
}
I wish to accomplish a state of validation such that only either of property1, property2 exists and at the same when one contains value other must be empty.
But I think keeping a "" as enum is not an ethical check.
Beta Was this translation helpful? Give feedback.
All reactions