Comparing version 3.6.0 to 3.7.0
@@ -7,2 +7,4 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -58,2 +60,7 @@ | ||
} | ||
}, { | ||
key: 'message', | ||
value: function message(rule, key, defaultMessage) { | ||
return rule.message && rule.message[key] || defaultMessage; | ||
} | ||
@@ -107,3 +114,3 @@ /** | ||
errors.push({ | ||
message: this.t('required'), | ||
message: this.message(rule, 'required', this.t('required')), | ||
field: key, | ||
@@ -276,2 +283,7 @@ code: this.t('missing_field') | ||
rule.message = rule.message || {}; | ||
if (typeof rule.message === 'string') { | ||
rule.message = _defineProperty({}, rule.type, rule.message); | ||
} | ||
return rule; | ||
@@ -340,11 +352,11 @@ } | ||
if (typeof value !== 'number' || value % 1 !== 0) { | ||
return this.t('should be an integer'); | ||
return this.message(rule, 'int', this.t('should be an integer')); | ||
} | ||
if (rule.hasOwnProperty('max') && value > rule.max) { | ||
return this.t('should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value < rule.min) { | ||
return this.t('should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('should bigger than %s', rule.min)); | ||
} | ||
@@ -368,9 +380,9 @@ } | ||
if (typeof value !== 'number' || isNaN(value)) { | ||
return this.t('should be a number'); | ||
return this.message(rule, 'number', this.t('should be a number')); | ||
} | ||
if (rule.hasOwnProperty('max') && value > rule.max) { | ||
return this.t('should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value < rule.min) { | ||
return this.t('should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('should bigger than %s', rule.min)); | ||
} | ||
@@ -397,3 +409,3 @@ } | ||
if (typeof value !== 'string') { | ||
return this.t('should be a string'); | ||
return this.message(rule, rule.type || 'string', this.t('should be a string')); | ||
} | ||
@@ -410,14 +422,14 @@ | ||
if (allowEmpty) return; | ||
return this.t('should not be empty'); | ||
return this.message(rule, 'allowEmpty', '') || this.message(rule, 'empty', '') || this.t('should not be empty'); | ||
} | ||
if (rule.hasOwnProperty('max') && value.length > rule.max) { | ||
return this.t('length should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value.length < rule.min) { | ||
return this.t('length should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min)); | ||
} | ||
if (rule.format && !rule.format.test(value)) { | ||
return rule.message || this.t('should match %s', rule.format); | ||
return this.message(rule, 'format', this.t('should match %s', rule.format)); | ||
} | ||
@@ -437,3 +449,6 @@ } | ||
function checkId(rule, value) { | ||
return checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value); | ||
var errorMessage = checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'id', errorMessage); | ||
} | ||
} | ||
@@ -452,3 +467,6 @@ | ||
function checkDate(rule, value) { | ||
return checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
var errorMessage = checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'date', errorMessage); | ||
} | ||
} | ||
@@ -467,3 +485,6 @@ | ||
function checkDateTime(rule, value) { | ||
return checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
var errorMessage = checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'dateTime', errorMessage); | ||
} | ||
} | ||
@@ -482,3 +503,3 @@ | ||
if (typeof value !== 'boolean') { | ||
return this.t('should be a boolean'); | ||
return this.message(rule, 'bool', '') || this.message(rule, 'boolean', '') || this.t('should be a boolean'); | ||
} | ||
@@ -504,3 +525,3 @@ } | ||
if (rule.values.indexOf(value) === -1) { | ||
return this.t('should be one of %s', rule.values.join(', ')); | ||
return this.message(rule, 'enum', this.t('should be one of %s', rule.values.join(', '))); | ||
} | ||
@@ -519,7 +540,9 @@ } | ||
function checkEmail(rule, value) { | ||
return checkString.call(this, { | ||
var errorMessage = checkString.call(this, { | ||
format: EMAIL_RE, | ||
message: rule.message || this.t('should be an email'), | ||
allowEmpty: rule.allowEmpty | ||
}, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'email', this.t('should be an email')); | ||
} | ||
} | ||
@@ -544,6 +567,6 @@ | ||
if (error) { | ||
return error; | ||
return this.message(rule, 'password', error); | ||
} | ||
if (rule.compare && obj[rule.compare] !== value) { | ||
return this.t('should equal to %s', rule.compare); | ||
return this.message(rule, 'compare', this.t('should equal to %s', rule.compare)); | ||
} | ||
@@ -562,7 +585,9 @@ } | ||
function checkUrl(rule, value) { | ||
return checkString.call(this, { | ||
var error = checkString.call(this, { | ||
format: URL_RE, | ||
message: rule.message || this.t('should be a url'), | ||
allowEmpty: rule.allowEmpty | ||
}, value); | ||
if (error) { | ||
return this.message(rule, 'url', this.t('should be a url')); | ||
} | ||
} | ||
@@ -584,3 +609,3 @@ | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object') { | ||
return this.t('should be an object'); | ||
return this.message(rule, 'object', this.t('should be an object')); | ||
} | ||
@@ -617,10 +642,10 @@ | ||
if (!Array.isArray(value)) { | ||
return this.t('should be an array'); | ||
return this.message(rule, 'array', this.t('should be an array')); | ||
} | ||
if (rule.hasOwnProperty('max') && value.length > rule.max) { | ||
return this.t('length should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value.length < rule.min) { | ||
return this.t('length should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min)); | ||
} | ||
@@ -627,0 +652,0 @@ |
78
index.js
@@ -47,2 +47,6 @@ 'use strict'; | ||
message(rule, key, defaultMessage) { | ||
return rule.message && rule.message[key] || defaultMessage; | ||
} | ||
/** | ||
@@ -93,3 +97,3 @@ * validate | ||
errors.push({ | ||
message: this.t('required'), | ||
message: this.message(rule, 'required', this.t('required')), | ||
field: key, | ||
@@ -259,2 +263,9 @@ code: this.t('missing_field') | ||
rule.message = rule.message || {} | ||
if (typeof rule.message === 'string') { | ||
rule.message = { | ||
[rule.type]: rule.message, | ||
} | ||
} | ||
return rule; | ||
@@ -323,11 +334,11 @@ } | ||
if (typeof value !== 'number' || value % 1 !== 0) { | ||
return this.t('should be an integer'); | ||
return this.message(rule, 'int', this.t('should be an integer')); | ||
} | ||
if (rule.hasOwnProperty('max') && value > rule.max) { | ||
return this.t('should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value < rule.min) { | ||
return this.t('should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('should bigger than %s', rule.min)); | ||
} | ||
@@ -351,9 +362,9 @@ } | ||
if (typeof value !== 'number' || isNaN(value)) { | ||
return this.t('should be a number'); | ||
return this.message(rule, 'number', this.t('should be a number')); | ||
} | ||
if (rule.hasOwnProperty('max') && value > rule.max) { | ||
return this.t('should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value < rule.min) { | ||
return this.t('should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('should bigger than %s', rule.min)); | ||
} | ||
@@ -380,3 +391,3 @@ } | ||
if (typeof value !== 'string') { | ||
return this.t('should be a string'); | ||
return this.message(rule, rule.type || 'string', this.t('should be a string')); | ||
} | ||
@@ -395,14 +406,14 @@ | ||
if (allowEmpty) return; | ||
return this.t('should not be empty'); | ||
return this.message(rule, 'allowEmpty', '') || this.message(rule, 'empty', '') || this.t('should not be empty'); | ||
} | ||
if (rule.hasOwnProperty('max') && value.length > rule.max) { | ||
return this.t('length should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value.length < rule.min) { | ||
return this.t('length should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min)); | ||
} | ||
if (rule.format && !rule.format.test(value)) { | ||
return rule.message || this.t('should match %s', rule.format); | ||
return this.message(rule, 'format', this.t('should match %s', rule.format)); | ||
} | ||
@@ -422,3 +433,6 @@ } | ||
function checkId(rule, value) { | ||
return checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value); | ||
const errorMessage = checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'id', errorMessage); | ||
} | ||
} | ||
@@ -437,3 +451,6 @@ | ||
function checkDate(rule, value) { | ||
return checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
const errorMessage = checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'date', errorMessage); | ||
} | ||
} | ||
@@ -452,3 +469,6 @@ | ||
function checkDateTime(rule, value) { | ||
return checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
const errorMessage = checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'dateTime', errorMessage); | ||
} | ||
} | ||
@@ -467,3 +487,3 @@ | ||
if (typeof value !== 'boolean') { | ||
return this.t('should be a boolean'); | ||
return this.message(rule, 'bool', '') || this.message(rule, 'boolean', '') || this.t('should be a boolean'); | ||
} | ||
@@ -489,3 +509,3 @@ } | ||
if (rule.values.indexOf(value) === -1) { | ||
return this.t('should be one of %s', rule.values.join(', ')); | ||
return this.message(rule, 'enum', this.t('should be one of %s', rule.values.join(', '))); | ||
} | ||
@@ -504,7 +524,9 @@ } | ||
function checkEmail(rule, value) { | ||
return checkString.call(this, { | ||
const errorMessage = checkString.call(this, { | ||
format: EMAIL_RE, | ||
message: rule.message || this.t('should be an email'), | ||
allowEmpty: rule.allowEmpty, | ||
}, value); | ||
if (errorMessage) { | ||
return this.message(rule, 'email', this.t('should be an email')); | ||
} | ||
} | ||
@@ -529,6 +551,6 @@ | ||
if (error) { | ||
return error; | ||
return this.message(rule, 'password', error); | ||
} | ||
if (rule.compare && obj[rule.compare] !== value) { | ||
return this.t('should equal to %s', rule.compare); | ||
return this.message(rule, 'compare', this.t('should equal to %s', rule.compare)); | ||
} | ||
@@ -547,7 +569,9 @@ } | ||
function checkUrl(rule, value) { | ||
return checkString.call(this, { | ||
const error = checkString.call(this, { | ||
format: URL_RE, | ||
message: rule.message || this.t('should be a url'), | ||
allowEmpty: rule.allowEmpty | ||
}, value); | ||
if (error) { | ||
return this.message(rule, 'url', this.t('should be a url')); | ||
} | ||
} | ||
@@ -569,3 +593,3 @@ | ||
if (typeof value !== 'object') { | ||
return this.t('should be an object'); | ||
return this.message(rule, 'object', this.t('should be an object')); | ||
} | ||
@@ -602,10 +626,10 @@ | ||
if (!Array.isArray(value)) { | ||
return this.t('should be an array'); | ||
return this.message(rule, 'array', this.t('should be an array')); | ||
} | ||
if (rule.hasOwnProperty('max') && value.length > rule.max) { | ||
return this.t('length should smaller than %s', rule.max); | ||
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max)); | ||
} | ||
if (rule.hasOwnProperty('min') && value.length < rule.min) { | ||
return this.t('length should bigger than %s', rule.min); | ||
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min)); | ||
} | ||
@@ -612,0 +636,0 @@ |
This software is licensed under the MIT License. | ||
Copyright(c) 2013 - 2018 node-modules and other contributors. | ||
Copyright(c) 2013 - present node-modules and other contributors. | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "parameter", | ||
"version": "3.6.0", | ||
"version": "3.7.0", | ||
"description": "A parameter verify tools.", | ||
@@ -12,8 +12,8 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec -t 1000 test/*.test.js", | ||
"cov": "istanbul cover _mocha -- -t 1000 test/*.test.js", | ||
"lint": "echo 'ignore'", | ||
"test": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run cov", | ||
"build:es5": "babel index.js --presets babel-preset-es2015 -o index.es5.js", | ||
"prepublish": "npm run build:es5", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
"prepublish": "npm run build:es5" | ||
}, | ||
@@ -25,5 +25,3 @@ "devDependencies": { | ||
"benchmark": "*", | ||
"istanbul": "*", | ||
"mocha": "^5.2.0", | ||
"semantic-release": "^6.3.6", | ||
"egg-bin": "^4.19.0", | ||
"should": "*" | ||
@@ -41,3 +39,3 @@ }, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
"node": ">= 10.0.0" | ||
}, | ||
@@ -44,0 +42,0 @@ "author": "fengmk2 <fengmk2@gmail.com>", |
@@ -5,11 +5,8 @@ parameter | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![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) | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![npm download][download-image]][download-url] | ||
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release) | ||
[npm-image]: https://img.shields.io/npm/v/parameter.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/parameter | ||
[travis-image]: https://img.shields.io/travis/node-modules/parameter.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/node-modules/parameter | ||
[codecov-image]: https://codecov.io/github/node-modules/parameter/coverage.svg?branch=master | ||
@@ -38,3 +35,3 @@ [codecov-url]: https://codecov.io/github/node-modules/parameter?branch=master | ||
- `options.convert` - convert primitive params to specific type, default to `false`. | ||
- `optinos.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__. | ||
- `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__. | ||
- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule. | ||
@@ -200,4 +197,4 @@ - `addRule(type, check)` - add custom rules. | ||
- `rule` - An object that validate the items of the array. Only work with `itemType`. | ||
- `max` - The maximun length of the array. | ||
- `min` - The minimun lenght of the array. | ||
- `max` - The maximum length of the array. | ||
- `min` - The minimum lenght of the array. | ||
@@ -246,11 +243,16 @@ #### abbr | ||
### Release process | ||
## License | ||
We're using [semantic-release](https://github.com/semantic-release/semantic-release) to run npm publish | ||
after every commit on master. | ||
[MIT](LICENSE.txt) | ||
See [Default Commit Message Format](https://github.com/semantic-release/semantic-release#default-commit-message-format) for details. | ||
<!-- GITCONTRIBUTOR_START --> | ||
## License | ||
## Contributors | ||
[MIT](LICENSE.txt) | ||
|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/5518?v=4" width="100px;"/><br/><sub><b>huacnlee</b></sub>](https://github.com/huacnlee)<br/>|[<img src="https://avatars.githubusercontent.com/u/143572?v=4" width="100px;"/><br/><sub><b>hotoo</b></sub>](https://github.com/hotoo)<br/>|[<img src="https://avatars.githubusercontent.com/u/2039144?v=4" width="100px;"/><br/><sub><b>sang4lv</b></sub>](https://github.com/sang4lv)<br/>|[<img src="https://avatars.githubusercontent.com/u/471928?v=4" width="100px;"/><br/><sub><b>ghostoy</b></sub>](https://github.com/ghostoy)<br/>| | ||
| :---: | :---: | :---: | :---: | :---: | :---: | | ||
[<img src="https://avatars.githubusercontent.com/u/12657964?v=4" width="100px;"/><br/><sub><b>beliefgp</b></sub>](https://github.com/beliefgp)<br/>|[<img src="https://avatars.githubusercontent.com/u/5825244?v=4" width="100px;"/><br/><sub><b>taylorharwin</b></sub>](https://github.com/taylorharwin)<br/>|[<img src="https://avatars.githubusercontent.com/u/3199140?v=4" width="100px;"/><br/><sub><b>tomowang</b></sub>](https://github.com/tomowang)<br/>|[<img src="https://avatars.githubusercontent.com/u/11374721?v=4" width="100px;"/><br/><sub><b>hdumok</b></sub>](https://github.com/hdumok)<br/>|[<img src="https://avatars.githubusercontent.com/u/7971415?v=4" width="100px;"/><br/><sub><b>paranoidjk</b></sub>](https://github.com/paranoidjk)<br/>|[<img src="https://avatars.githubusercontent.com/u/30565051?v=4" width="100px;"/><br/><sub><b>zcxsythenew</b></sub>](https://github.com/zcxsythenew)<br/> | ||
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Apr 05 2022 10:44:22 GMT+0800`. | ||
<!-- GITCONTRIBUTOR_END --> |
6
1150
255
43978
5