/* * @copyright: ChengXuan * @Author: ding * @Date: 2022-08-19 10:06:04 * @LastEditors: ding * @LastEditTime: 2022-08-19 11:18:42 * @Description: egg-validate 插件补充类型声明 * */ declare module "egg" { export interface Context { /** * * 参数验证插件 * - see https://github.com/node-modules/parameter#rule * * @param {IvalidateRules} rule * @param {*} [data] 要验证的数据,默认是ctx.request.body * @memberof Context */ validate(rule: IvalidateRules, data?: any): void; } export interface Application { /** 参数对象验证器 */ validator: { /** 添加规则 */ addRule: (type: string, check: RegExp | CheckHandlerFunc) => void; /** app上验证,但不会抛出错误,而是返回错误 */ validate: (rules: IvalidateRules, data: any) => ValidateError[]; }; } } /** egg-validate 抛出的校验异常信息 */ interface ValidateError { code: string; field?: string; message: string; } /** 添加规则处理函数,返回字符串表示错误信息,不返回表示验证通过 */ type CheckHandlerFunc = (rule: IvalidateRules, value: any) => string | void; /** 验证插件的验证规则 * * - see https://github.com/node-modules/parameter#rule */ interface IvalidateRules { [key: string]: | { type: TenumValidateDataType; required?: boolean; default?: any; /** use addRules added rule use test field get the addRule name. */ test?: string; widelyUndefined?: any; convertType?: "int" | "number" | "string" | "boolean" | ((...args: any[]) => any); /** for int integer number string password and array. approve it to limit. */ max?: number; /** for int integer number string password and array. approve it to limit. */ min?: number; /** for string email.*/ allowEmpty?: boolean; /** for string email. equal to allowEmpty */ empty?: boolean; /** for string. - A RegExp to check string's format. */ format?: RegExp; /** for string. - Trim the string before check, default is false. */ trim?: boolean; /** for password. - Compare field to check equal, default null, not check. */ compare?: string; /** for enum. An array of data, value must be one on them. this rule is required. */ values?: any[]; /** * for object. An object that validate the properties ot the object. * * for array. It's array of object. An object that validate the items of the array. Only work with itemType. * */ rule?: IvalidateRules | IvalidateRules[]; /** for array. The type of every item in this array. */ itemType?: TenumValidateDataType; } | TAttrValidateType | any[] | RegExp; } /** 数据类型 */ type TenumValidateDataType = | "int" | "integer" | "number" | "string" | "boolean" | "bool" | "date" | "datetime" | "dateTime" | "id" | "password" | "email" | "url" | "enum" | "object" | "array"; /** 简写的验证类型 省略写对象*/ type TAttrValidateType = | "int" | "int?" | "integer" | "number" | "string" | "string?" | "password" | "boolean" | "bool" | "date" | "dateTime" | "id" | "email" | "url" | "enum" | "object" | "array";