Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat required config #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Parameter {
if (opts.validateRoot) {
this.validateRoot = true;
}
this.defaultRequired = opts.defaultRequired !== undefined ?
opts.defaultRequired :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.defaultRequired = opts.defaultRequired !== false;

Copy link
Author

@ycjcl868 ycjcl868 Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for cr ! ~

true;
}

t() {
Expand Down Expand Up @@ -71,9 +74,11 @@ class Parameter {
for (var key in rules) {
var rule = formatRule(rules[key]);
var has = obj.hasOwnProperty(key);

if (!has) {
if (rule.required !== false) {
var required = rule.required !== undefined ?
rule.required :
this.defaultRequired;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放一行吧,这样缩进有点奇怪

Copy link
Author

@ycjcl868 ycjcl868 Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,已修改。 839d425

if (required) {
errors.push({
message: this.t('required'),
field: key,
Expand Down
30 changes: 26 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ var parameter = new Parameter();
var parameterWithRootValidate = new Parameter({
validateRoot: true,
});
var parameterWithDefaultRequiredValidate = new Parameter({
defaultRequired: false,
});

describe('parameter', function () {
describe('required', function () {
it('should required work fine', function () {
var value = {int: 1};
var value = {};
var rule = {int: {type: 'int', required: true}};
parameter.validate(rule, {})[0].should.eql({
parameter.validate(rule, value)[0].should.eql({
code: 'missing_field',
field: 'int',
message: 'required'
});
});

it('should not required work fine', function () {
var value = {int: 1};
var value = {};
var rule = {int: {type: 'int', required: false}};
should.not.exist(parameter.validate(rule, {}));
should.not.exist(parameter.validate(rule, value));
});

it('should not required check ok', function () {
Expand Down Expand Up @@ -727,3 +730,22 @@ describe('validate with option.validateRoot', function () {
parameterWithRootValidate.validate(rule, value)[0].message.should.equal('the validated value should be a object');;
});
});


describe('validate with option.defaultNotRequired', function () {
it('should not required work fine', function () {
var value = {};
var rule = {int: {type: 'int'}};
should.not.exist(parameterWithDefaultRequiredValidate.validate(rule, value));
});

it('should required check ok', function () {
var value = {int: 1.1};
var rule = {int: {type: 'int', required: true}};
parameterWithDefaultRequiredValidate.validate(rule, value)[0].should.eql({
code: 'invalid',
field: 'int',
message: 'should be an integer'
});
});
});