Combining multiple checks#

Given the sample data set below, we want to check if each book has a at least one tag and if there is no book with the category fantasy.

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95,
        "tags": ["book", "Rees", "reference", "Sayings"]
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99,
        "tags": ["book", "Waugh", "fiction", "Sword"]
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99,
        "tags": ["book", "Melville", "fiction", "Moby"]
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99,
        "tags": []
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

Note

You can use the jsonPath online evaluator to check your jsonPath syntax.

We have to define two checks now:

Check if each book has a at least one tag#

  1. Define a ref that returns the object you are interested in. In this case we want to check if each book has a at least one tag, so we can use $.store.book[*] as ref.

  2. Define a condition that checks if the book has a at least one tag. In this case we can use $.tags.length() > 0 as condition.

Check if there is no book with the category fantasy#

  1. Define a ref that returns the object you are interested in. In this case we want to check if there is no book with the category fantasy, so we can use $.store.book[*] as ref.

  2. Define a condition that checks if the book has the category fantasy. In this case we can use !($[*].category).includes('fantasy') as condition.

Combining the checks#

  1. Define a condition that references and combines the two checks. In this case we could use has_at_least_one_tag && no_fantasy_book as condition.

Your config file then looks like this:

checks:
  - name: has_at_least_one_tag
    ref: $.store.book[*]
    condition: none(ref, "($.tags).length === 0")
  - name: no_fantasy_book
    ref: $.store.book[*]
    condition: (!($[*].category).includes('fantasy'))
concatenation:
  condition: has_at_least_one_tag && no_fantasy_book

For the example data set above, the result of the has_at_least_one_tag check would be RED and the result of the no_fantasy_book check would be GREEN. The result of the concatenation check would be RED because the && operator requires both checks to be GREEN to return GREEN.

Note

You can also use the || operator to combine checks. This operator requires at least one check to be GREEN to return GREEN.