Dominik Lemberger
Dominik Lemberger

Reputation: 2426

Json Schema pattern for integer values

Is there a way to verify the pattern of an integer value?

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "additionalProperties": false,
    "definitions": {},
    "id": "http://example.com/example.json",
    "properties": {
        "test": {
            "type": "integer",
            "pattern":"1343"
        }
    },
    "type": "object"
}

Just a little test JSON

{
  "test": 1
}

This always validates true with http://www.jsonschemavalidator.net/

I know that I can make a small workaround by using "minimum":1, "maximum":1 but this looks kind of strange and needs 2 lines for 1 validation.

Is there a way to check like on strings with "pattern" or anything else? Regex ^1$ doesn't work either

Upvotes: 2

Views: 4242

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24399

The pattern keyword only applies to strings. The best way to constrain a number to a specific value is to use enum or the new const keyword.

{
  "enum": [1343]
}

-

{
  "const": 1343
}

Upvotes: 4

Related Questions