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

fix: message should work when its empty string #256

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions __tests__/required.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,22 @@ describe('required', () => {
},
);
});

it('should support empty string message', done => {
new Schema({
v: {
required,
message: '',
},
}).validate(
{
v: '',
},
errors => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe('');
done();
},
);
});
});
10 changes: 9 additions & 1 deletion __tests__/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,27 @@ describe('validator', () => {
return true;
},
},
// Customize with empty message
{
validator() {
return false;
},
message: '',
},
],
}).validate(
{
v: 2,
},
errors => {
expect(errors.length).toBe(6);
expect(errors.length).toBe(7);
expect(errors[0].message).toBe('e1');
expect(errors[1].message).toBe('e2');
expect(errors[2].message).toBe('e3');
expect(errors[3].message).toBe('v3 fails');
expect(errors[4].message).toBe('e5');
expect(errors[5].message).toBe('e6');
expect(errors[6].message).toBe('');
done();
},
);
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Schema.prototype = {
if (!options.suppressWarning && errors.length) {
Schema.warning('async-validator:', errors);
}
if (errors.length && rule.message) {
if (errors.length && rule.message !== undefined) {
errors = [].concat(rule.message);
}

Expand All @@ -178,7 +178,7 @@ Schema.prototype = {
// does not exist fail at the rule level and don't
// go deeper
if (rule.required && !data.value) {
if (rule.message) {
if (rule.message !== undefined) {
errors = [].concat(rule.message).map(complementError(rule));
} else if (options.error) {
errors = [
Expand Down Expand Up @@ -263,7 +263,8 @@ Schema.prototype = {
}
if (
typeof rule.validator !== 'function' &&
rule.type && !validators.hasOwnProperty(rule.type)
rule.type &&
!validators.hasOwnProperty(rule.type)
) {
throw new Error(format('Unknown rule type %s', rule.type));
}
Expand Down Expand Up @@ -300,5 +301,4 @@ Schema.messages = defaultMessages;

Schema.validators = validators;


export default Schema;