Skip to content

Commit a5c1721

Browse files
paranoidjkdead-horse
authored andcommitted
feat: Add option.validateRoot to enable validate the root object (#41)
1 parent cef3eaf commit a5c1721

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ $ npm install parameter --save
3232

3333
- `constructor([options])` - new Class `Parameter` instance
3434
- `options.translate` - translate function
35+
- `options.validateRoot` - config whether to validate the passed in value must be a object.
3536
- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule.
3637
- `addRule(type, check)` - add custom rules.
3738
- `type` - rule type, required and must be string type.
@@ -47,7 +48,8 @@ var parameter = new Parameter({
4748
var args = Array.prototype.slice.call(arguments);
4849
// Assume there have I18n.t method for convert language.
4950
return I18n.t.apply(I18n, args);
50-
}
51+
},
52+
validateRoot: true, // restrict the being validate value must be a object
5153
});
5254

5355
var data = {

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class Parameter {
2929
if (typeof opts.translate === 'function') {
3030
this.translate = opts.translate;
3131
}
32+
33+
if (opts.validateRoot) {
34+
this.validateRoot = true;
35+
}
3236
}
3337

3438
t() {
@@ -52,6 +56,14 @@ class Parameter {
5256
throw new TypeError('need object type rule');
5357
}
5458

59+
if (this.validateRoot && (typeof obj !== 'object' || !obj)) {
60+
return [{
61+
message: 'the validated value should be a object',
62+
code: this.t('invalid'),
63+
field: undefined,
64+
}];
65+
}
66+
5567
var self = this;
5668

5769
var errors = [];

test/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var util = require('util');
1717
var Parameter = require('..');
1818
var parameter = new Parameter();
1919

20+
var parameterWithRootValidate = new Parameter({
21+
validateRoot: true,
22+
});
23+
2024
describe('parameter', function () {
2125
describe('required', function () {
2226
it('should required work fine', function () {
@@ -47,6 +51,18 @@ describe('parameter', function () {
4751
});
4852

4953
describe('validate', function () {
54+
it('should throw error when received a non object', function () {
55+
var value = null;
56+
var rule = {int: {type: 'int1', required: false}};
57+
let err;
58+
try {
59+
parameter.validate(rule, undefined)
60+
} catch (e) {
61+
err = e;
62+
}
63+
should(err.message).equal("Cannot read property 'hasOwnProperty' of undefined");
64+
});
65+
5066
it('should invalid type throw', function () {
5167
(function () {
5268
var value = {int: 1.1};
@@ -662,3 +678,12 @@ describe('parameter', function () {
662678
});
663679
});
664680
});
681+
682+
683+
describe('validate with option.validateRoot', function () {
684+
it('should not pass when received a invalid value', function () {
685+
var value = null;
686+
var rule = { int: { type: 'int1', required: false } };
687+
parameterWithRootValidate.validate(rule, value)[0].message.should.equal('the validated value should be a object');;
688+
});
689+
});

0 commit comments

Comments
 (0)