skip to Main Content

I have this schema that is used by jsonforms.

const schema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    other_ubos: {
      type: 'boolean',
    },
    persons: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
      },
    },
  },
  required: ['other_ubos'],
  if: {
    properties: {
      other_ubos: {
        const: true,
      },
    },
  },
  then: {
    required: ['persons'],
  },
};

I want to set a condition where if other_ubos is true each array item should have name``function and email required.

so basically something like this

const schema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    other_ubos: {
      type: 'boolean',
    },
    persons: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
      },
    },
  },
  required: ['other_ubos'],
  if: {
    properties: {
      other_ubos: {
        const: true,
      },
    },
  },
  then: {
    required: ['persons[number].name'],
  },
};

setting required directly on

{
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
}

won’t have the desired effect since it will validate even if other_ubos is false

2

Answers


  1. Chosen as BEST ANSWER

    What i ended up doing is conditionally setting the whole properties object:

    const schema = {
      $schema: 'http://json-schema.org/draft-07/schema#',
      type: 'object',
      properties: {
        other_ubos: {
          type: 'boolean',
        },
      },
      if: {
        properties: {
          other_ubos: {
            const: true,
          },
        },
      },
      then: {
        properties: {
          other_ubos: {
            type: 'boolean',
          },
          persons: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                name: {
                  type: 'string',
                  pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
                },
                function: {
                  type: 'string',
                  pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
                },
                email: {
                  type: 'string',
                  pattern: '^(.+)@(.+)$',
                },
              },
              required: ['name', 'function', 'email'],
            },
          },
        },
      },
      required: ['other_ubos'],
    };
    

  2. Or an even better solution:
    You can partially repeat yourself within JSON Schema. It doesn’t really matter as the end validation is just a union of everything specified.

    So a JSON Schema might look like this:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "other_ubos": {
          "type": "boolean"
        },
        "persons": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "pattern": "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$"
              },
              "function": {
                "type": "string",
                "pattern": "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$"
              },
              "email": {
                "type": "string",
                "pattern": "^(.+)@(.+)$"
              }
            }
          }
        }
      },
      "required": [
        "other_ubos"
      ],
      "if": {
        "properties": {
          "other_ubos": {
            "const": true
          }
        }
      },
      "then": {
        "required": [
          "persons"
        ],
        "properties": {
          "persons": {
            "items": {
              "required": [
                "name",
                "function",
                "email"
              ]
            }
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search