feat: yup schema should not allow unknown keys

This commit is contained in:
Anoop M D 2022-10-14 22:42:46 +05:30
parent 8763ff2ad1
commit e46e3d5b22
3 changed files with 17 additions and 2 deletions

View File

@ -2,7 +2,8 @@ const Yup = require('yup');
const uidSchema = Yup.string()
.length(21, 'uid must be 21 characters in length')
.matches(/^[a-zA-Z0-9]*$/, 'uid must be alphanumeric');
.matches(/^[a-zA-Z0-9]*$/, 'uid must be alphanumeric')
.strict();
module.exports = {
uidSchema

View File

@ -8,7 +8,7 @@ const workspaceSchema = Yup.object({
.max(50, 'name must be 50 characters or less')
.required('name is required'),
collectionUids: Yup.array().of(uidSchema)
});
}).noUnknown(true).strict();
module.exports = {
workspaceSchema

View File

@ -69,4 +69,18 @@ describe('Workspace Schema Validation', () => {
)
]);
});
it('workspace schema must throw error when unknown key is present', async () => {
const workspace = {
uid: uuid(),
name: 'My Workspace',
foo: 'bar'
};
return Promise.all([
expect(workspaceSchema.validate(workspace)).rejects.toEqual(
validationErrorWithMessages('this field has unspecified keys: foo')
)
]);
});
});