0

Here is the desired schema and json for illustration purpose. Please see the link below.

JSON Schema and JSON

{
  "id": "123" ,
  "ts": "1234567890",
  "complex_rules":
  [
    {
      "type":"admin",
      "rule":{
       "rights":"all",
       "remarks": "some admin remarks"
       }
    },
    {
      "type":"guest",
      "rights": "limited"
    },
    {
      "type":"anonymous",
      "rights": "blocked"
    }
  ]
}

The 'complex_rules' is an array of json object:

  1. With type either be a : "admin", "guest", "anonymous" and the 'type' attribute is MANDATORY.
  2. Each object in array can have its own structure, but the type can be either of: "admin", "guest", "anonymous" only. No other type attribute is acceptable.

The conditions to evaluate:

  1. The type of object in the array cannot re-occur in the array. (I know this seems to be not possible, so we can ignore this)
  2. If attribute "rights" in the {type=admin object} with any value, then we cannot have "rights": "limited" or any value in {type=guest object}. The JSON Schema validation must complain about this.
  3. Another twist, either object {type":"guest"}or {type":"anonymous"} can exist. Both types cannot coexist along with other types.

----Update The above link is the solution this question.

6
  • The updated answer to your other question covered consideration 2 here. Give it a try =] Commented Sep 30, 2019 at 14:14
  • Consideration 1 is not possible with draft-7 but is possible with draft 2019-09 (which has only just come out and has very few implementations). You may want to provide your schema so far (which includes identifying which version of JSON Schema you are using) Commented Sep 30, 2019 at 14:15
  • I cannot see any answer here in this question? Commented Sep 30, 2019 at 14:52
  • 1
    As in my answer to your other question: stackoverflow.com/a/58136549/89211 - That covers your second requirement. Although in this instance, you've nested rights under rule, so you'd have to make the same change in the schema. Commented Sep 30, 2019 at 15:17
  • Thanks a lot. That worked Commented Sep 30, 2019 at 16:51

1 Answer 1

0

In regards to 1 and 2:

You need to use a combination of if, then, and not keywords to construct the logic you require with the correct level of applicability.

In regards to 3:

The type of object in the array cannot re-occur in the array. (I know this seems to be not possible, so we can ignore this)

Right, that's correct, it's not possible as of draft-7 JSON Schema.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.