Type Coercion Suite (`infer.coerce`)
Safely parse incoming strings from HTTP queries, forms, and headers into real typed values.
Why Coercion Matters
Rather than writing manual parsing logic across every controller, wrap fields with infer.coerce.*. If input is string '25', it becomes number 25 before schema checks run.
const QueryParams = infer.object({
page: infer.coerce.number().int().min(1).default(1),
limit: infer.coerce.number().int().max(100).default(20),
activeOnly: infer.coerce.boolean(),
startDate: infer.coerce.date(),
balance: infer.coerce.bigint(),
idString: infer.coerce.string()
});Supported Coercion Methods
• infer.coerce.string(): Converts any value to string using String(val) or JSON.stringify for objects. • infer.coerce.number(): Uses Number(val); produces issue if result is NaN. • infer.coerce.boolean(): Evaluates boolean truthiness with custom string rules. • infer.coerce.bigint(): Converts string/number to BigInt. • infer.coerce.date(): Parses date strings/timestamps into valid Date instances.
Smart Boolean Parsing
By default, JavaScript Boolean('false') evaluates to true. Subatom Infer's infer.coerce.boolean() correctly maps 'false', '0', and 'off' to false.
