simple-body-validator
Version:
This package is inspired by Laravel validation, and aims to make body validation easier for Javascript developers
163 lines (162 loc) • 5.24 kB
TypeScript
import { CustomMesages, InitialRules, CustomAttributes, CustomErrors } from './types';
import ErrorBag from './validators/errorBag';
declare class Validator {
/**
* The lang used to return error messages
*/
private lang;
/**
* The data object that will be validated
*/
private data;
/**
* The rules that will be used to check the validity of the data
*/
private rules;
/**
* This is an unchanged version of the inital rules before being changed for wildcard validations
*/
private initalRules;
/**
* The array of wildcard attributes with their asterisks expanded.
*/
private implicitAttributes;
/**
* Hold the error messages
*/
private messages;
/**
* Stores an instance of the validateAttributes class
*/
private validateAttributes;
/**
* Flag that defines wether or not validation should stop on first failure
*/
private stopOnFirstFailureFlag;
/**
* Custom mesages returrned based on the error
*/
customMessages: CustomMesages;
/**
* Object of custom attribute name;
*/
customAttributes: CustomAttributes;
constructor(data: object, rules: InitialRules, customMessages?: CustomMesages, customAttributes?: CustomAttributes);
setData(data: object): Validator;
setRules(rules: InitialRules): Validator;
setLang(lang: string): Validator;
getLang(): string;
setCustomMessages(customMessages?: CustomMesages): Validator;
setCustomAttributes(customAttributes?: CustomAttributes): Validator;
stopOnFirstFailure(stopOnFirstFailure?: boolean): Validator;
errors(): ErrorBag;
clearErrors(keys?: string[]): ErrorBag;
/**
* Create a new ErrorBag instance and set the custom errors, thus removing previous error messages
*/
setErrors(errors: CustomErrors): ErrorBag;
/**
* Append the error messages to the existing ErrorBag instance, thus preserving the old error messages if any
*/
appendErrors(errors: CustomErrors): ErrorBag;
/**
* Run the validator's rules against its data.
*/
validate(key?: string, value?: any): boolean;
/**
* Run the validator's rules against its data asynchronously.
*/
validateAsync(key?: string, value?: any): Promise<boolean>;
/**
* Get the displayable name of the attribute.
*/
getDisplayableAttribute(attribute: string): string;
private addCustomErrors;
/**
* Replace all error message place-holders with actual values.
*/
private makeReplacements;
/**
* Loop through all rules and run validation against each one of them
*/
private runAllValidations;
/**
* Loop through all rules and run validation against each one of them asynchronously.
*/
private runAllValidationsAsync;
/**
* Run validation for one specific attribute
*/
private runSingleValidation;
/**
* Run validation for one specific attribute asynchronously.
*/
private runSingleValidationAsync;
/**
* Run validation rules for the specified property and stop validation if needed
*/
private runValidation;
/**
* Run validation rules for the specified property asynchronously and stop validation if needed
*/
private runValidationAsync;
/**
* Check if we should stop further validations on a given attribute.
*/
private shouldStopValidating;
/**
* Parse the given rules add assign them to the current rules
*/
private addRules;
/**
* validate a given attribute against a rule.
*/
private validateAttribute;
/**
* Validate an attribute using a custom rule object
*/
private validateUsingCustomRule;
/**
* Set the error message linked to a custom validation rule
*/
private setCustomRuleErrorMessages;
/**
* Add a new error message to the messages object
*/
private addFailure;
/**
* Replace each field parameter which has asterisks with the given keys.
*
* Example: parameters = [name.*.first] and keys = [1], then the result will be name.1.first
*/
private replaceAsterisksInParameters;
/**
* Determine if the attribute is validatable.
*/
private isValidatable;
/**
* Determine if the field is present, or the rule implies required.
*/
private presentOrRuleIsImplicit;
/**
* Determine if the attribute passes any optional check.
*/
private passesOptionalCheck;
/**
* Determine if the attribute fails the nullable check.
*/
private isNotNullIfMarkedAsNullable;
/**
* Get the primary attribute name
*
* Example: if "name.0" is given, "name.*" will be returned
*/
private getPrimaryAttribute;
/**
* Get the explicit keys from an attribute flattened with dot notation.
*
* Example: 'foo.1.bar.spark.baz' -> [1, 'spark'] for 'foo.*.bar.*.baz'
*/
private getExplicitKeys;
}
export default Validator;