UNPKG

logitar-validation

Version:

JavaScript validation library distributed by Logitar.

85 lines (84 loc) 3.2 kB
import type { RuleConfiguration, RuleOptions, ValidationOptions, ValidationResult, ValidationRule, ValidationRuleKey, ValidationRuleSet, ValidatorOptions } from "./types"; /** * A validator is a collection of validation rules that can be executed on a value. */ declare class Validator { /** * The message formatter to use. */ private readonly messageFormatter; /** * The rules registered to this validator. */ private readonly rules; /** * A value indicating whether the validator should throw an error if the validation fails. */ private readonly throwOnFailure; /** * A value indicating whether warnings should be treated as errors. */ private readonly treatWarningsAsErrors; /** * Initializes a new instance of the Validator class. * @param options The options of the validator. */ constructor(options?: ValidatorOptions); /** * Clears all the rules registered to this validator. */ clearRules(): void; /** * Gets a rule from the validator. * @param key The key of the rule to get. * @returns The rule configuration. */ getRule(key: ValidationRuleKey): RuleConfiguration | undefined; /** * Checks if a rule is registered to this validator. * @param key The key of the rule to check. * @returns A value indicating whether the rule is registered to this validator. */ hasRule(key: ValidationRuleKey): boolean; /** * Lists all the rules registered to this validator. * @returns The rules registered to this validator. */ listRules(): [ValidationRuleKey, RuleConfiguration][]; /** * Removes a rule from the validator. * @param key The key of the rule to remove. * @returns A value indicating whether the rule was removed from the validator. */ removeRule(key: ValidationRuleKey): boolean; /** * Registers a rule to the validator. * @param key The key of the rule to register. * @param rule The rule to register. * @param options The options of the rule. */ setRule(key: ValidationRuleKey, rule: ValidationRule, options?: RuleOptions): void; /** * Validates a field/property against a set of rules. * @param name The name of the field/property to validate. * @param value The value of the field/property to validate. * @param rules The rule set to validate the value against. * @param options The options of the validation operation. * @returns The result of the validation operation. */ validate(name: string, value: unknown, rules: ValidationRuleSet, options?: ValidationOptions): ValidationResult; /** * Formats a validation rule execution message. * @param result The result to format the message of. * @param options The options of the validation operation. */ private formatMessage; /** * Checks if a severity is an error. * @param severity The severity to check. * @param options The options of the validation operation. * @returns A value indicating whether the severity is an error. */ private isError; } export default Validator;