Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON-schema: validating an integer formatted in a string with min and max values

Tags:

jsonschema

Through a json schema validator (like z-schema), I would like to validate an integer formatted in a string, e.g.:

{
    "myvalue": "45"
}

Currently, the following validation schema is:

{
    "type": "string",
    "pattern": "^[0-9]+$"
}

However, now it would be great to be able to validate a minimum and maximum value, like:

{
    "type": "integer",
    "minimum": 0,
    "maximum": 32
 }

However the above json value "45" is not an integer.

like image 466
Derek Avatar asked Oct 30 '25 01:10

Derek


1 Answers

Without changing the type to integer, the best you can do is use the pattern keyword to enforce the range using a regular expression. Here is an example of a regular expression to match integers from 0..32.

/^[1-2]?[0-9]$|^3[0-2]$/
like image 74
Jason Desrosiers Avatar answered Nov 03 '25 00:11

Jason Desrosiers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!