Numeric Types
The number type and its family of integer, unsigned, and float shortcuts.
The number type validates a numeric value. It is the base of a small family of predefined shortcuts — int, uint, int8, byte, and so on — where each shortcut is simply number with a fixed set of constraints baked in. For example, int8 is number restricted to whole values in −128…127.
Two related numeric types have their own pages: BigInt (arbitrary-precision integers, suffix
n) and Decimal (fixed-precision decimals, suffixm). For how numbers are written (decimal, hex, octal, binary, scientific,NaN,Inf), see Numeric Values.
The number family
Each name below is the base number type plus preset constraints. A conformant validator MUST recognize all of these names.
number
no
IEEE-754 double
float
no
IEEE-754 double
int
yes
unbounded integer
uint
yes
integer ≥ 0
int8
yes
−128 … 127
uint8 / byte
yes
0 … 255
int16
yes
−32 768 … 32 767
uint16
yes
0 … 65 535
int32
yes
−2 147 483 648 … 2 147 483 647
uint32
yes
0 … 4 294 967 295
byte is an alias for uint8. A value outside a type's own range MUST be rejected with out-of-range-integer — the limit belongs to the type, so the fix is to widen the type. A value that breaks a min/max the schema declared is a different error; see min / max.
age: int8
---
~ 200 # ✗ out-of-range-integer — int8 max is 127Whole-number rule. The
int/uint/int8…32/uint8…32shortcuts MUST reject values with a fractional part (intaccepts42, not42.5).numberandfloataccept any finite double.
Reserved (not yet supported).
int64,uint64,float32, andfloat64are reserved for future use; the reference implementation currently rejects them.
TypeDef
A number MemberDef accepts only the options below. Any other key is invalid.
type
string
Type name (number or any family member). First positional value.
default
number
Value used when the member is omitted. Second positional value.
choices
array of number
Restricts the value to a fixed set. Third positional value.
min
number
Minimum allowed value (inclusive).
max
number
Maximum allowed value (inclusive).
multipleOf
number
The value must be an exact multiple of this.
format
string
Presentation, write-only. Notation used when writing: decimal (default), hex, octal, binary, scientific.
optional
bool
If true, the member may be omitted. Shorthand: ? suffix on the key.
null
bool
If true, the member may be null. Shorthand: * suffix on the key.
Constraints
min / max
Inclusive bounds. A value below min is rejected with mismatched-min, above max with mismatched-max — the code names the keyword that rejected it, so the direction is never in doubt.
These are distinct from out-of-range-integer, which means the value does not fit the type (int8 given 200, with no bound declared). One is fixed by changing the data, the other by widening the type.
An explicit
min/maxreplaces a shortcut's built-in bound rather than narrowing it — e.g.{ int8, min: -200 }currently accepts −200. See Implementation status below.
multipleOf
The value must be an exact multiple of the given number.
choices
Restricts the value to a fixed set. As the third positional value it may omit the key.
format
Controls how the number is written on serialization. It is write-only: it does not restrict which input notations are accepted (any notation is read).
The base prefix is part of the written literal, and a sign precedes it (-0xff). A value with a fractional part has no radix literal, so a radix format does not apply to it and the decimal notation is written instead.
Special values
NaN and Inf/-Inf are valid only for number/float. When combined with a numeric bound, the reference implementation currently coerces them to null rather than validating them — see Implementation status.
Optional, nullable & defaults
How a member resolves (verified behavior):
value present, valid
the value
value present, below min / above max
mismatched-min / mismatched-max error
value present, outside the type's range
out-of-range-integer error
value is N (null), key is nullable (*)
null
value is N (null), key is not nullable
forbidden-null error
value omitted, default set
the default
value omitted, key optional (?), no default
absent
value omitted, required, no default
missing-value error
The suffixes are shorthand for the keyed options — c?*: number and c: { number, optional: T, "null": T } declare the same member. A member name that has to be quoted can only use the keyed form; see MemberDef.
Implementation status (beta)
A few behaviors on this page describe the agreed target; the reference implementation is catching up:
Whole-number enforcement for the
intfamily is not yet applied (intcurrently accepts3.14).bytealias is being added (currently onlyuint8is recognized).Explicit
min/maxcan widen a shortcut's range (under review).NaN/Infunder a bound resolve tonull(under review).
Examples
A schema mixing notations and family members (adapted from the playground):
Any input notation works for any number type — 0x11, 0o21, 0b10001, and 17 all denote the same value.
See Also
Numeric Values — how numbers are written
Last updated
Was this helpful?
