{"id":16264875,"url":"https://github.com/d-xuanmo/validator","last_synced_at":"2025-03-16T13:31:00.104Z","repository":{"id":149643511,"uuid":"614410124","full_name":"D-xuanmo/validator","owner":"D-xuanmo","description":"💪 用最少的代码，解决繁琐的事情，专注数据校验","archived":false,"fork":false,"pushed_at":"2024-06-16T02:20:04.000Z","size":309,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-27T09:35:06.102Z","etag":null,"topics":["form-validator","validator"],"latest_commit_sha":null,"homepage":"https://uoo.ink/2d906hs","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/D-xuanmo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-15T14:28:40.000Z","updated_at":"2024-06-16T02:20:07.000Z","dependencies_parsed_at":"2023-11-26T02:21:14.815Z","dependency_job_id":"5e2c528a-4561-46d2-8450-739774c639a8","html_url":"https://github.com/D-xuanmo/validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/D-xuanmo%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/D-xuanmo%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/D-xuanmo%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/D-xuanmo%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/D-xuanmo","download_url":"https://codeload.github.com/D-xuanmo/validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243815592,"owners_count":20352195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["form-validator","validator"],"created_at":"2024-10-10T17:04:11.928Z","updated_at":"2025-03-16T13:30:59.785Z","avatar_url":"https://github.com/D-xuanmo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Validator\n\n[![Node.js CI](https://github.com/D-xuanmo/validator/actions/workflows/build.yml/badge.svg)](https://github.com/D-xuanmo/validator/actions/workflows/build.yml)\n![npm bundle size](https://img.shields.io/bundlephobia/min/%40xuanmo%2Fvalidator)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40xuanmo%2Fvalidator)\n\n## 简介\n\n- `Validator` 是数据集验证工具，主打多个规则以管道符模式进行串行校验，常用于表单数据繁琐的校验\n- 用最少的代码，解决繁琐的事情\n- 支持校验规则扩展、单例模式、异步校验、国际化\n- 完善的 `TypeScript` 类型\n- 规则快速预览： `required|email|length:8|between:2,8`\n\n## 安装\n\n```bash\n# npm\n$ npm i @xuanmo/validator\n\n# yarn\n$ yarn add @xuanmo/validator\n\n# pnpm\n$ pnpm add @xuanmo/validator\n```\n\n## 使用\n\n- [在线编辑](https://uoo.ink/Form) 与 [DLUI](https://www.xuanmo.xin/-/dl-ui/components/form) 表单结合使用\n- [在线编辑](https://uoo.ink/2d906hs) 与 [Element-plus](https://uoo.ink/jkgm4q) 表单结合使用\n\n```typescript\nimport validator from '@xuanmo/validator'\n\n// 默认不注册国际化词条，需要引入国际化词条文件自行注册\nimport zhCN from '@xuanmo/validator/locale/zh-CN.json'\n\n// 注册国际化词条\nvalidator.localize(zhCN)\n\n// 执行校验\nvalidator\n  .validate([\n    {\n      dataKey: 'name',\n      value: 'xuanmo',\n      rules: 'required|max:5'\n    },\n    {\n      dataKey: 'age',\n      value: 18,\n      required: true,\n      rules: 'max:8'\n    },\n    {\n      dataKey: 'scope',\n      value: '',\n      message: '局部校验规则失败',\n      validator() {\n        return false\n      }\n    },\n    // 以下为矩阵校验示例\n    {\n      // 需要指定为矩阵校验\n      matrix: true,\n      // 矩阵对应的 id，整个校验数据唯一\n      matrixId: 'matrixId',\n      value: {\n        columns: [\n          {\n            dataKey: 'column1',\n            label: '矩阵列 1',\n            rules: 'required|min_length:9'\n          },\n          {\n            dataKey: 'column2',\n            label: '矩阵列 2',\n            rules: 'confirmed:@column1'\n          }\n        ],\n        data: [\n          {\n            rowId: 'a',\n            column2: '222value'\n          },\n          {\n            rowId: 'b',\n            column1: 22,\n            column2: 'xxx'\n          }\n        ]\n      }\n    }\n  ])\n  .then(() =\u003e {\n    console.log('校验通过')\n  })\n  .catch((error) =\u003e {\n    console.log(error)\n  })\n```\n\n## API\n\n### 关键词说明\n\n#### message\n\n- `{#field}` 会被替换为当前字段\n- `{meta}` 具体规则的值\n- `ruleParams` 多个值通过 `,` 逗号分隔\n\n```typescript\n// 以 `length` 规则为例，错误信息定义：{#field}长度必须为:{length}\n// 最终执行结果被替换为：name长度必须为:5\nvalidator.validate([\n  {\n    dataKey: 'name',\n    value: 'xuanmo',\n    rules: 'length:5'\n  }\n])\n```\n\n### validate 执行校验\n\n```typescript\nimport validator from '@xuanmo/validator'\n\nvalidator.validate([\n  {\n    dataKey: 'name',\n    value: 'xuanmo',\n    rules: 'length:5|between:2,8'\n  },\n  {\n    dataKey: 'scope',\n    value: 'xuanmo',\n    // 局部校验规则，优先级最高，不会执行 rules 模式\n    validator(value) {\n      return false\n    }\n  }\n])\n```\n\n#### Rule 结构说明\n\n|键名|说明|类型|\n|---|---|---|\n|value|(必须)当前被校验的数据|`unknown`|\n|required|(非必须)是否必填，与 `rules` 中的 `required` 等价|`boolean`|\n|rules|(非必须)校验规则，以管道符分隔，冒号后边的为校验规则值|`string`|\n|label|(非必须)用于覆盖 `message {#field}` 标识|`string`|\n|regexp|(非必须)正则校验，一般只有全局中无匹配规则时使用|`RegExp \\| string`|\n|matrix|(非必须)是否为矩阵数据|`boolean`|\n|matrixId|(非必须)矩阵 id|`string`|\n|rowId|(非必须)数据每行对应的 id|`string`|\n|message|(非必须)校验错误提示信息，权重最高，一般不使用|`string`|\n|validator|(非必须)校验函数权重高于 `rules`，一般只有全局中无匹配规则时使用|`ValidatorHandlerType`|\n\n#### TS 类型\n\n```typescript\n/**\n * 执行校验\n * @param data 校验数据\n * @param options\n */\ntype ValidateType = (\n  data: ValidateDataModel,\n  options?: {\n    // 是否执行全部规则的校验，默认为 true，如果字段较多，存在多个异步校验，不建议开启\n    checkAll?: boolean;\n  }\n) =\u003e ValidateReturnType;\n```\n\n#### 校验权重说明\n\n\u003e required \u003e regexp \u003e validator \u003e rules\n\n#### message 字段权重说明\n\n\u003e validate.field.message \u003e rule.message\n\n### localize 国际化词条注册\n\n```typescript\nimport zhCN from '@xuanmo/validator/locale/zh-CN.json'\n\nvalidator.localize(zhCN)\n```\n\n### 注册校验规则\n\n#### extend 单个注册\n\n```typescript\nvalidator.extend('custom', {\n  message: '{#field}自定义校验规则失败：{custom}',\n  validator(value, ruleValue) {\n    return (value as string).length \u003e +ruleValue!\n  }\n})\n```\n\n#### extends 多个规则同时注册\n\n```typescript\nvalidator.extends({\n  regexp: {\n    message: '正则校验失败信息',\n    regexp: '\\\\d+'\n  },\n  custom: {\n    message: '{#field}自定义校验规则失败：{custom}',\n    // 可返回 Promise\u003cboolean\u003e\n    validator(value, ruleValue) {\n      return (value as string).length \u003e +ruleValue!\n    }\n  }\n})\n```\n\n### rules 内置规则\n\n\u003e 更多规则可以从 `@xuanmo/validator/rules/{type}/*.js` 导出进行注册，默认不打包\n\n- `required` 必填校验\n- `email` 邮箱\n- `length` 长度，示例：`length:2`\n- `min_length` 最小长度，示例：`min_length:2`\n- `max_length` 最大长度，示例：`max_length:2`\n- `min` 最小值，示例：`min:5`\n- `max` 最大值，示例：`max:5`\n- `number` 数字，包含浮点数、整数\n- `float` 浮点数\n- `integer` 整数\n- `between` 数值区间\n- `confirmed` 二次确认，如密码场景，示例：`confirmed:@target`\n- `alpha` 只能包含字母\n- `alpha_num` 只能包含字母数字\n- `alpha_spaces` 只能包含字母空格\n- `url` URL\n\n## 单例模式\n\n```typescript\nimport { Validator } from '@xuanmo/validator'\nimport zhCN from '@xuanmo/validator/locale/zh-CN.json'\n\nconst validator = new Validator()\nvalidator.localize(zhCN)\n// ...\n```\n\n## 鸣谢\n\n\u003e Validator 的成长，离不开前辈的作品\n\n- 感谢 [vee-validate](https://github.com/logaretm/vee-validate) 提供的管道符校验模式\n\n## License\n\n- 本项目基于 [MIT](./LICENSE) 协议，欢迎有兴趣的朋友一起交流\n- Copyright © 2023-PRESENT [D-Xuanmo](https://github.com/D-xuanmo)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-xuanmo%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd-xuanmo%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-xuanmo%2Fvalidator/lists"}