Skip to content

Commit a6a5075

Browse files
author
不四
committed
feat: support validateWithTranslate
1 parent db5030c commit a6a5075

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ $ npm install parameter --save
3232
- `constructor([options])` - new Class `Parameter` instance
3333
- `options.translate` - translate function
3434
- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule.
35+
- `validateWithTranslate(rule, value, translate)` - validate the `value` conforms to `rule`, with customize translate method.
3536
- `addRule(type, check)` - add custom rules.
3637
- `type` - rule type, required and must be string type.
3738
- `check` - check handler. can be a `function` or a `RegExp`.

index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,34 @@ class Parameter {
4949
}
5050
}
5151

52+
/**
53+
* validate with customize translate
54+
*
55+
* @param {Object} rules
56+
* @param {Mixed} obj
57+
* @param {Function} translate
58+
* @return {Object} errors
59+
* @api public
60+
*/
61+
validateWithTranslate(rules, obj, translate) {
62+
var _translate = this.translate;
63+
this.translate = translate;
64+
try {
65+
return this.validate(rules, obj);
66+
} finally {
67+
this.translate = _translate;
68+
}
69+
};
70+
5271
/**
5372
* validate
5473
*
5574
* @param {Object} rules
56-
* @return {Object} obj
75+
* @param {Mixed} obj
76+
* @return {Object} errors
5777
* @api public
5878
*/
79+
5980
validate(rules, obj) {
6081
if (typeof rules !== 'object') {
6182
throw new TypeError('need object type rule');

test/index.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,40 @@ describe('parameter', function () {
594594
error.field.should.equal('name');
595595
});
596596
});
597+
598+
describe('validateWithTranslate()', function() {
599+
it('should work', function() {
600+
var translate = function() {
601+
var args = Array.prototype.slice.call(arguments);
602+
args[0] = args[0] + '-add.';
603+
return util.format.apply(util, args);
604+
};
605+
606+
var p1 = new Parameter();
607+
608+
var rule = { name: 'string' };
609+
var error = p1.validateWithTranslate(rule, {}, translate)[0];
610+
error.message.should.equal('required-add.');
611+
error.code.should.equal('missing_field-add.');
612+
error.field.should.equal('name');
613+
});
614+
615+
it('should reset translate', function(done) {
616+
var translate = function() {
617+
var args = Array.prototype.slice.call(arguments);
618+
args[0] = args[0] + '-add.';
619+
return util.format.apply(util, args);
620+
};
621+
622+
var p1 = new Parameter();
623+
624+
var rule = { name: 'invalid' };
625+
try {
626+
p1.validateWithTranslate(rule, {name: 1}, translate);
627+
} catch (e) {
628+
should.not.exist(p1.translate);
629+
done();
630+
}
631+
});
632+
});
597633
});

0 commit comments

Comments
 (0)