Numbers and enums
Pema provides numeric schemas for JavaScript number and bigint values. All
numeric schemas are Storable; the width determines their database datatype.
Numeric types
| Factory | JavaScript value | Range or alias |
|---|---|---|
p.number, p.f64 |
number |
64-bit floating point |
p.f32 |
number |
exactly representable as 32-bit float |
p.int, p.i32 |
number |
−2³¹ through 2³¹−1 |
p.uint, p.u32 |
number |
0 through 2³²−1 |
p.i8, p.i16 |
number |
signed 8- and 16-bit integers |
p.u8, p.u16 |
number |
unsigned 8- and 16-bit integers |
p.bigint, p.i64 |
bigint |
signed 64-bit integer |
p.biguint, p.u64 |
bigint |
unsigned 64-bit integer |
p.i128 |
bigint |
signed 128-bit integer |
p.u128 |
bigint |
unsigned 128-bit integer |
Integer schemas reject fractions and enforce their declared range. Loose number and integer schemas coerce valid numeric strings:
p.loose.u16.parse("8080"); // 8080
p.u16.parse("8080"); // ParseErrorLoose bigint schemas (bigint, biguint, i64, u64, i128, u128)
preserve integer strings exactly, including values beyond JavaScript's safe
integer range:
p.loose.bigint.parse("9007199254740993"); // 9007199254740993n
p.loose.u64.parse("18446744073709551615"); // 18446744073709551615n
p.loose.bigint.parse("4.2e1"); // 42nDecimal strings may include surrounding whitespace, a sign, a decimal point,
or an exponent, but must represent an exact integer within the schema's
range. Genuinely fractional strings, including "1.0000000000000001", are
rejected rather than rounded. If your application needs rounding, apply an
explicit rounding policy before validation. Hexadecimal/binary/octal prefixes,
numeric separators, bigint n suffixes, and non-finite numeric forms are not
accepted as strings.
JavaScript number inputs retain their existing precision semantics: a bigint
schema cannot recover digits already lost when a number was created. Use a
string or bigint input when exactness matters. Strict schemas do not coerce
strings or numbers to bigint.
Numeric validators
Numeric types expose:
| Method | Requirement |
|---|---|
values({ ... }) |
Value occurs in the object's values |
range(from, to) |
Inclusive range |
min(from) |
Value is at least from |
max(to) |
Value is at most to |
check(predicate, message) |
Custom primitive check |
Numeric minimums and maximums are value thresholds, not cardinalities. A floating-point schema may therefore use negative and fractional limits:
const Temperature = p.number.min(-273.15).max(1_000);Unsigned number types also expose port(), which accepts non-privileged ports
from 1024 through 65535:
const Port = p.u16.port();The public NumberType and UintType types describe common concrete return
types. DataType maps persistent datatype names to their JavaScript values.
Enums
p.enum(values) creates a numeric enum stored as u8. Keys must use
SCREAMING_SNAKE_CASE and values must fit the underlying unsigned byte schema.
const Status = p.enum({
DRAFT: 0,
PUBLISHED: 1,
});
Status.parse(Status.DRAFT); // 0
Status.nameOf(1); // "PUBLISHED"
Status.values; // { DRAFT: 0, PUBLISHED: 1 }The returned Enum combines EnumType with the declared constants. It also
supports optional() and default().
See Pema and Stores for the complete persistence matrix.