Skip to content

Commit

Permalink
Object deps error path. Closes #1960
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 25, 2019
1 parent 917fdf7 commit 9d207f1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 65 deletions.
3 changes: 1 addition & 2 deletions lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ internals.Object = class extends Any {

const failed = internals.dependencies[dep.type](this, dep, value, state, prefs);
if (failed) {
const localState = dep.key ? this._state([...state.path, ...dep.key.path], null, state) : this._state(state.path, null, state);
const report = this.createError(failed.code, value, failed.context, localState, prefs);
const report = this.createError(failed.code, value, failed.context, state, prefs);
if (prefs.abortEarly) {
return { value, errors: report };
}
Expand Down
16 changes: 7 additions & 9 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('errors', () => {
value.required = value.renamed;
delete value.renamed;

expect(error).to.be.an.error('"value" 11. "email" 19. "date" 18. "alphanum" 16. "min" 14. "max" 15. "notEmpty" 3. "required" 7. "xor" 7');
expect(error).to.be.an.error('"value" 11. "email" 19. "date" 18. "alphanum" 16. "min" 14. "max" 15. "notEmpty" 3. "value" 7');
expect(error.name).to.equal('ValidationError');
expect(error.details).to.equal([
{
Expand Down Expand Up @@ -125,30 +125,28 @@ describe('errors', () => {
context: { value: '', invalids: [''], label: 'notEmpty', key: 'notEmpty' }
},
{
message: '"required" 7',
path: ['required'],
message: '"value" 7',
path: [],
type: 'object.without',
context: {
main: 'required',
mainWithLabel: 'required',
peer: 'xor',
peerWithLabel: 'xor',
label: 'required',
key: 'required',
label: 'value',
value
}
},
{
message: '"xor" 7',
path: ['xor'],
message: '"value" 7',
path: [],
type: 'object.without',
context: {
main: 'xor',
mainWithLabel: 'xor',
peer: 'required',
peerWithLabel: 'required',
label: 'xor',
key: 'xor',
label: 'value',
value
}
}
Expand Down
25 changes: 10 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,14 @@ describe('Joi', () => {
expect(err).to.be.an.error('"txt" missing required peer "upc"');
expect(err.details).to.equal([{
message: '"txt" missing required peer "upc"',
path: ['txt'],
path: [],
type: 'object.with',
context: {
main: 'txt',
mainWithLabel: 'txt',
peer: 'upc',
peerWithLabel: 'upc',
label: 'txt',
key: 'txt',
label: 'value',
value: { txt: 'a' }
}
}]);
Expand All @@ -527,15 +526,14 @@ describe('Joi', () => {
message: '"txt" missing required peer "upc"',
details: [{
message: '"txt" missing required peer "upc"',
path: ['txt'],
path: [],
type: 'object.with',
context: {
main: 'txt',
mainWithLabel: 'txt',
peer: 'upc',
peerWithLabel: 'upc',
label: 'txt',
key: 'txt',
label: 'value',
value: { txt: 'test' }
}
}]
Expand All @@ -562,15 +560,14 @@ describe('Joi', () => {
message: '"txt" missing required peer "upc"',
details: [{
message: '"txt" missing required peer "upc"',
path: ['txt'],
path: [],
type: 'object.with',
context: {
main: 'txt',
mainWithLabel: 'txt',
peer: 'upc',
peerWithLabel: 'upc',
label: 'txt',
key: 'txt',
label: 'value',
value: { txt: 'test', upc: undefined }
}
}]
Expand All @@ -590,15 +587,14 @@ describe('Joi', () => {
expect(err).to.be.an.error('"txt" conflict with forbidden peer "upc"');
expect(err.details).to.equal([{
message: '"txt" conflict with forbidden peer "upc"',
path: ['txt'],
path: [],
type: 'object.without',
context: {
main: 'txt',
mainWithLabel: 'txt',
peer: 'upc',
peerWithLabel: 'upc',
label: 'txt',
key: 'txt',
label: 'value',
value: { txt: 'a', upc: 'b' }
}
}]);
Expand Down Expand Up @@ -629,15 +625,14 @@ describe('Joi', () => {
message: '"txt" conflict with forbidden peer "upc"',
details: [{
message: '"txt" conflict with forbidden peer "upc"',
path: ['txt'],
path: [],
type: 'object.without',
context: {
main: 'txt',
mainWithLabel: 'txt',
peer: 'upc',
peerWithLabel: 'upc',
label: 'txt',
key: 'txt',
label: 'value',
value: { txt: 'test', upc: 'test' }
}
}]
Expand Down
65 changes: 26 additions & 39 deletions test/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,15 +627,14 @@ describe('object', () => {
message: '"first" missing required peer "second"',
details: [{
message: '"first" missing required peer "second"',
path: ['first'],
path: [],
type: 'object.with',
context: {
main: 'first',
mainWithLabel: 'first',
peer: 'second',
peerWithLabel: 'second',
label: 'first',
key: 'first',
label: 'value',
value: { first: 'value' }
}
}]
Expand Down Expand Up @@ -758,15 +757,14 @@ describe('object', () => {
},
{
message: '"a" missing required peer "b"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'a',
peer: 'b',
peerWithLabel: 'b',
label: 'a',
key: 'a',
label: 'value',
value: input
}
}
Expand Down Expand Up @@ -797,15 +795,14 @@ describe('object', () => {
expect(error).to.be.an.error('"first" missing required peer "b.second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "b.second"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'b.second',
label: 'a',
key: 'a',
label: 'value',
value: { a: 1, b: { d: 2 } }
}
}]);
Expand Down Expand Up @@ -3358,15 +3355,14 @@ describe('object', () => {
expect(error).to.be.an.error('"first" missing required peer "second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "second"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b',
peerWithLabel: 'second',
label: 'a',
key: 'a',
label: 'value',
value: { a: 1 }
}
}]);
Expand All @@ -3386,15 +3382,14 @@ describe('object', () => {
message: '"a" missing required peer "b.c"',
details: [{
message: '"a" missing required peer "b.c"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'a',
peer: 'b.c',
peerWithLabel: 'b.c',
key: 'a',
label: 'a',
label: 'value',
value: { a: 'test', b: { d: 80 } }
}
}]
Expand All @@ -3412,15 +3407,14 @@ describe('object', () => {
message: '"a.b" missing required peer "b.c"',
details: [{
message: '"a.b" missing required peer "b.c"',
path: ['a', 'b'],
path: [],
type: 'object.with',
context: {
main: 'a.b',
mainWithLabel: 'a.b',
peer: 'b.c',
peerWithLabel: 'b.c',
key: 'b',
label: 'a.b',
label: 'value',
value: { a: { b: 'test' }, b: {} }
}
}]
Expand All @@ -3444,15 +3438,14 @@ describe('object', () => {
expect(error).to.be.an.error('"a" missing required peer "b.c"');
expect(error.details).to.equal([{
message: '"a" missing required peer "b.c"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'a',
peer: 'b.c',
peerWithLabel: 'b.c',
key: 'a',
label: 'a',
label: 'value',
value: error.details[0].context.value
}
}]);
Expand All @@ -3473,15 +3466,14 @@ describe('object', () => {
expect(error).to.be.an.error('"first" missing required peer "b.second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "b.second"',
path: ['a'],
path: [],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'b.second',
label: 'a',
key: 'a',
label: 'value',
value: { a: 1, b: { d: 2 } }
}
}]);
Expand All @@ -3500,15 +3492,14 @@ describe('object', () => {
expect(error2).to.be.an.error('"a.first" missing required peer "b.second"');
expect(error2.details).to.equal([{
message: '"a.first" missing required peer "b.second"',
path: ['a', 'b'],
path: [],
type: 'object.with',
context: {
main: 'a.b',
mainWithLabel: 'a.first',
peer: 'b.c',
peerWithLabel: 'b.second',
label: 'a.b',
key: 'b',
label: 'value',
value: { a: { b: 'test' }, b: {} }
}
}]);
Expand Down Expand Up @@ -3583,15 +3574,14 @@ describe('object', () => {
expect(error).to.be.an.error('"first" conflict with forbidden peer "second"');
expect(error.details).to.equal([{
message: '"first" conflict with forbidden peer "second"',
path: ['a'],
path: [],
type: 'object.without',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b',
peerWithLabel: 'second',
label: 'a',
key: 'a',
label: 'value',
value: { a: 1, b: 'b' }
}
}]);
Expand All @@ -3615,15 +3605,14 @@ describe('object', () => {
expect(error2).to.be.an.error('"a" conflict with forbidden peer "b.d"');
expect(error2.details).to.equal([{
message: '"a" conflict with forbidden peer "b.d"',
path: ['a'],
path: [],
type: 'object.without',
context: {
main: 'a',
mainWithLabel: 'a',
peer: 'b.d',
peerWithLabel: 'b.d',
key: 'a',
label: 'a',
label: 'value',
value: sampleObject2
}
}]);
Expand All @@ -3648,15 +3637,14 @@ describe('object', () => {
expect(error2).to.be.an.error('"a" conflict with forbidden peer "b.d"');
expect(error2.details).to.equal([{
message: '"a" conflict with forbidden peer "b.d"',
path: ['a'],
path: [],
type: 'object.without',
context: {
main: 'a',
mainWithLabel: 'a',
peer: 'b.d',
peerWithLabel: 'b.d',
key: 'a',
label: 'a',
label: 'value',
value: error2.details[0].context.value
}
}]);
Expand All @@ -3677,15 +3665,14 @@ describe('object', () => {
expect(error).to.be.an.error('"first" conflict with forbidden peer "b.second"');
expect(error.details).to.equal([{
message: '"first" conflict with forbidden peer "b.second"',
path: ['a'],
path: [],
type: 'object.without',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'b.second',
label: 'a',
key: 'a',
label: 'value',
value: { a: 1, b: { c: 'c' } }
}
}]);
Expand Down

0 comments on commit 9d207f1

Please sign in to comment.