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
Prop | Type | Default | Description |
---|---|---|---|
name read only | string | The rule's name | |
message | string | null | null | The localized message to be shown as error message. |
validator | Validator | null | null | The validator for current rule. |
valid | boolean | true | Indicate current rule is valid or not. |
error | string | null | null | The localized error message. |
schema | RuleSchema | Validator | The 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
ofRule
instance.
getSchema
Return current schema.
Signatures
getSchema(): RuleSchema | Validator;
Returns
RuleSchema | Validator
.