{"id":19849640,"url":"https://github.com/ckpack/parameter","last_synced_at":"2025-10-26T17:03:16.828Z","repository":{"id":57102341,"uuid":"342151773","full_name":"ckpack/parameter","owner":"ckpack","description":"JSON validator for Node.js and browser by using simple configuration rule","archived":false,"fork":false,"pushed_at":"2022-04-01T03:48:27.000Z","size":587,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-06T19:08:21.305Z","etag":null,"topics":["parameter","verify"],"latest_commit_sha":null,"homepage":"https://ckpack.github.io/parameter/","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/ckpack.png","metadata":{"files":{"readme":"README-ZH.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-25T06:55:05.000Z","updated_at":"2022-01-24T07:36:16.000Z","dependencies_parsed_at":"2022-08-20T22:10:23.893Z","dependency_job_id":null,"html_url":"https://github.com/ckpack/parameter","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckpack%2Fparameter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckpack%2Fparameter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckpack%2Fparameter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckpack%2Fparameter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ckpack","download_url":"https://codeload.github.com/ckpack/parameter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251654549,"owners_count":21622336,"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":["parameter","verify"],"created_at":"2024-11-12T13:22:22.417Z","updated_at":"2025-10-26T17:03:11.786Z","avatar_url":"https://github.com/ckpack.png","language":"TypeScript","readme":"# Parameter\n\n通过简单的配置验证JSON数据\n\n## API\n\n### Class\n\n`Parameter`\n\n+ `constructor([options])` - `Parameter` 构造函数\n  + `options.isUseDefault` - 是否使用默认值, 默认值: `true`\n  + `options.isRemoveAdditional` - 是否删除未在规则中定义的参数, 默认值: `false`\n  + `options.isCoerceTypes` - 是否开启参数类型转换, 默认值: `false`\n  + `options.emptyValues` - 包含在内的值将被视为空值,此时如果开启`isUseDefault`, 参数会被设置为对应值, 默认值:`[null, undefined, NaN, '']`\n+ `schema(rule, [options])` - 返回一个具有固定参数的validate\n+ `validate(rule, value, [options])` - 验证 `value` 是否符合 `rule`。 如果违反规则，则返回错误数组\n  + `rule` -  规则\n  + `value` - 待验证的JSON参数\n  + `options` - 可选参数，可用于覆盖构造函数中的参数\n+ `addRule(type, checkFunction)` - 添加自定义验证规则\n  + `type` - 自定义验证规则类型, 必须为字符串类型\n  + `checkFunction(rule, value)` - 自定义验证规则函数该函数接受两个参数，第一个参数为规则，第二个参数为值\n\n```js\nimport { Parameter } from '@ckpack/parameter';\n\nconst parameter = new Parameter();\n\n// check rule\nconst rule = {\n  isAdmin: {\n    type: 'boolean',\n  },\n  age: {\n    type: 'int',\n    min: 0,\n  },\n  role: {\n    type: 'enum',\n    enum: ['am', 'pm'],\n  }\n  ids: {\n    type: 'array',\n    itemType: 'int',\n    itemRule: {\n      min: 1\n    }\n  }\n}\n\n// check value\nconst data = {\n  isAdmin: true,\n  age: 18,\n  role: 'am',\n  ids: [1, 2, 3]\n};\n\nconst errors = parameter.validate(rule, data);\n```\n\n### Function\n\n`setLocale` 设置错误信息语言\n\n设置默认语言为中文\n\n```ts\nimport { zhLocale, setLocale, Parameter } from '@ckpack/parameter';\n\nsetLocale(zhLocale);\n\nconst parameter = new Parameter();\n\n// parameter.validate(rule, data);\n```\n\n## RULE\n\n### common rules\n\n+ `type`: 规则类型\n+ `message`: 自定义错误信息\n+ `isRequired`: 是否检查空值, 默认值: `true`\n+ `default`: 是`isUseDefault`是`true`，空值会设置默认值，默认: `undefined`\n\n### int\n\n如果 type 为 `int`，则有以下选项规则\n\n+ `max` - 值的最大值，值必须 \u003c= `max`\n+ `min` - 值的最小值，值必须 \u003e= `min`\n\n```js\n{\n  score: 'int',\n}\n// or\n{\n  score: {\n    type: 'int',\n    min: 0,\n    max: 200,\n  }\n}\n```\n\n### number\n\n如果 type 为 `number`，则有以下选项规则\n\n+ `max` - 值的最大值，值必须 \u003c= `max`\n+ `min` - 值的最小值，值必须 \u003e= `min`\n\nexample\n\n```js\n{\n  score: 'number',\n}\n// or\n{\n  score: {\n    type: 'number',\n    min: 0,\n    max: 100,\n  }\n}\n```\n\n### string\n\n如果 type 为 `string`，则有以下选项规则\n\n+ `regexp` - 检查字符串格式的正则表达式\n+ `max` - 字符串的最大长度\n+ `min` - 字符串的最小长度\n\nexample\n\n```js\n{\n  username: 'string',\n}\n// or\n{\n  username: /\\S{4,20}/,\n}\n// or\n{\n  username: {\n    type: 'string',\n    regexp: /\\S{4,20}/\n  }\n}\n```\n\n### boolean\n\n如果类型是 `boolean`\n\nexample\n\n```js\n{\n  isAll: 'boolean',\n}\n// or\n{\n  isAll: {\n    type: 'boolean',\n  }\n}\n```\n\n### array\n\n如果 type 为 `array`，则有以下选项规则\n\n+ `itemType` - 数组中每个子项目的类型\n+ `itemRule` - 数组中每个子项目的规则\n+ `itemChecker`- 每个项目的检查器，在这种情况下你可以省略 `itemType` 和 `itemRule`\n+ `max` - 数组的最大长度\n+ `min` - 数组的最小长度\n\nexample\n\n```js\n{\n  ids: {\n    itemType: 'int',\n    itemRule: {\n      min: 1,\n      max: 1000,\n    },\n    min: 0,\n    max: 100,\n  }\n}\n```\n\n### enum\n\n如果 type 为 `enum`，则有以下规则\n\n+ `enum`- 一个数据数组，值必须是其中一个\n\nexample\n\n```js\n{\n  sex: ['man', 'woman']\n}\n// or\n{\n  sex: {\n    type: 'enum'\n    enum: ['man', 'woman']\n  }\n}\n```\n\n### object\n\n如果type是`object`，有以下规则\n\n+ `rule`- 验证对象属性的对象\n\nexample\n\n```js\n{\n  people: {\n    type: 'object',\n    rule: {\n      name: 'string',\n      age: {\n        isRequired: false,\n        type: 'int',\n        min: 1,\n        max: 200\n      }\n    }\n  }\n}\n```\n\n### custom rule\n\n定义自定义规则，\n\n示例\n\n```js\n// custom check\nparameter.addRule('even', (rule, value) =\u003e {\n  return value % 2 === 0 ? null : `${value} is not even`;\n});\n```\n\n使用自定义规则\n\n```js\n{\n  someNumber: 'even'\n}\n//or\n{\n  someNumber: {\n    type: \"even\",\n  }\n}\n```\n\n您也可以添加选项规则\n\n```js\nimport { Parameter } from '@ckpack/parameter';\nconst parameter = new Parameter();\nparameter.addRule('times', (rule, value) =\u003e {\n  const { times } = rule;\n  return value % times === 0 ? null : `not an integer multiple of ${times}`;\n});\n\n// rule\n{\n  someNumber: {\n    type: \"times\",\n    times: 3,\n  }\n}\n```\n\n[其他用例](/__test__/parameter.test.ts)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckpack%2Fparameter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fckpack%2Fparameter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckpack%2Fparameter/lists"}