Skip to content

Commit

Permalink
Fix concat of empty() schemas. Fixes #1622.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Oct 29, 2018
1 parent 3607799 commit 5fc4178
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,2 +1,3 @@
examples
sandbox.js
/benchmarks/node_modules
16 changes: 15 additions & 1 deletion lib/types/any/index.js
Expand Up @@ -168,7 +168,21 @@ module.exports = internals.Any = class {
obj._invalids.merge(schema._invalids, schema._valids);
obj._tests.push(...schema._tests);
obj._refs.push(...schema._refs);
Hoek.merge(obj._flags, schema._flags);
if (obj._flags.empty && schema._flags.empty) {
obj._flags.empty = obj._flags.empty.concat(schema._flags.empty);
const flags = Object.assign({}, schema._flags);
delete flags.empty;
Hoek.merge(obj._flags, flags);
}
else if (schema._flags.empty) {
obj._flags.empty = schema._flags.empty;
const flags = Object.assign({}, schema._flags);
delete flags.empty;
Hoek.merge(obj._flags, flags);
}
else {
Hoek.merge(obj._flags, schema._flags);
}

obj._description = schema._description || obj._description;
obj._unit = schema._unit || obj._unit;
Expand Down
79 changes: 79 additions & 0 deletions test/types/any.js
Expand Up @@ -1588,6 +1588,85 @@ describe('any', () => {
]);
});

it('merges two schemas (flags with empty on both sides)', () => {

const a = Joi.string().valid('a').empty('');
const b = Joi.string().insensitive().empty(' ');

Helper.validate(a, [
['a', true],
['A', false, null, {
message: '"value" must be one of [a]',
details: [{
message: '"value" must be one of [a]',
path: [],
type: 'any.allowOnly',
context: { value: 'A', valids: ['a'], label: 'value', key: undefined }
}]
}],
['b', false, null, {
message: '"value" must be one of [a]',
details: [{
message: '"value" must be one of [a]',
path: [],
type: 'any.allowOnly',
context: { value: 'b', valids: ['a'], label: 'value', key: undefined }
}]
}],
['', true, null, undefined],
[' ', false, null, {
message: '"value" must be one of [a]',
details: [{
message: '"value" must be one of [a]',
path: [],
type: 'any.allowOnly',
context: { value: ' ', valids: ['a'], label: 'value', key: undefined }
}]
}]
]);

const ab = a.concat(b);
Helper.validate(ab, [
['a', true, null, 'a'],
['A', true, null, 'a'],
['b', false, null, {
message: '"value" must be one of [a]',
details: [{
message: '"value" must be one of [a]',
path: [],
type: 'any.allowOnly',
context: { value: 'b', valids: ['a'], label: 'value', key: undefined }
}]
}],
['', false, null, {
message: '"value" is not allowed to be empty',
details: [{
message: '"value" is not allowed to be empty',
path: [],
type: 'any.empty',
context: { value: '', invalids: [''], label: 'value', key: undefined }
}]
}],
[' ', true, null, undefined]
]);

expect(ab.describe()).to.equal({
type: 'string',
flags: {
allowOnly: true,
empty: {
type: 'string',
flags: { allowOnly: true },
valids: [' '],
invalids: ['']
},
insensitive: true
},
valids: ['a'],
invalids: ['']
});
});

it('overrides and append information', () => {

const a = Joi.description('a').unit('a').tags('a').example('a');
Expand Down

0 comments on commit 5fc4178

Please sign in to comment.