Number Type Validation with JSON Schema

Validate number and integer types in JSON Schema using minimum, maximum, and multipleOf. Learn the difference between number and integer keywords.

Basic Types

JSON Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "price": {
      "type": "number",
      "minimum": 0
    },
    "quantity": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9999
    },
    "discount": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    }
  },
  "required": ["price", "quantity"]
}

Test Data

{
  "price": 29.99,
  "quantity": 3,
  "discount": 0.15
}

Detailed Explanation

Validating Numbers in JSON Schema

JSON Schema provides two numeric types: number (any valid JSON number, including decimals) and integer (whole numbers only). Both types support range and divisibility constraints.

Number vs. Integer

{ "type": "number" }   // accepts 3, 3.14, -0.5, 1e10
{ "type": "integer" }  // accepts 3, -7, 0 — rejects 3.14

The integer type is a subset of number. A value like 3.0 is accepted as an integer by most validators because its fractional part is zero, but this behavior can vary across implementations.

Range Constraints

  • minimum — the value must be greater than or equal to this number.
  • maximum — the value must be less than or equal to this number.
  • exclusiveMinimum — the value must be strictly greater than this number.
  • exclusiveMaximum — the value must be strictly less than this number.

How the Example Schema Works

The schema models a simple e-commerce line item:

  1. price is a number with minimum: 0 — it accepts 29.99, 0, or 100, but rejects -5.
  2. quantity is an integer between 1 and 9999 — it accepts 3 but rejects 0, 10000, or 2.5.
  3. discount is a number between 0 and 1, representing a percentage — 0.15 means 15% off.

The test data passes all three checks: 29.99 >= 0, 3 is an integer in [1, 9999], and 0.15 is in [0, 1].

Floating-Point Precision

JSON numbers follow IEEE 754 double-precision rules. Extremely large integers (beyond 2^53) or very precise decimals may lose accuracy. If you need exact decimal arithmetic (e.g., financial calculations), consider using string-encoded decimals with a pattern constraint instead.

Combining with multipleOf

The multipleOf keyword constrains a number to be evenly divisible:

{ "type": "number", "multipleOf": 0.01 }

This ensures values have at most two decimal places — useful for currency amounts. See the dedicated number-multiple-of example for a deeper look at this keyword.

Use Case

Use number and integer validation in API schemas for e-commerce carts, financial transactions, measurement inputs, and pagination parameters. Range constraints prevent impossible values like negative prices or zero quantities from reaching your business logic.

Try It — JSON Schema Validator

Open full tool