1

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": "test@email.om",
          "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

rAFTA
  • 35
  • 5
  • 1
    There are at least 3 issues with your schema here. It's OK, reading spec-ise is hard, and we're here to help. I can't igve you a full answer right now, but I can point you to the issues. First, the value of `if` must be a JSON Schema. `then` will only be used IF the `if` schema is valid. Second, the schema which is the value of `then` doesn't actually do anything, as a definition on it's own imposes no validation. The `then` schema is not ADDED to the root schema, as it looks like you assumed, but it is APPLIED to the data instance as a schema all by itself. Make sense? – Relequestual Oct 25 '18 at 07:49
  • Have a look at these examples of how to use `if/then/else` https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#ifthenelse – Relequestual Oct 25 '18 at 07:50

1 Answers1

1

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": "test@email.om",
          "description": "no format"
        }
      }
    },
    "thetoggler-is-testme": {
      "properties": {
        "theToggler": { "const": "testme" }
      }
    },
    "mysession-id-is-an-email": {
      "properties": {
        "mysession": {
          "properties": {
            "theID": { "format": "email" }
          }
        }
      }
    }
  }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53