UNPKG

@codeparticle/formal

Version:

A <2kb library for validating data of any kind

43 lines (39 loc) 1.67 kB
import { ValidationRuleset, ValidationCheck, ValidationM } from '../types/index.js'; /** * @file Util functions for validators * @name utils.js * @author Nick Krause * @license MIT */ declare class ValidationError extends Error { } /** * id function - returns whatever its input is. * @param {Any} x - Any param supplied to this function. * @returns {Any} The param supplied to this function. */ declare const id: (x: any) => any; declare const pipeValidators: (fns: ValidationRuleset, values?: any) => ValidationCheck; /** * returnConstant is a convenience method that can be used as an argument to functions that require a function, * but ultimately do nothing but return a primitive value. * @param {Any} x - Primitive value to be returned. */ declare function returnConstant<T>(x: T): () => T; /** * Function that checks whether the supplied validator is, itself, valid. * @param {Function} validator - Parameter description. * @throws {Exception Type} Exception description. */ declare const checkIsValidationM: (validator: ValidationM) => void; /** * Function that takes an object with field names and rules, then applies those rules to the fields * of another object with the same field names. Great for validation of entire objects at once, like * forms or API responses. */ declare const validateObject: <Rules extends Record<string, ValidationRuleset>>(fieldRules: Rules) => <Vals extends Record<keyof Rules, any>>(values: Vals) => { values: Vals; hasErrors: boolean; errors: Partial<Record<keyof Rules, string[]>>; }; export { ValidationError, checkIsValidationM, id, pipeValidators, returnConstant, validateObject };