Reputation: 13
I currently have a JsonSchema which has an enum field with 12 values, and lets say two other regular string fields in the same object, fields X and Y. If the value for the enum field is 1 of 3 out of the 12 enum values allowed, then I want to make X required, else if it's any of the other 9 enum values, then Y is required.
For example:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"efield": {
"type": "string",
"enum": ["n1", "n2", "...", "n12"]
},
"x": {
"type": "string"
},
"y": {
"type": "string"
}
},
"if": {
"properties": { "efield": { "in": ["n1", "n2", "n3"] } }
},
"then": {
"required": ["x"]
},
"else": {
"required": ["y"]
}
}
This is what it would look like idealy, though I know that the "in" keyword doesn't exist, closest I can figure at the moment is just using "allOf" and listing out 12 if statements for each enum value and which field is required based on each value, which feels kind of over the top / inelegant.
I also looked at "oneOf" and having two sub-schemas, one where 'efield' had the first 3 enums and x was required, and the other where efield had the other 9 enums and y required, but validation would complain about the enums, e.g. if I tried using an enum value from the second schema of 9 enums, it would complain that the value isn't in the first schema with the 3 values...
Are there any keywords that I'm missing, or any elegant ways of performing this use case? Thanks.
Upvotes: 1
Views: 7613
Reputation: 1817
Just use enum
instead of your in
keyword and it should be totally fine:
Upvotes: 1