diff --git a/src/locale.js b/src/locale.js index df2e58944..a0c422942 100644 --- a/src/locale.js +++ b/src/locale.js @@ -54,7 +54,7 @@ export let date = { export let boolean = {}; export let object = { - noUnknown: '${path} field cannot have keys not specified in the object shape', + noUnknown: '${path} field has unspecified keys: ${unknown}', }; export let array = { diff --git a/src/object.js b/src/object.js index fef9d0c64..0166ad04b 100644 --- a/src/object.js +++ b/src/object.js @@ -240,8 +240,12 @@ inherits(ObjectSchema, MixedSchema, { exclusive: true, message: message, test(value) { + const unknownKeys = unknown(this.schema, value); return ( - value == null || !noAllow || unknown(this.schema, value).length === 0 + value == null || + !noAllow || + unknownKeys.length === 0 || + this.createError({ params: { unknown: unknownKeys.join(', ') } }) ); }, }); diff --git a/test/object.js b/test/object.js index cc847e5b3..a62a89d8d 100644 --- a/test/object.js +++ b/test/object.js @@ -325,7 +325,7 @@ describe('Object types', () => { .validate({ extra: 'field' }, { strict: true }) .should.be.rejected() .then(err => { - err.errors[0].should.be.a('string'); + err.errors[0].should.be.a('string').that.include('extra'); }), ]); });