Rule

Class Rule

Validation rule class.

Constructor

Rule(rule: Rule | RuleSchema | Validator);

Parameters

  • rule - The schema object or Rule instance or just a validator function.
type Validator = (
  value: any,
  // The rule's properties, e.g, maxLength, min...
  props?: Record<string, any>,
  ...args: any[]
) => string | boolean | Promise<string | boolean>;

interface RuleSchema {
  validator?: Validator;
  // Rule name. If not provided, a name will be auto generated 
  name?: string;
  // The error message when the rule is invalid
  message?: string;
}

Properties

PropTypeDefaultDescription
name read onlystringThe rule's name
messagestring | nullnullThe localized message to be shown as error message.
validatorValidator | nullnullThe validator for current rule.
validbooleantrueIndicate current rule is valid or not.
errorstring | nullnullThe localized error message.
schemaRuleSchema | ValidatorThe current rule schema.

Methods

setMessage

Set rule message.

Signatures

setMessage(message?: string): void;

Parameters

  • message - The message.

reset

Reset the current rule to the initial state. That is error = null, valid = true.

Signatures

reset(): void;

validate

Validate curernt rule asynchronously. When calling, it will first trigger the validate event, then do the valiadtion. Finally, emit a validated event.

Signatures

async validate(value: any, props: Record<string, any> = {}, ...args: any[]): Promise<Rule>;

Parameters

  • value - Any value to validate.
  • props - Properties object that used in validator.
  • ...args - The rest of the parameters want to pass along to the validator.
vue-formily will pass the current element's props to the 3rd paremter, and that element to the 4th paramter.

Returns

  • The Promise of Rule instance.

getSchema

Return current schema.

Signatures

getSchema(): RuleSchema | Validator;

Returns

  • RuleSchema | Validator.

Inherited Methods

From class Evento

Edit this page on GitHub Updated at Fri, Feb 4, 2022