Schema - Introduction
Schema is a module of VkrunJS designed for runtime data validation. It provides a comprehensive set of tools to validate and structure data in JavaScript and TypeScript projects. With Schema, you can define simple or complex validation rules to ensure the integrity of your application's data.
Schemas are created by chaining validation methods, making it intuitive to define and manage data validation logic. Schema offers a robust, customizable, and scalable solution for your validation needs.
Quick Start
- Import schema.
- Chain methods to build a schema.
- After completing the schema, call a validation method with the input value:
.validate(value: any): boolean
.validateAsync(value: any): Promise<boolean>
.test(value: any, valueName: string): Tests
.testAsync(value: any, valueName: string): Promise<Tests>
.throw(value: any, valueName: string, ClassError?: ErrorTypes): void
.throwAsync(value: any, valueName: string, ClassError?: ErrorTypes): Promise<void>
The following example demonstrates a simple schema for validating a user object:
import { schema } from "vkrun"
const userSchema = schema().object({
id: schema().string().UUID(),
fullName: schema().string().minWord(2),
email: schema().string().email()
})
// Validates the data against the schema
const isValid = userSchema.validate({
id: "3ef7c105-c4ea-444d-bf47-e2e1a49ea613",
fullName: "John Doe",
email: "john.doe@example.com"
})
console.log(isValid) // true