1

By the below example question will get more clear:

data = {"type": "object", "properties": {"product": {"type": ["boolean","string"]}}}

It includes both boolean and string types. It works, but I want to limit the string part to a list of enums:

["off", "on", "semi-on"]

When type is not a list it works, but when I make it as a list then I cannot provide the enum for the string type.

The below example without boolean works too:

data = {"type": "object", "properties": {"product": {"type": "string", "enum": ["off", "on", "semi-on"]}}}

Should split the schema, or is there another way for this?


EDIT-1: The real schema is as below:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "is_trusted": {"type": "boolean"},
        "is_active": {"type": "boolean"},
        "addresses": {"type": "array"},
        "attachments": {
            "type": "object",
            "properties": {
                "send_web_push": {"type": "boolean"},
                "receive_web_push": {"type": "boolean"}
            },
            "required": ["send_web_push", "receive_web_push"],
            "additionalProperties": false
        }
    },
    "required": ["is_trusted", "is_active", "addresses"],
    "additionalProperties": false
}
Relequestual
  • 11,631
  • 6
  • 47
  • 83
Alireza
  • 6,497
  • 13
  • 59
  • 132
  • The way you've presented looks reasonable. I'm going to need some more information to help though. Can you let me know what version of JSON Schema you are using please (The draft number)? Thanks. – Relequestual Oct 29 '18 at 10:51
  • Also please provide the full JSON instance you want to validate with your schema. This will help me make sure there isn't some other problem which means your schema is passing validation when you don't believe it should. – Relequestual Oct 29 '18 at 10:51
  • @Relequestual Thank you for your comment, please have a look at the `EDIT-1` section. – Alireza Oct 29 '18 at 11:32
  • I can't see `enum` in your schema. I'm also going to assume you are using draft 7, and I will now answer your question. – Relequestual Oct 29 '18 at 12:08
  • @Relequestual well the question(problem) is that I cannot use enum when multiple types are set. I couldn't handle it using `jsonschema`. – Alireza Oct 29 '18 at 12:16
  • Right, you were using it incorrectly, but I would still have liked to have seen where you thought it should go, so I could show you the fixed version, with a valid instance. – Relequestual Oct 29 '18 at 12:21
  • 1
    Feel free to join the JSON Schema slack should you have any issues which don't fit into a SO question. "Discussion" link on the JSON Schema site. – Relequestual Oct 29 '18 at 12:41

1 Answers1

3

JSON Schema defines validation keywords sectioned by how they will apply to an instance.

enum is under the Validation Keywords for Any Instance Type section.

This means it is applicable to any instance type, including booleans.

As such, you must include the boolean values true and false in your enum of valid values. (This does make type redundant in this example).

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "product": {
      "type": [
        "boolean",
        "string"
      ],
      "enum": [
        "off",
        "on",
        "semi-on",
        true,
        false
      ]
    }
  }
}

The below instance is now valid for the above schema. In your first schema, product having a value of true would fail enum validation because true is not included in the enum.

{
  "product": true
}
Community
  • 1
  • 1
Relequestual
  • 11,631
  • 6
  • 47
  • 83