rAFTA
rAFTA

Reputation: 35

jsonschema validation with conditional format

I have a schema that I want to add a "format" keyword to, but only on certain cases. I am having jsconschema draft 07 and I'm trying to use if/else statements, however, I think I'm starting to realize that you can't add formatting when using if/else keywords.

Here is my schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://json-schema.org/draft-07/schema#",
  "title": "Core schema meta-schema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "theToggler": {
      "type": "string"
    },
    "mysession": {
      "$ref": "#/definitions/mysession"
    }
  },
  "definitions": {
    "mysession": {
      "type": "object",
      "properties": {
        "theID": {
          "type": "string",
          "example": "[email protected]",
          "description": "no format"
        }
      }
    }
  },
  "if": {
    "theToggler": "testme"
  },
  "then": {
    "definitions": {
      "mysession": {
        "type": "object",
        "properties": {
          "theID": {
            "type": "string",
            "format": "email"
          }
        }
      }
    }
  }
}

and here is my input:

{
  "theToggler": "testme",
  "mysession": {
    "theID": "test"
  }
}

You would think that this throws an arrow (if 'theToggler' = "testme" then theID should have an @ sign because I'm defining the "email" format. Am I doing something wrong, or is this not supported, or do you see anything else that I could be missing?

Thanks!

P.S. I am testing it in https://www.jsonschemavalidator.net

Upvotes: 1

Views: 493

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24399

You have a number of issues.

First of all, it looks like you've used the meta schema as an example. That's fine, but you can't reuse the meta schema's $id. Your schema needs to have a unique $id or none at all.

The if and then keywords must be schemas. If the instance is valid against the if schema, then the then schema must also be valid.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "theToggler": { "type": "string" },
    "mysession": { "$ref": "#/definitions/mysession" }
  },
  "allOf": [
    {
      "if": { "$ref": "#/definitions/thetoggler-is-testme" },
      "then": { "$ref": "#/definitions/mysession-id-is-an-email" }
    }
  ],
  "definitions": {
    "mysession": {
      "type": "object",
      "properties": {
        "theID": {
          "type": "string",
          "example": "[email protected]",
          "description": "no format"
        }
      }
    },
    "thetoggler-is-testme": {
      "properties": {
        "theToggler": { "const": "testme" }
      }
    },
    "mysession-id-is-an-email": {
      "properties": {
        "mysession": {
          "properties": {
            "theID": { "format": "email" }
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions