2

I'm trying to use if/then/else to use three different schemas for an object, but based on the value of a JSON field outside of that object. I've only seen example of people making their conditionals based on fields within the same object.

Here's what I've tried:

schema: {

   'MyOtherObject': {
          '$id': '#/properties/MyOtherObject',
          'type': 'object',
          'additionalProperties': false,
          'title': 'The MyOtherObject Schema',
          'required': [
            'PaymentMethod'
          ],
          'properties': {
            'PaymentMethod': {
              '$id': '#/properties/Deal/properties/PaymentMethod',
              'type': 'string',
              'title': 'The PaymentMethod Schema',
              'default': '',
              'examples': [
                'Cash'
              ],
              'pattern': '^(.*)$'
            },
    ...
},

   'MyConditionalSchemaObject': {
          '$id': '#/properties/MyConditionalSchemaObject',
          'type': 'object',
          'additionalProperties': false,
          'title': 'The MyConditionalSchemaObject Schema',

          'if': {
            // Trying to get at the value above but don't know how... my schema validations still failing
            '2/MyOtherObject/properties/PaymentMethod' : { 'const': 'Cash' }
          },
          'then': {
            'required': [
              'PaymentStartDate'
            ],
            'properties': {
                'BankName': {
                  '$id': '#/properties/Financing/properties/BankName',
                  'type': ['string', 'null'],
                  'title': 'The Bankname Schema',
                  'default': '',
                  'examples': [
                    'Capital One Auto Finance'
                  ],

   ...

   }
}

I would expect that this code would check the value of the PaymentMethod field in the MyOtherObject json object and then use the schema based on passing the conditional check (if it equals Cash), but I'm still getting schema validation errors saying, Should not be additional properties 'BankName', implying that the schema in the "then" block is not being used for validation.

Davigor
  • 373
  • 3
  • 15
  • 1
    Possible duplicate of [jsonSchema attribute conditionally required depends on parent object](https://stackoverflow.com/questions/51203606/jsonschema-attribute-conditionally-required-depends-on-parent-object) – Jason Desrosiers Jan 09 '19 at 05:30
  • @JasonDesrosiers I honestly can't make sense of the syntax from that answer. I'm trying to do this in the most readable, literal way using if-then-else. – Davigor Jan 09 '19 at 14:25
  • Did you also look at the answer linked in that question (https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required)? It fully describes the patterns in use. I suggest you read and understand that first. Then, go back and see if that answer makes sense. – Jason Desrosiers Jan 09 '19 at 16:08
  • Possible duplicate of [How do I use the \`If\` \`then\` \`else\` condition in json schema?](https://stackoverflow.com/questions/51539586/how-do-i-use-the-if-then-else-condition-in-json-schema) – Relequestual Mar 07 '19 at 08:45
  • @JasonDesrosiers I also need something like this, as I am validating a postgres JSONB column, and I would like the json schema for that column to be able to refer to the values of other (non-JSONB) columns in the if-conditions, in order to conditionally validate the JSONB column. I need a separate schema for each JSONB column that I am validating, rather than a single schema that validates the entire row. – Hans Brende Jul 10 '20 at 15:19
  • @HansBrende This is not possible. JSON Schema is designed to operate on one JSON instance at a time. There's no way to reference another instance from your schema. You can reference another schema, but not an instance. – Jason Desrosiers Jul 10 '20 at 20:46
  • @JasonDesrosiers I'm not saying it has to reference a _separate_ instance. It would suffice to reference the _same_ instance, but one level up, in the parent object. E.g. (rough pseudocode), {"if": {"properties": {reference_to_parent_object/"task_partition": {"const": "partitionA"}}}, "then": schema_for_this_object} – Hans Brende Jul 10 '20 at 20:56
  • JSON Schema keywords can never access data up the hierarchy, only down. You have to move your `if`/`then` up to a level where it can access both values. – Jason Desrosiers Jul 10 '20 at 22:04
  • In other words, you can't have a schema per column and have conditionals that access multiple columns. The only way to access multiple columns is with a row-level schema. – Jason Desrosiers Jul 10 '20 at 22:24
  • @JasonDesrosiers thanks for the feedback. Do you know if this this a philosophical limitation or is it simply that not enough people have made a feature request to go "up the hierarchy"? If nobody has yet, I've created a feature request for this [here](https://github.com/json-schema-org/json-schema-spec/issues/957#issuecomment-656814411). – Hans Brende Jul 10 '20 at 23:18

0 Answers0