2

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

1 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

enum validates right, const somehow doesn't ("const":2 validates my "test":1 right)
const is seriously brand new (a couple weeks). I only know of one validator (AJV) that supports it. Yours probably doesn't yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.