Schema - Number
The .number method is used to validate whether a given value is of type number. It ensures that the input meets the expected data type criteria and can be further combined with additional validation methods to define specific rules for numeric inputs, such as range restrictions or specific formats.
This method is the foundation for validating numerical data in your application, providing flexibility to handle diverse use cases like validating IDs, quantities, prices, or any numeric-based input.
Content
Quick Start
Below is an example of how to use .number to validate whether a value is a valid number:
import { schema } from 'vkrun'
const exampleSchema = schema().number()
const validateA = exampleSchema.validate(123) // Valid number
const validateB = exampleSchema.validate('123') // Invalid number (string)
console.log(validateA) // true
console.log(validateB) // falseError Messages
The .number method provides robust error messages to help identify issues during validation. These messages can be used as is or customized for specific use cases.
Default Error Message
If no custom error message is provided, the .number method generates the following default message:
"[valueName] must be a number type!"This message dynamically replaces [valueName] with the name of the variable being validated, providing clarity about the source of the error.
Example
import { schema } from 'vkrun'
const value = 'one'
const numberSchema = schema().number()
const defaultValue = numberSchema.parse(value, 'value_name')Throw:
value_name must be a number type!Custom Error Messages
To make error messages more descriptive or tailored to specific use cases, you can define custom messages using keywords like [value] and [valueName]. These keywords are replaced dynamically with the actual value and the name of the variable being validated.
Supported Keywords
[value]: The actual value being validated.[valueName]: The name of the value being validated.
Example
import { schema } from 'vkrun'
const value = 'one'
const exampleSchema = schema().number({
message: '[valueName] must be of type number, but received [value]!',
})
const customResult = exampleSchema.parse(value, 'value_name')Throw:
value_name must be of type number, but received one!