Pack-size validation
Validate quantities that must be divisible by a fixed pack size.
Zod · Module
.multipleOf(value) constrains a Zod number to values that are multiples of value. Zod documents .step(value) as an alias.
.multipleOf(value) adds a numeric constraint requiring a parsed number to be an exact multiple of the supplied value. Zod documents .step(value) as an alias for the same constraint.
z.number().multipleOf(value)| Name | Type | Description |
|---|---|---|
value | number | The number that every accepted value must be a multiple of. |
A number schema with a multiple-of constraint.
.multipleOf(value) after z.number() to require values to follow a specified multiple.5, meaning accepted values must align with increments of five..step(value) provides an alternate name for the same numeric constraint..step(5) is an alias for .multipleOf(5), so either spelling creates the same constraint.import { z } from "zod";
const schema = z.number().multipleOf(5);
console.log(schema.parse(15)); // 15
// schema.parse(12); // throws a validation errorThe schema accepts numbers divisible by 5 and rejects other numbers.
Validate quantities that must be divisible by a fixed pack size.
Enforce numeric increments such as five-point scores or fixed measurement steps.
.multipleOf() do?It validates that a number is a multiple of the supplied value. For example, z.number().multipleOf(5) accepts 15 but rejects 12.
Follow-up: What equivalent API does the documentation provide? .step(5) is documented as an alias for .multipleOf(5).
Call it on a number schema and pass the interval as an argument, such as z.number().multipleOf(5). The resulting schema keeps the number type while adding the divisibility check.
Follow-up: How would you express the same rule with the alternate spelling? Use z.number().step(5).
.multipleOf() represent?The documented declaration is z.number().multipleOf(5), and the argument specifies the required multiple. Values that do not satisfy that multiple fail validation.
Follow-up: Which related numeric constraints can be combined with it? The same documented number API lists bounds and sign checks such as .gt(), .gte(), .lt(), .lte(), .positive(), and .nonnegative().
stepnumbergtgteltltepositivenegative