UNPKG

web-crawling-utils

Version:
40 lines (39 loc) 2.04 kB
import { Schema } from "./validationSchema"; /** * Validates data against a defined schema, checking for required fields, data types, * and additional constraints (e.g., min/max values, pattern matching). * * @function validate * @param {Schema} schema - A validation schema specifying rules for each field, * including type, required status, minimum/maximum constraints, and pattern matching. * @param {any} data - The input data to be validated against the schema. * @returns {string[]} - An array of validation error messages. If validation passes, * the array will be empty. * * @description This function iterates through each key in the provided schema to validate * the corresponding field in the data. It performs the following checks: * - **Required Fields**: Ensures required fields are present. * - **Type Validation**: Validates the type (e.g., string, number, boolean, array, object, date). * - **String Constraints**: Checks min/max length and pattern matching for strings. * - **Number Constraints**: Enforces min/max values for numbers. * - **Array Validation**: Validates arrays of strings or numbers. * - **Object Validation**: Supports nested object validation through recursive calls. * - **Enum Validation**: Confirms values match one of a specified list. * * @example * const schema = { * name: { type: "string", required: true, minLength: 3 }, * age: { type: "number", min: 18, max: 99 }, * preferences: { type: "array:string" }, * }; * * const data = { name: "Alice", age: 25, preferences: ["reading", "swimming"] }; * const errors = validate(schema, data); * * @remarks * This function provides a centralized validation approach to enforce data integrity and * prevent invalid inputs from entering the application, supporting a uniform data format. * If validation fails, it returns an array of descriptive error messages, highlighting the * fields that do not meet the specified criteria. */ export declare function validate(schema: Schema, data: any): string[];