0

I want to set up the conditional validation in my schema. I saw an example here on SO. I have a similar setup, where I would like to validate if the field public is set to string "public". If it is set to "public" then I want to make fields description, attachmentUrl and tags required. If the field is not set to "public" then this fields are not required.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Update todo",
  "type": "object",
  "properties": {
    "public": {
      "type": "string"
    },
    "description": {
      "type": "string",
      "minLength": 3
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true,
      "minItems": 1
    },
    "attachmentUrl": {
      "type": "string"
    }
  },
  "anyOf": [
    {
      "not": {
        "properties": {
          "public": { "const": "public" }
        },
        "required": ["public"]
      }
    },
    { "required": ["description", "tags", "attachmentUrl"] }
  ],
  "additionalProperties": false
}

But, when I try to deploy it like that, I get the following error:

Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified. Unsupported keyword(s): ["const"]]

Leff
  • 1,968
  • 24
  • 97
  • 201

1 Answers1

2

The "const" keyword wasn't added until draft 06. You should upgrade to an implementation that supports at least that version.

https://json-schema.org/draft-06/json-schema-release-notes.html#additions-and-backwards-compatible-changes

Otherwise, you can use "enum" with a single value: "enum": ["public"]

Ether
  • 53,118
  • 13
  • 86
  • 159