Skip to main content

Number Mutators

Number mutators transform the input data before validation. The number mutator automatically converts string values to numbers.


All Mutators


Auto Number Conversion

The Number Validator automatically converts string values to numbers using the numberMutator. This mutator is applied automatically when you create a v.number() validator.

How It Works

v.number()
// Input: "123" → Output: 123
// Input: "45.67" → Output: 45.67
// Input: "0" → Output: 0
// Input: 123 → Output: 123 (unchanged)

Examples

String to Number

const numberSchema = v.number()
.required()
.min(0)
.max(100);

const result = await validate(numberSchema, "50");
// Result: { isValid: true, data: 50 }

Decimal String to Number

const priceSchema = v.number()
.required()
.min(0);

const result = await validate(priceSchema, "99.99");
// Result: { isValid: true, data: 99.99 }

Form Input Handling

// HTML form inputs are always strings
const formSchema = v.number()
.required()
.between(1, 100);

const result = await validate(formSchema, "25"); // From form input
// Result: { isValid: true, data: 25 }

See Also