Schema - NotRequired
The .notRequired()
method is used to specify that a value is not required, meaning it can be undefined
. By default, Schema treats the value as required, meaning it must be different from undefined
. When a value is not required and should only be validated if provided (i.e., other than undefined
), we use the .notRequired()
method.
Example
Below is an example of how to use .notRequired()
to validate a value that is not required and can be undefined
:
import { schema } from "vkrun"
const exampleSchema = schema().number().notRequired()
const validateA = exampleSchema.validate(undefined)
const validateB = exampleSchema.validate(123)
const validateC = exampleSchema.validate("123")
console.log(validateA) // true
console.log(validateB) // true
console.log(validateC) // false