Skip to content

Commit

Permalink
feat: provide keys in default noUnknown message (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Mar 13, 2020
1 parent e87b9a9 commit ad5d015
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 5 additions & 1 deletion src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ') } })
);
},
});
Expand Down
2 changes: 1 addition & 1 deletion test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}),
]);
});
Expand Down

0 comments on commit ad5d015

Please sign in to comment.