SCHEMA
Validation Methods
.object

Schema - Object

The .object method is used to validate whether a given value is an object and that it matches a set of specified properties, each with its own schema for validation. It ensures that the input value is an object and that each property adheres to the defined validation rules. This method is essential when you need to validate structured data, such as user profiles, form submissions, or complex objects with specific properties.


Example: Basic Validation with .object

Below is an example of how to use .object to validate whether an object matches the defined schema for its properties:

import { schema } from "vkrun"
 
const exampleSchema = schema().object({
  id: schema().string().UUID(),
  fullName: schema().string().minWord(2),
  description: schema().string().notRequired()
})
 
const validateA = exampleSchema.validate({
  id: "3ef7c105-c4ea-444d-bf47-e2e1a49ea613",
  fullName: "Full Name"
})
 
const validateB = exampleSchema.validate({
  id: "3ef7c105-c4ea-444d-bf47-e2e1a49ea613",
  description: "Description"
})
 
console.log(validateA) // true
console.log(validateB) // false
Copyright © 2024 MIT by Mario Elvio