UNPKG

js-formdata-validator

Version:

JS Form Validator is a simple form data validation library for JavaScript. It provides a set of base rules for checking the type and value of various inputs, and allows you to define custom rules as well.

114 lines (113 loc) 3.95 kB
import type { FormData, FormState, ValidationRules, BaseValidatorRule, CustomRules, CustomFieldName, CustomValidatorErrorMessage } from "./type"; export default class Validator { private formData; private rules; private validatorError; private validator; stopOnFirstFailure: boolean; private customFieldNames; private customValidatorErrorMessage; /** * Constructor for the Validator class. * @param formData The form data to validate. * @param customRules Custom validation rules to use. * @param rules Validation rules to apply to the form data. */ constructor({ formData, customRules, rules, stopOnFirstFailure, }?: FormState); /** * Merges the base rules and custom rules into a single object. * @param customRules The custom rules to merge with the base rules. */ mergeCustomRules(customRules: CustomRules | undefined): this; /** * Validates the form data with the set of given rules. */ validate(): Promise<Validator>; /** * Validates the given field with the set of rules. * @param field The field to validate. * @param fieldRules The validation rules to apply to the field. */ private validateField; /** * Validates the elements of an array field in the form data. * @param field The field name, including a wildcard character. * @param fieldKeys The field name split into an array of keys. * @param fieldRules The validation rules to apply to the field. */ private validateArrayObjects; /** * Validates the elements of an array field in the form data. * @param field The field name, whitout a wildcard character. * @param fieldKeys The field name split into an array of keys. * @param fieldRules The validation rules to apply to the field. */ private validateFieldValue; /** * Gets the validator result for the given field and rule. * @param field The field to validate. * @param rule The validation rule to apply to the field. */ private getValidatorResult; /** * Handles string rules by parsing the rule string and calling the corresponding * validator function with the provided arguments. * @param field The field to validate. * @param rule The string rule to handle. */ private handleStringRule; /** * To parse rule into validator name & parameters * Example: * between:2,3 * Result: * validatorName = between * parameters = [2,3] * @param rule */ private parseRule; /** * To get errorBag */ getErrorBag(): import("./type").ErrorBag; /** * To get overall error mesasge */ getErrorMessage(): string; /** * To set custom error message */ setErrorMessage(errorMessage: string): void; /** * To get error by field name */ getError(field: string): string | undefined; /** * To get failed fields */ getFailedFields(): string[]; /** * To check if form data is pass the validator */ pass(): boolean; /** * To check if form data is fail the validator */ fail(): boolean; /** * To clear error from error bags */ clearErrors(): this; getFormData(): FormData; setFormData(formData: FormData): this; setFieldErrors(field: string, fieldErrors: string[]): void; setFormKeyValue(key: string, value: any): void; getValidator(): BaseValidatorRule & CustomRules; setValidator(validator: BaseValidatorRule & CustomRules): this; getRules(): ValidationRules; setRules(rules: ValidationRules): this; setCustomFieldName(customFieldNames: CustomFieldName): void; getCustomFieldName(): CustomFieldName; setCustomValidatorErrorMessage(customValidatorErrorMessage: CustomValidatorErrorMessage): void; getCustomValidatorErrorMessage(): CustomValidatorErrorMessage; }