Validating a json based on the input values #229
-
Hi, I'm someone new to json schema and while going to the docs I've found that it has a lot of functionality, most of which are based on the keys in the json. I have a very specific requirement and was wondering if that would be possible to do directly through JSON schema (found an alternative where i would have to change the structure but I'd like to avoid that extra overhead.)
My requirement is that
Would appreciate any help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This can be done fairly easily if you're using a draft 7 or later. What you want is going to be a combination of this StackOverflow answer and the For (1)You want at least one item to have
If you've already required that all of the items are objects with For (2)You want a conditional that if the item with First, for the conditional, you want to use
So for
And for the
Putting all of this together, you get:
|
Beta Was this translation helpful? Give feedback.
This can be done fairly easily if you're using a draft 7 or later.
What you want is going to be a combination of this StackOverflow answer and the
contains
keyword.For (1)
You want at least one item to have
name: USA
.contains
alone solves this for you.contains
defines a schema, and if the value is an array, it must contain at least one item that matches that schema. It doesn't matter where in the array the item is.If you've already required that all of the items are objects with
name
andvalue
, you can leave out thetype
andrequired
from co…