Schema - Throw
The .throw
method validates a value synchronously and throws an error if the value fails validation. It is particularly useful when validation failure should halt further execution.
Syntax
.throw(value: any, valueName: string, ClassError?: ErrorTypes): void
- value: The value to be validated.
- valueName: A string used to reference the value in the error message.
- ClassError (optional): A custom error class to be used when throwing the error.
Example
The following example demonstrates how to use .throw
to validate a value and throw an error if the validation fails:
import { schema } from "vkrun"
const schema = schema().number()
try {
schema.throw("123", "value_name")
} catch (error) {
console.error(error.message) // "value_name must be a number type!"
}