Skip to content

Commit e12fac4

Browse files
authored
feat: add global addRule method (#46)
1 parent d0d7121 commit e12fac4

4 files changed

Lines changed: 93 additions & 45 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: node_js
33
notifications:
44
email: false
55
node_js:
6+
- '10'
67
- '8'
78
- '6'
89
- '4'

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This software is licensed under the MIT License.
22

3-
Copyright(c) 2013 - 2017 node-modules and other contributors.
3+
Copyright(c) 2013 - 2018 node-modules and other contributors.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

index.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Parameter {
5858

5959
if (this.validateRoot && (typeof obj !== 'object' || !obj)) {
6060
return [{
61-
message: 'the validated value should be a object',
61+
message: this.t('the validated value should be a object'),
6262
code: this.t('invalid'),
6363
field: undefined,
6464
}];
@@ -111,34 +111,6 @@ class Parameter {
111111
return errors;
112112
}
113113
}
114-
115-
/**
116-
* add custom rule
117-
*
118-
* @param {String} type
119-
* @param {Function | RegExp} check
120-
* @api public
121-
*/
122-
123-
addRule(type, check) {
124-
if (!type) {
125-
throw new TypeError('`type` required');
126-
}
127-
128-
if (typeof check === 'function') {
129-
TYPE_MAP[type] = check;
130-
return;
131-
}
132-
133-
if (check instanceof RegExp) {
134-
TYPE_MAP[type] = function (rule, value) {
135-
return checkString.call(this, {format: check}, value);
136-
};
137-
return;
138-
}
139-
140-
throw new TypeError('check must be function or regexp');
141-
}
142114
};
143115

144116
/**
@@ -147,6 +119,41 @@ class Parameter {
147119
*/
148120
module.exports = Parameter;
149121

122+
/**
123+
* add custom rule to global rules list.
124+
*
125+
* @param {String} type
126+
* @param {Function | RegExp} check
127+
* @param {Boolean} [override] - override exists rule or not, default is true
128+
* @api public
129+
*/
130+
Parameter.prototype.addRule = Parameter.addRule = function addRule(type, check, override) {
131+
if (!type) {
132+
throw new TypeError('`type` required');
133+
}
134+
135+
if (typeof override !== 'boolean') {
136+
override = true;
137+
}
138+
139+
if (!override && TYPE_MAP[type]) {
140+
throw new TypeError('rule `' + type + '` exists');
141+
}
142+
143+
if (typeof check === 'function') {
144+
TYPE_MAP[type] = check;
145+
return;
146+
}
147+
148+
if (check instanceof RegExp) {
149+
TYPE_MAP[type] = function (rule, value) {
150+
return checkString.call(this, {format: check}, value);
151+
};
152+
return;
153+
}
154+
155+
throw new TypeError('check must be function or regexp');
156+
};
150157

151158
/**
152159
* Simple type map
@@ -299,7 +306,7 @@ function checkString(rule, value) {
299306
*/
300307

301308
function checkId(rule, value) {
302-
return checkString.call(this, {format: ID_RE, allowEmpty: rule.allowEmpty}, value);
309+
return checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value);
303310
}
304311

305312
/**
@@ -313,7 +320,7 @@ function checkId(rule, value) {
313320
*/
314321

315322
function checkDate(rule, value) {
316-
return checkString.call(this, {format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty}, value);
323+
return checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
317324
}
318325

319326
/**
@@ -327,7 +334,7 @@ function checkDate(rule, value) {
327334
*/
328335

329336
function checkDateTime(rule, value) {
330-
return checkString.call(this, {format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty}, value);
337+
return checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
331338
}
332339

333340
/**

test/index.test.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
/**
2-
* Copyright(c) node-modules and other contributors.
3-
* MIT Licensed
4-
*
5-
* Authors:
6-
* dead_horse <dead_horse@qq.com>
7-
*/
8-
91
'use strict';
102

11-
/**
12-
* Module dependencies.
13-
*/
14-
153
var should = require('should');
164
var util = require('util');
175
var Parameter = require('..');
@@ -350,6 +338,14 @@ describe('parameter', function () {
350338
message: 'should be an email'
351339
}
352340
]);
341+
342+
parameter.validate({ name: { type: 'email', message: '错误 email' } }, { name: email }).should.eql([
343+
{
344+
code: 'invalid',
345+
field: 'name',
346+
message: '错误 email'
347+
}
348+
]);
353349
});
354350
});
355351
});
@@ -407,6 +403,23 @@ describe('parameter', function () {
407403
message: 'length should bigger than 6'
408404
}
409405
]);
406+
407+
parameter.validate({
408+
password: {
409+
type: 'password',
410+
min: 4,
411+
compare: 're-password'
412+
}
413+
}, {
414+
password: '1',
415+
're-password': '1',
416+
}).should.eql([
417+
{
418+
code: 'invalid',
419+
field: 'password',
420+
message: 'length should bigger than 4'
421+
}
422+
]);
410423
});
411424
});
412425

@@ -475,6 +488,14 @@ describe('parameter', function () {
475488
message: 'should be a url'
476489
}
477490
]);
491+
492+
parameter.validate({ name: { type: 'url', message: '不合法 url' } }, { name: url }).should.eql([
493+
{
494+
code: 'invalid',
495+
field: 'name',
496+
message: '不合法 url'
497+
}
498+
]);
478499
});
479500
});
480501
});
@@ -632,6 +653,18 @@ describe('parameter', function () {
632653
(function () {
633654
parameter.addRule();
634655
}).should.throw('`type` required');
656+
(function () {
657+
Parameter.addRule();
658+
}).should.throw('`type` required');
659+
});
660+
661+
it('should throw error when override exists rule', function () {
662+
(function () {
663+
parameter.addRule('string', function() {}, false);
664+
}).should.throw('rule `string` exists');
665+
(function () {
666+
Parameter.addRule('string', function() {}, false);
667+
}).should.throw('rule `string` exists');
635668
});
636669

637670
it('should throw without check', function () {
@@ -658,6 +691,13 @@ describe('parameter', function () {
658691
var value = {key: 'not-prefixed'};
659692
parameter.validate(rule, value)[0].message.should.equal('should match /^prefix/');
660693
});
694+
695+
it('should add with regexp on global', function () {
696+
Parameter.addRule('prefix2', /^prefix/);
697+
var rule = {key: 'prefix2'};
698+
var value = {key: 'not-prefixed'};
699+
parameter.validate(rule, value)[0].message.should.equal('should match /^prefix/');
700+
});
661701
});
662702

663703
describe('custom translate function', function(){

0 commit comments

Comments
 (0)