Skip to content

Commit ad5d015

Browse files
authoredMar 13, 2020
feat: provide keys in default noUnknown message (#579)
1 parent e87b9a9 commit ad5d015

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed
 

‎src/locale.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export let date = {
5454
export let boolean = {};
5555

5656
export let object = {
57-
noUnknown: '${path} field cannot have keys not specified in the object shape',
57+
noUnknown: '${path} field has unspecified keys: ${unknown}',
5858
};
5959

6060
export let array = {

‎src/object.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ inherits(ObjectSchema, MixedSchema, {
240240
exclusive: true,
241241
message: message,
242242
test(value) {
243+
const unknownKeys = unknown(this.schema, value);
243244
return (
244-
value == null || !noAllow || unknown(this.schema, value).length === 0
245+
value == null ||
246+
!noAllow ||
247+
unknownKeys.length === 0 ||
248+
this.createError({ params: { unknown: unknownKeys.join(', ') } })
245249
);
246250
},
247251
});

‎test/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ describe('Object types', () => {
325325
.validate({ extra: 'field' }, { strict: true })
326326
.should.be.rejected()
327327
.then(err => {
328-
err.errors[0].should.be.a('string');
328+
err.errors[0].should.be.a('string').that.include('extra');
329329
}),
330330
]);
331331
});

0 commit comments

Comments
 (0)
Please sign in to comment.