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.
49 lines (48 loc) • 1.67 kB
TypeScript
import { CustomRules, CustomValidatorErrorMessage, ValidationRule } from "./type";
export declare class FieldValidator {
private fieldName;
private fieldValue;
private fieldRules;
private validatorError;
private validator;
private formData;
private customFieldName?;
private customValidatorErrorMessage;
constructor();
setCustomRules(customRules: CustomRules): void;
setFieldName(name: string): void;
setFormData(formData: {
[key: string]: any;
}): void;
getFormData(): {
[key: string]: any;
};
getFieldName(): string;
setFieldRules(rules: ValidationRule[]): void;
getFieldRules(): ValidationRule[];
setFieldValue(value: any): void;
getFieldValue(): any;
validate(): Promise<void>;
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;
private parseRule;
pass(): boolean;
fail(): boolean;
getErrorBag(): string[];
/**
* Merges the base rules and custom rules into a single object.
* @param customRules The custom rules to merge with the base rules.
*/
private mergeCustomRules;
private setValidator;
setCustomFieldName(customFieldName: string): void;
getCustomFieldName(): string | undefined;
setCustomValidatorErrorMessage(customValidatorErrorMessage: CustomValidatorErrorMessage): void;
getCustomValidatorErrorMessage(): CustomValidatorErrorMessage;
}