Integer Range Validation with minimum and maximum
Validate integers within specific ranges using minimum and maximum in JSON Schema. Examples for ports, HTTP status codes, and priority levels.
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"port": {
"type": "integer",
"minimum": 1,
"maximum": 65535
},
"httpStatusCode": {
"type": "integer",
"minimum": 100,
"maximum": 599
},
"priority": {
"type": "integer",
"minimum": 0,
"maximum": 10
}
},
"required": ["port"]
}Test Data
{
"port": 8080,
"httpStatusCode": 200,
"priority": 5
}Detailed Explanation
Integer Range Validation
Constraining integers to a valid range is one of the most common validation tasks. JSON Schema's minimum and maximum keywords let you define inclusive bounds for numeric values.
Basic Range Schema
{
"type": "integer",
"minimum": 1,
"maximum": 100
}
This accepts whole numbers from 1 to 100, inclusive on both ends. Values like 0, 101, -5, and 50.5 are all rejected — 0 and 101 are out of range, -5 is below the minimum, and 50.5 is not an integer.
How the Example Schema Works
The schema defines three fields with domain-specific integer ranges:
port— TCP/UDP port numbers range from 1 to 65535. The schema enforces this withminimum: 1(port 0 is reserved) andmaximum: 65535(the highest valid port number for a 16-bit unsigned integer).httpStatusCode— valid HTTP status codes range from 100 (Continue) to 599 (the upper boundary of the 5xx server error class). This prevents invalid codes like99or600from reaching your application logic.priority— a simple 0-10 priority scale where 0 is lowest and 10 is highest. This is a common pattern for task management and job queue systems.
The test data passes: 8080 is in [1, 65535], 200 is in [100, 599], and 5 is in [0, 10].
Inclusive vs. Exclusive Bounds
JSON Schema offers both inclusive and exclusive bounds:
{
"minimum": 0, // >= 0 (inclusive)
"maximum": 100, // <= 100 (inclusive)
"exclusiveMinimum": 0, // > 0 (exclusive)
"exclusiveMaximum": 100 // < 100 (exclusive)
}
You can mix these — for example, exclusiveMinimum: 0 with maximum: 100 accepts values in the range (0, 100].
Draft 4 vs. Modern Drafts
In Draft 4, exclusiveMinimum and exclusiveMaximum were boolean modifiers on minimum/maximum. Starting from Draft 6, they became standalone numeric keywords. Be aware of this if your validator uses an older draft.
Practical Tips
- Port validation: Remember that ports 1-1023 are "well-known" and typically require root/admin privileges. Consider using
minimum: 1024for user-configurable ports. - Pagination: For page numbers, use
minimum: 1(pages are 1-indexed). For page sizes, combine withmultipleOfto enforce round numbers.
Use Case
Use integer range validation for network port configuration, HTTP status code handling, priority queues, pagination parameters, and any API field that represents a bounded whole number. Proper range constraints prevent invalid values from reaching your business logic and causing runtime errors.