UNPKG

@code-gorilla-au/vue-forms

Version:
112 lines (111 loc) 3.8 kB
export interface RuleArgument { rule: string; value: string; ruleArgs: string[]; } export type RuleFunction = (value: string, ...args: string[]) => boolean; export interface Rule { handler: RuleFunction; validationMessage(value: string): string; } export interface RulesRepository { [key: string]: Rule; } export declare const RULE_NAME_EMAIL = "email"; export declare const RULE_NAME_NOT = "not"; export declare const RULE_NAME_IS = "is"; export declare const RULE_NAME_CONTAINS = "contains"; export declare const RULE_NAME_PREFIX = "prefix"; export declare const RULE_NAME_SUFFIX = "suffix"; export declare const RULE_NAME_RULE_NOT_FOUND = "ruleNotFound"; /** * rule that validate email * @param value email to validate against */ export declare function ruleEmail(value: string): boolean; /** * rule to ensure the input value does not contain the supplied arguments * @param value input value to validate against * @param args arguments provided to the rule. */ export declare function ruleNot(value: string, ...args: string[]): boolean; /** * rule to ensure the input value does not contain the supplied arguments * @param value input value to validate against * @param args arguments provided to the rule. */ export declare function ruleContains(value: string, ...args: string[]): boolean; /** * rule to ensure the input value does contain the supplied arguments * @param value input value to validate against * @param args arguments provided to the rule. */ export declare function ruleIs(value: string, ...args: string[]): boolean; /** * rule to ensure the input value starts with the prefix * @param value input value to validate against * @param args arguments provided to the rule. */ export declare function rulePrefix(value: string, ...args: string[]): boolean; /** * rule to ensure the input value ends with the prefix * @param value input value to validate against * @param args arguments provided to the rule. */ export declare function ruleSuffix(value: string, ...args: string[]): boolean; /** * parse the expression and generate the rules to run against the input value. * @param value input value * @param expression list of rules and arguments to validate against the input */ export declare function parseExpression(value: string, expression: string): RuleArgument[]; /** * Validations rules engine. Run rules against an input value given an expression. * Rule names are in camelCase. If a given rule does not exist, it will not attempt to parse. * See available rules for a list. * * Combine rules using the '|' token (example: 'foo | bar' ). * * Some rules require arguments, declare the rule first, then ':' follow by comma ',' delimited list (example: 'not:foo,bar') * * * @example * ```javascript * // test if input value does not contain the words 'hello' or 'world' * const expression = 'not:hello,world' * const v = validations(); * v.evaluate('hello bin world', expression); * * * ``` * * ```javascript * // test if the input value is a valid email * const expression = 'email' * const v = validations(); * v.evaluate('hello@mail.com', expression); * * * ``` * * ```javascript * // test if the input value does not contain the word 'hello' and is a valid email * const expression = 'not:hello|email' * const v = validations(); * v.evaluate('hello@mail.com', expression); * * * ``` */ export declare function validations(): { /** * list all available rules for the engine */ availableRules(): string[]; /** * evaluate the input against the rules within the expression. * @param inputValue input value to run the rules against * @param expression rules expression */ evaluate(inputValue: string, expression: string): string | undefined; };