@formkit/validation

介绍

FormKit的官方验证包/插件。阅读验证文档以获取使用说明。

函数

createMessageName()

给定一个节点,返回应在验证消息中使用的名称。这可以是validationLabel属性、label属性或输入的名称(按顺序)。

签名

createMessageName(node: FormKitNode): string;

参数

  • node — 要显示的节点

返回值

createValidationPlugin()

实际的验证插件函数。必须在此处进行所有的引导。

签名

createValidationPlugin(baseRules?: FormKitValidationRules): (node: FormKitNode) => void;

参数

  • baseRules 可选 — 要包含在插件中的基本验证规则。默认情况下,FormKit通过defaultConfig提供了@formkit/rules包中的所有规则。

getValidationMessages()

从给定的节点及其所有后代中提取所有验证消息。这不是响应式的,必须在每次消息更改时重新调用。

签名

getValidationMessages(node: FormKitNode): Map<FormKitNode, FormKitMessage[]>;

参数

  • node — 要从中提取验证规则的FormKit节点及其后代。

TypeScript

FormKitValidationHints

影响验证应用方式的特殊验证属性。

interface FormKitValidationHints {
    blocking: boolean;
    debounce: number;
    force: boolean;
    name: string;
    skipEmpty: boolean;
}

FormKitValidationMessages

本地化验证消息注册表的接口。

interface FormKitValidationMessages {
    [index: string]: string | ((...args: FormKitValidationI18NArgs) => string);
}

FormKitValidationRules

FormKit验证规则结构化为一个键/函数对的对象,其中对象的键是验证规则名称。

interface FormKitValidationRules {
    [index: string]: FormKitValidationRule;
}

FormKitValidation

定义完全解析的验证规则的样式。

type FormKitValidation = {
    rule: FormKitValidationRule;
    args: any[];
    timer: number;
    state: boolean | null;
    queued: boolean;
    deps: FormKitDependencies;
    messageObserver?: FormKitObservedNode;
} & FormKitValidationHints;

FormKitValidationI18NArgs

传递给i18n插件中的验证消息的参数。

type FormKitValidationI18NArgs = [
    {
        node: FormKitNode;
        name: string;
        args: any[];
        message?: string;
    }
];

FormKitValidationIntent

定义解析验证规则的样式,但尚未必定替换验证规则。

type FormKitValidationIntent = [string | FormKitValidationRule, ...any[]];

FormKitValidationRule

通用验证规则的签名。它接受一个输入(通常是字符串),但应能够接受任何输入类型,并返回一个布尔值,指示是否通过了验证。

type FormKitValidationRule = {
    (node: FormKitNode, ...args: any[]): boolean | Promise<boolean>;
    ruleName?: string;
} & Partial<FormKitValidationHints>;