{"id":13526701,"url":"https://github.com/node-modules/parameter","last_synced_at":"2025-06-14T03:08:02.560Z","repository":{"id":9163530,"uuid":"10960377","full_name":"node-modules/parameter","owner":"node-modules","description":"A parameter verify tools.","archived":false,"fork":false,"pushed_at":"2023-03-14T10:06:36.000Z","size":144,"stargazers_count":728,"open_issues_count":34,"forks_count":117,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-03-13T22:09:05.112Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-modules.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2013-06-26T06:36:32.000Z","updated_at":"2025-02-11T02:44:54.000Z","dependencies_parsed_at":"2023-01-13T15:12:06.471Z","dependency_job_id":"c6f52f98-d2c1-4e45-8b06-46bd3535df0d","html_url":"https://github.com/node-modules/parameter","commit_stats":{"total_commits":91,"total_committers":18,"mean_commits":5.055555555555555,"dds":0.5934065934065934,"last_synced_commit":"a63a613604e8750cb727a7b78c5d915882ee326d"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fparameter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fparameter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fparameter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fparameter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-modules","download_url":"https://codeload.github.com/node-modules/parameter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246600934,"owners_count":20803519,"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":[],"created_at":"2024-08-01T06:01:33.407Z","updated_at":"2025-04-01T07:33:25.278Z","avatar_url":"https://github.com/node-modules.png","language":"JavaScript","readme":"parameter\n=======\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/node-modules/parameter/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/parameter/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/parameter.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/parameter\n[codecov-image]: https://codecov.io/github/node-modules/parameter/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/node-modules/parameter?branch=master\n[download-image]: https://img.shields.io/npm/dm/parameter.svg?style=flat-square\n[download-url]: https://npmjs.org/package/parameter\n\nA parameter verify tools.\n\n## Install\n\n```bash\n$ npm install parameter --save\n```\n\n## Usage\n\n### API\n\n`Parameter` Class\n\n- `constructor([options])` - new Class `Parameter` instance\n  - `options.translate` - translate function\n  - `options.validateRoot` - config whether to validate the passed in value must be a object, default to `false`.\n  - `options.convert` - convert primitive params to specific type, default to `false`.\n  - `options.widelyUndefined` - convert empty string(`''`), NaN, Null to undefined, this option can make `rule.required` more powerful, default to `false`.__This may change the original input params__.\n- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule.\n- `addRule(type, check)` - add custom rules.\n   - `type` - rule type, required and must be string type.\n   - `check` - check handler. can be a `function` or a `RegExp`.\n\n__Note: when `options.convert` enabled, all built-in rules check for primitive input param and convert it to rule's default `convertType`(which defined below), you can also enable this feature for specific rule by `convertType` options in each rule definition.__\n\n### Example\n\n```js\nvar Parameter = require('parameter');\n\nvar parameter = new Parameter({\n  translate: function() {\n    var args = Array.prototype.slice.call(arguments);\n    // Assume there have I18n.t method for convert language.\n    return I18n.t.apply(I18n, args);\n  },\n  validateRoot: true, // restrict the being validate value must be a object\n});\n\nvar data = {\n  name: 'foo',\n  age: 24,\n  gender: 'male'\n};\n\nvar rule = {\n  name: 'string',\n  age: 'int',\n  gender: ['male', 'female', 'unknown']\n};\n\nvar errors = parameter.validate(rule, data);\n```\n\n#### [complex example](example.js)\n\n### Rule\n\n#### common rule\n\n- `required` - if `required` is set to false, this property can be null or undefined. default to `true`.\n- `type` - The type of property, every type has it's own rule for the validate.\n- `convertType` - Make parameter convert the input param to the specific type, support `int`, `number`, `string` and `boolean`, also support a function to customize your own convert method.\n- `default` - The default value of property, once the property is allowed non-required and missed, parameter will use this as the default value. __This may change the original input params__.\n- `widelyUndefined` - override `options.widelyUndefined`\n\n__Note: you can combile require and type end with a notation `?` like: `int?` or `string?` to specific both type and non-required.__\n\n#### int\n\nIf type is `int`, there has tow addition rules:\n\n- `max` - The maximum of the value, `value` must \u003c= `max`.\n- `min` - The minimum of the value, `value` must \u003e= `min`.\n\nDefault `convertType` is `int`.\n\n__Note: default `convertType` will only work when `options.convert` set to true in parameter's constructor.__\n\n#### integer\n\nAlias to `int`.\n\n#### number\n\nIf type is `number`, there has tow addition rules:\n\n- `max` - The maximum of the value, `value` must \u003c= `max`.\n- `min` - The minimum of the value, `value` must \u003e= `min`.\n\nDefault `convertType` is `number`.\n\n#### date\n\nThe `date` type want to match `YYYY-MM-DD` type date string.\n\nDefault `convertType` is `string`.\n\n#### dateTime\n\nThe `dateTime` type want to match `YYYY-MM-DD HH:mm:ss` type date string.\n\nDefault `convertType` is `string`.\n\n#### datetime\n\nAlias to `dateTime`.\n\n#### id\n\nThe `id` type want to match `/^\\d+$/` type date string.\n\nDefault `convertType` is `string`.\n\n#### boolean\n\nMatch `boolean` type value.\n\nDefault `convertType` is `boolean`.\n\n#### bool\n\nAlias to `boolean`\n\n#### string\n\nIf type is `string`, there has four addition rules:\n\n- `allowEmpty`(alias to `empty`) - allow empty string, default to false. If `rule.required` set to false, `allowEmpty` will be set to `true` by default.\n- `format` - A `RegExp` to check string's format.\n- `max` - The maximum length of the string.\n- `min` - The minimum length of the string.\n- `trim` - Trim the string before check, default is `false`.\n\nDefault `convertType` is `string`.\n\n#### email\n\nThe `email` type want to match [RFC 5322](http://tools.ietf.org/html/rfc5322#section-3.4) email address.\n\n- `allowEmpty` - allow empty string, default is false.\n\nDefault `convertType` is `string`.\n\n#### password\n\nThe `password` type want to match `/^$/` type string.\n\n- `compare` - Compare field to check equal, default null, not check.\n- `max` - The maximum length of the password.\n- `min` - The minimum length of the password, default is 6.\n\nDefault `convertType` is `string`.\n\n#### url\n\nThe `url` type want to match [web url](https://gist.github.com/dperini/729294).\n\nDefault `convertType` is `string`.\n\n#### enum\n\nIf type is `enum`, it requires an addition rule:\n\n- `values` - An array of data, `value` must be one on them. ___this rule is required.___\n\n#### object\n\nIf type is `object`, there has one addition rule:\n\n- `rule` - An object that validate the properties ot the object.\n\n#### array\n\nIf type is `array`, there has four addition rule:\n\n- `itemType` - The type of every item in this array.\n- `rule` - An object that validate the items of the array. Only work with `itemType`.\n    ```js\n    /**\n     * check array\n     * {\n     *   type: 'array',\n     *   itemType: 'string'\n     *   rule: {type: 'string', allowEmpty: true}\n     * }\n     *\n     * {\n     *   type: 'array'.\n     *   itemType: 'object',\n     *   rule: {\n     *     name: 'string'\n     *   }\n     * }\n     */\n    ```\n- `max` - The maximum length of the array.\n- `min` - The minimum lenght of the array.\n\n#### abbr\n\n- `'int'` =\u003e `{type: 'int', required: true}`\n- `'int?'` =\u003e `{type: 'int', required: false }`\n- `'integer'` =\u003e `{type: 'integer', required: true}`\n- `'number'` =\u003e `{type: 'number', required: true}`\n- `'date'` =\u003e `{type: 'date', required: true}`\n- `'dateTime'` =\u003e `{type: 'dateTime', required: true}`\n- `'id'` =\u003e `{type: 'id', required: true}`\n- `'boolean'` =\u003e `{type: 'boolean', required: true}`\n- `'bool'` =\u003e `{type: 'bool', required: true}`\n- `'string'` =\u003e `{type: 'string', required: true, allowEmpty: false}`\n- `'string?'` =\u003e `{type: 'string', required: false, allowEmpty: true}`\n- `'email'` =\u003e `{type: 'email', required: true, allowEmpty: false, format: EMAIL_RE}`\n- `'password'` =\u003e `{type: 'password', required: true, allowEmpty: false, format: PASSWORD_RE, min: 6}`\n- `'object'` =\u003e `{type: 'object', required: true}`\n- `'array'` =\u003e `{type: 'array', required: true}`\n- `[1, 2]` =\u003e `{type: 'enum', values: [1, 2]}`\n- `/\\d+/` =\u003e `{type: 'string', required: true, allowEmpty: false, format: /\\d+/}`\n\n### `errors` examples\n\n#### `code: missing_field`\n\n```js\n{\n  code: 'missing_field',\n  field: 'name',\n  message: 'required'\n}\n```\n\n#### `code: invalid`\n\n```js\n{\n  code: 'invalid',\n  field: 'age',\n  message: 'should be an integer'\n}\n```\n\n## License\n\n[MIT](LICENSE.txt)\n\n\u003c!-- GITCONTRIBUTOR_START --\u003e\n\n## Contributors\n\n|[\u003cimg src=\"https://avatars.githubusercontent.com/u/156269?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003efengmk2\u003c/b\u003e\u003c/sub\u003e](https://github.com/fengmk2)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/985607?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003edead-horse\u003c/b\u003e\u003c/sub\u003e](https://github.com/dead-horse)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/5518?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ehuacnlee\u003c/b\u003e\u003c/sub\u003e](https://github.com/huacnlee)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/143572?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ehotoo\u003c/b\u003e\u003c/sub\u003e](https://github.com/hotoo)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/2039144?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003esang4lv\u003c/b\u003e\u003c/sub\u003e](https://github.com/sang4lv)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/471928?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003eghostoy\u003c/b\u003e\u003c/sub\u003e](https://github.com/ghostoy)\u003cbr/\u003e|\n| :---: | :---: | :---: | :---: | :---: | :---: |\n[\u003cimg src=\"https://avatars.githubusercontent.com/u/12657964?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ebeliefgp\u003c/b\u003e\u003c/sub\u003e](https://github.com/beliefgp)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/5825244?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003etaylorharwin\u003c/b\u003e\u003c/sub\u003e](https://github.com/taylorharwin)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/3199140?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003etomowang\u003c/b\u003e\u003c/sub\u003e](https://github.com/tomowang)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/11374721?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ehdumok\u003c/b\u003e\u003c/sub\u003e](https://github.com/hdumok)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/7971415?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003eparanoidjk\u003c/b\u003e\u003c/sub\u003e](https://github.com/paranoidjk)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/30565051?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ezcxsythenew\u003c/b\u003e\u003c/sub\u003e](https://github.com/zcxsythenew)\u003cbr/\u003e\n\nThis project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Apr 05 2022 10:44:22 GMT+0800`.\n\n\u003c!-- GITCONTRIBUTOR_END --\u003e\n","funding_links":[],"categories":["Repository","JavaScript"],"sub_categories":["Data Validation"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fparameter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-modules%2Fparameter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fparameter/lists"}