@sebgroup/frontend-tools
Version:
A set of frontend tools
94 lines (93 loc) • 4.97 kB
TypeScript
export declare type ValidationSpecs = {
minLength?: number;
maxLength?: number;
minValue?: number;
maxValue?: number;
minDate?: Date;
maxDate?: Date;
};
export declare type ModelFieldError<K extends string = "empty", J extends {
[key in string | number]: any;
} = {}> = {
errorCode: ValidationErrors | K;
specs?: ValidationSpecs & J;
};
export declare type ModelErrors<T, J extends string = "empty"> = {
[K in keyof T]?: ModelFieldError<J>;
};
export declare type ValidationTypeWithoutSpecs = "required" | "isDate" | "validEmail" | "strongPassword" | "isPhoneNumber";
export declare type ValidationTypeWithSpecs = "dateRange" | "valueRange" | "textLength";
export declare type ValidationType = ValidationTypeWithoutSpecs | ValidationTypeWithSpecs;
export declare type ValidationErrors = "empty" | "invalidDate" | "invalidEmail" | "weakPassword" | "beforeMinDate" | "afterMaxDate" | "lessThanMinLength" | "moreThanMaxLength" | "lessThanMinValue" | "moreThanMaxValue" | "invalidInput" | "invalidPhoneNumber";
export declare type FormFieldTypes = "date" | "string" | "number" | "array" | "object";
declare type SpecialPick<T, K extends keyof T, P extends keyof T> = Required<Pick<T, K>> | Required<Pick<T, P>> | Required<Pick<T, K | P>>;
export declare class FormValidator<T> {
private originalFormObject;
private formObject;
private formObjectErrors;
private customValidators;
constructor(formObject: T);
/**
* Validates the specified fields based on the validation type
* @param fields The fields to be validated
* @param type The type of validation
* @important Not matching `ValidationTypeWithoutSpecs` typescript error means that you need to provide a specs object as a third paramenter for the selected type
* @returns The form validator
*/
addValidation(fields: Array<keyof T>, type: ValidationTypeWithoutSpecs): FormValidator<T>;
/**
* Validates the specified fields based on the validation type
* @param fields The fields to be validated
* @param type The type of validation `"dateRange"`
* @param spec The specifications of the validation (`minDate` and/or `maxDate`)
* @returns The form validator
*/
addValidation(fields: Array<keyof T>, type: "dateRange", specs: SpecialPick<ValidationSpecs, "minDate", "maxDate">): FormValidator<T>;
/**
* Validates the specified fields based on the validation type
* @param fields The fields to be validated
* @param type The type of validation `"textLength"`
* @param spec The specifications of the validation (`minLength` and/or `maxLength`)
* @returns The form validator
*/
addValidation(fields: Array<keyof T>, type: "textLength", specs: SpecialPick<ValidationSpecs, "minLength", "maxLength">): FormValidator<T>;
/**
* Validates the specified fields based on the validation type
* @param fields The fields to be validated
* @param type The type of validation `"valueRange"`
* @param spec The specifications of the validation (`minValue` and/or `maxValue`)
* @returns The form validator
*/
addValidation(fields: Array<keyof T>, type: "valueRange", specs: SpecialPick<ValidationSpecs, "minValue", "maxValue">): FormValidator<T>;
/**
* Add a custom validator that returns an error message if found
* @param {Array<string>} errorFields The fields where the error is reported to
* @param {function} validator The validator method
* @returns {FormValidator} The form validator object
* @example addValidator(["balance", "payment"], ["payment"], (balance: number, payment: number) => { return payment > balance ? "The payment exceeds your balance" : null; });
*/
addCustomValidation(errorFields: Array<keyof T>, validator: () => ModelFieldError<any>): FormValidator<T>;
/**
* Get the error found in the form object. Has to be called after `validate` method has been called.
* @returns {any} The form object object populated by validation errors, if found any. Otherwise, it's an empty object.
*/
getErrors(): ModelErrors<T>;
/**
* Get a specific error found during validation, if any. Has to be called after `validate` method has been called.
* @returns {any} The error of the specific item in the form, if any.
*/
getError(name: keyof T): ModelFieldError;
/**
* Validates the form object passed in the constructor
* @returns {FormValidator} The form validator object
*/
validate(): FormValidator<T>;
/**
* Validate a parameter in the form formObject based on predefined set of criteria
* @param {ValidatorModelItem} fieldObject The field object stored in the local formObject
* @returns {string} The error found in the parameter
*/
private validateField;
private isValidType;
}
export {};