Skip to content

Validating a json based on the input values #229

Answered by gregsdennis
sturaw asked this question in Q&A
Discussion options

You must be logged in to vote

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.

{
  ...
  "contains": {
    "type": "object",
    "properties": {
      "name": { "const": "USA" }
    },
    "required": [ "name" ]
  }
  ...
}

If you've already required that all of the items are objects with name and value, you can leave out the type and required from co…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@handrews
Comment options

Answer selected by handrews
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants