streetlight
streetlight

Reputation: 5968

Setting an Attribute as a "string" or null in JSON Schema with a maxLength

I'm trying to create a JSON schema that allows for a nullable attribute. For example, I want the following JSON to be valid:

{
 "some_name" : null
}

with the following schema:

{
  "type": "object",
  "properties": {
    "some_name": {
      "type": [
        "string",
        null
      ],
      "maxLength": 100
    }
  }
}

However, it's invalid as it thinks that "null" can't have a maxLength. Is there a good way to do this? I wish there was a "nullable" attribute, or something of the sort!

Upvotes: 2

Views: 831

Answers (1)

streetlight
streetlight

Reputation: 5968

It seems like making the type "null" instead of null, in my SCHEMA does the trick. Null is it's own schema-type, and seems to trigger better validation.

{
  "type": "object",
  "properties": {
    "some_name": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 100
    }
  }
}

Upvotes: 1

Related Questions