0

I have two properties in JSON Schema:
"A": {"type": "object", "properties":{ "X":{"type":"boolean"}}}
"B": {"type": "object", "properties":{...}}

In the logic of B I need to use value of X to set B property values and "required":[...].
How do I use value of X within the B ?

2 Answers 2

2

You need to declare a rule at the schema level that defines the object that contains these two properties. You can then use the dependentSchemas, dependentRequired, or if/then/else keywords to define rules like "if <something> in property A, then <something> in property B".

https://json-schema.org/understanding-json-schema/reference/conditionals.html

For example:

{
  "type": "object",
  "properties": {
    "A": {
      "type": "object",
      "properties":{ "X":{"type":"boolean"}}
    },
    "B": {
      "type": "object",
      "properties":{...}
    }
  },
  "if": {
    "required": ["A"],
    "properties": {
      "A": {
        "required": ["X"],
        "properties": { "X": { "const": true } }
      }
    }
  },
  "then": {
    "required": ["B"],
    "properties": {
      "B": {
        "required": ["Y"],
        "properties": { "Y": { "const": "/A/X is true" } }
      }
    }
  }
}

This is saying "if property A exists, and has a subproperty X which is true, then there must be a property Y under B which is the string "/A/X is true"".

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

2 Comments

Thank your for the response. You need to declare a rule at the schema level that defines the object that contains these two properties. -- I'm not exactly following you here.
I've added an example.
1

Speaking about the standard specification of JSON Schema in general, you can't "use" values from the JSON instance at some other place for validation in the meaning that, for example, you take a value and fill a required keyword with it.

However, you can define conditions using if-then-else that use values to decide what other schemas should be applied subsequently at this point in the validation flow.

The dependentSchemas and dependentRequired keywords are focused on the presence of properties.

More details are available here.

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.