yamljsonschemajson-schema-validatorpython-jsonschema

Import variable as dependency from the main YAML schema to a sub-schema


I have a YAML schema splitted in two files. Like this:

main.yaml

type: object
properties:
  a:
    description: variable a
    type: string
  b:
    description: variable b
    $ref: support.yaml

and the support.yaml

type: object
properties:
  c:
    description: variable c
    type: string
  d:
    description: variable d
    type: string

I have the following situation: the variable a in main.yaml is required when the variable d in support.yaml is present. I know there is the keyword dependencies in jsonschema but I am not very familiar with the syntax to referencing things in schema or if this is possible.

Do you know if this is possible or any documentation that could help?


Solution

  • depending on the version you are using, you can do something like this:

    Using draft-07

    src: https://tour.json-schema.org/content/05-Conditional-Validation/04-if-then-else

    # main.yaml
    $schema: http://json-schema.org/draft-07/schema#
    $id: https://example.org/schema/main.yaml
    type: object
    properties:
      a:
        description: variable a
        type: string
      b:
        description: variable b
        $ref: 'support.yaml#'
    allOf:
      - if:
          required:
            - b
          properties:
            b:
              properties:
                d: true
        then:
          required:
          - a
    
    
    
    # support.yaml
    $schema: 'http://json-schema.org/draft-07/schema#'
    $id: 'https://example.org/schema/support.yaml'
    type: object
    properties:
      c:
        description: variable c
        type: string
      d:
        description: variable d
        type: string