@acai/validator 中文文档教程

发布于 3年前 浏览 22 项目主页 更新于 3年前

Açai Validator Module

https://gitlab.com /acaijs/validator.git https: //www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator

Açaí 框架的可定制验证器。 您可以轻松添加自己的规则并扩展我们验证器的功能。

Usage

import { addRule } from "@acai/validator";

// first thing we need is to extend the base Validator class
class RegisterValidator extends Validator {
  protected getSchema() {
    return {
      email: ["required", "email"],
      password: ["required", "confirmed"],
    };
  }
}

// now we get the fields to be validated
const fields = {
  email: "not an email",
  password: "not confirmed password",
};

const validation = new RegisterValidator(fields);
const errors = validation.errors;

// this will be filled if the validation failed
if (errors) {
  console.warn(errors);
}

Custom rules

import { addRule } from "@acai/validator";

addRule("password", {
    /** Validation to check if value should pass */
    onValidate  ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return /* your validation here */
    }
    /** Message to be returned in case of validation error */
    onError     ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return `${key} is not a valid password`;
    }
    /** Alters values to other rules */
    onMask      ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return /* new masked value here */;
    }
});

Rules

Type validation rules

nameargsdescription
arraycontent type, such as: string, number, etc…Check if field is an array
objectcontent type, such as: string, number, etc…Check if field is an object
booleanno argumentsCheck if field is an boolean
stringno argumentsCheck if field is an string
number/floatforce to allow strings that can be convertable to numberCheck if value is a valid number and if force is passed, maskes field into an actual number if is not
integerforce to allow strings that can be convertable to numberCheck if value is a valid integer and if force is passed, maskes field into an actual integer if is not

Composite validation rules

这些规则适用于同一领域中存在的其他规则

nameargsdescription
includeslist of keywords to lookCheck if the field includes the keywords present, if it's an object, will look for the keys, if it's an array, will look for the values. If it's a string or a number, will look for the pattern
minminimal value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is bigger than the passed argument
maxmax value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is smaller than the passed argument

General validation rules

nameargsdescription
requiredno argumentsField cannot be undefined
confirmedreference fieldLooks for a field with the suffix _confirmation that has the same value
uuidno argumentsField must be a valid uuid4
truthyno argumentsField must be a javascript true comparison (1, "any string value, true, etc…)
emailno argumentsField must be a valid email
regexregex patternField must match the provided regex pattern
dateoptional format for dateString that contains a valid date format

Açai Validator Module

https://gitlab.com/acaijs/validator.git https://www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator

A customizable validator for the Açaí framework. You can easily add your own rules and extend the capabilities of our validator.

Usage

import { addRule } from "@acai/validator";

// first thing we need is to extend the base Validator class
class RegisterValidator extends Validator {
  protected getSchema() {
    return {
      email: ["required", "email"],
      password: ["required", "confirmed"],
    };
  }
}

// now we get the fields to be validated
const fields = {
  email: "not an email",
  password: "not confirmed password",
};

const validation = new RegisterValidator(fields);
const errors = validation.errors;

// this will be filled if the validation failed
if (errors) {
  console.warn(errors);
}

Custom rules

import { addRule } from "@acai/validator";

addRule("password", {
    /** Validation to check if value should pass */
    onValidate  ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return /* your validation here */
    }
    /** Message to be returned in case of validation error */
    onError     ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return `${key} is not a valid password`;
    }
    /** Alters values to other rules */
    onMask      ? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
        return /* new masked value here */;
    }
});

Rules

Type validation rules

nameargsdescription
arraycontent type, such as: string, number, etc…Check if field is an array
objectcontent type, such as: string, number, etc…Check if field is an object
booleanno argumentsCheck if field is an boolean
stringno argumentsCheck if field is an string
number/floatforce to allow strings that can be convertable to numberCheck if value is a valid number and if force is passed, maskes field into an actual number if is not
integerforce to allow strings that can be convertable to numberCheck if value is a valid integer and if force is passed, maskes field into an actual integer if is not

Composite validation rules

Those are rules that adapt to other rules present in the same field

nameargsdescription
includeslist of keywords to lookCheck if the field includes the keywords present, if it's an object, will look for the keys, if it's an array, will look for the values. If it's a string or a number, will look for the pattern
minminimal value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is bigger than the passed argument
maxmax value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is smaller than the passed argument

General validation rules

nameargsdescription
requiredno argumentsField cannot be undefined
confirmedreference fieldLooks for a field with the suffix _confirmation that has the same value
uuidno argumentsField must be a valid uuid4
truthyno argumentsField must be a javascript true comparison (1, "any string value, true, etc…)
emailno argumentsField must be a valid email
regexregex patternField must match the provided regex pattern
dateoptional format for dateString that contains a valid date format
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文