Skip to content

Commit

Permalink
Fix error messages of regex renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Oct 8, 2017
1 parent 5f1fe2e commit 9a92283
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/language.js
Expand Up @@ -97,7 +97,11 @@ exports.errors = {
assert: '!!"{{ref}}" validation failed because "{{ref}}" failed to {{message}}',
rename: {
multiple: 'cannot rename child "{{from}}" because multiple renames are disabled and another key was already renamed to "{{to}}"',
override: 'cannot rename child "{{from}}" because override is disabled and target "{{to}}" exists'
override: 'cannot rename child "{{from}}" because override is disabled and target "{{to}}" exists',
regex: {
multiple: 'cannot rename children {{from}} because multiple renames are disabled and another key was already renamed to "{{to}}"',
override: 'cannot rename children {{from}} because override is disabled and target "{{to}}" exists'
}
},
type: 'must be an instance of "{{type}}"',
schema: 'must be a Joi instance'
Expand Down
4 changes: 2 additions & 2 deletions lib/types/object/index.js
Expand Up @@ -112,7 +112,7 @@ internals.Object = class extends Any {
if (!rename.options.multiple &&
renamed[rename.to]) {

errors.push(this.createError('object.rename.multiple', { from: matchedTargetKeys, to: rename.to }, state, options));
errors.push(this.createError('object.rename.regex.multiple', { from: matchedTargetKeys, to: rename.to }, state, options));
if (options.abortEarly) {
return finish();
}
Expand All @@ -122,7 +122,7 @@ internals.Object = class extends Any {
!rename.options.override &&
!renamed[rename.to]) {

errors.push(this.createError('object.rename.override', { from: rename.from, to: rename.to }, state, options));
errors.push(this.createError('object.rename.regex.override', { from: matchedTargetKeys, to: rename.to }, state, options));
if (options.abortEarly) {
return finish();
}
Expand Down
44 changes: 41 additions & 3 deletions test/types/object.js
Expand Up @@ -1098,7 +1098,13 @@ describe('object', () => {

Joi.compile(schema).validate({ test: 'b', test1: 'a' }, (err, value) => {

expect(err.message).to.equal('"value" cannot rename child "/^test1$/i" because override is disabled and target "test1" exists');
expect(err.message).to.equal('"value" cannot rename children [test1] because override is disabled and target "test1" exists');
expect(err.details).to.equal([{
message: '"value" cannot rename children [test1] because override is disabled and target "test1" exists',
path: [],
type: 'object.rename.regex.override',
context: { from: ['test1'], to: 'test1', key: undefined, label: 'value' }
}]);
done();
});
});
Expand Down Expand Up @@ -1319,7 +1325,13 @@ describe('object', () => {

Joi.compile(schema).validate({ FOOBAR: 'a', FooBar: 'b' }, (err, value) => {

expect(err.message).to.equal('"value" cannot rename child "[fooBar]" because multiple renames are disabled and another key was already renamed to "fooBar"');
expect(err.message).to.equal('"value" cannot rename children [fooBar] because multiple renames are disabled and another key was already renamed to "fooBar"');
expect(err.details).to.equal([{
message: '"value" cannot rename children [fooBar] because multiple renames are disabled and another key was already renamed to "fooBar"',
path: [],
type: 'object.rename.regex.multiple',
context: { from: ['fooBar'], to: 'fooBar', key: undefined, label: 'value' }
}]);
done();
});
});
Expand All @@ -1329,7 +1341,33 @@ describe('object', () => {
Joi.object().keys({ z: Joi.string() }).rename(/a/i, 'b').rename(/c/i, 'b').rename(/z/i, 'z').options({ abortEarly: false }).validate({ a: 1, c: 1, d: 1, z: 1 }, (err, value) => {

expect(err).to.exist();
expect(err.message).to.equal('"value" cannot rename child "[c]" because multiple renames are disabled and another key was already renamed to "b". "value" cannot rename child "/z/i" because override is disabled and target "z" exists. "d" is not allowed. "b" is not allowed');
expect(err.message).to.equal('"value" cannot rename children [c] because multiple renames are disabled and another key was already renamed to "b". "value" cannot rename children [z] because override is disabled and target "z" exists. "d" is not allowed. "b" is not allowed');
expect(err.details).to.equal([
{
message: '"value" cannot rename children [c] because multiple renames are disabled and another key was already renamed to "b"',
path: [],
type: 'object.rename.regex.multiple',
context: { from: ['c'], to: 'b', key: undefined, label: 'value' }
},
{
message: '"value" cannot rename children [z] because override is disabled and target "z" exists',
path: [],
type: 'object.rename.regex.override',
context: { from: ['z'], to: 'z', key: undefined, label: 'value' }
},
{
message: '"d" is not allowed',
path: ['d'],
type: 'object.allowUnknown',
context: { child: 'd', key: 'd', label: 'd' }
},
{
message: '"b" is not allowed',
path: ['b'],
type: 'object.allowUnknown',
context: { child: 'b', key: 'b', label: 'b' }
}
]);
done();
});
});
Expand Down

0 comments on commit 9a92283

Please sign in to comment.