Home > Blockchain >  JSON Schema: using property value defined in another object
JSON Schema: using property value defined in another object

Time:05-18

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 ?

CodePudding user response:

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

CodePudding user response:

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.

  • Related