SCHEMA
Validation Types
.throwAsync

Schema - ThrowAsync

The .throwAsync method performs the same functionality as .throw, but works asynchronously, throwing an error if the value fails validation.


Syntax

.throwAsync(value: any, valueName: string, ClassError?: ErrorTypes): Promise<void>
  • value: The value or a Promise of 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 .throwAsync to validate a value asynchronously and throw an error if validation fails:

import { schema } from "vkrun"
 
const schema = schema().number()
 
try {
  const value = async (): Promise<string> => {
    return await new Promise((resolve) => {
      setTimeout(() => {
        resolve("123")
      }, 100)
    })
  }
 
  schema.throwAsync(value(), "value_name")
} catch (error) {
  console.error(error.message) // "value_name must be a number type!"
}
Copyright © 2024 MIT by Mario Elvio