Make string input strict

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-02-18 11:55:00 +01:00
parent 9ad0744e4b
commit 6d490aa366
2 changed files with 35 additions and 2 deletions

View File

@ -23,7 +23,7 @@ describe('Entity validator', () => {
}, },
}; };
const input = { title: 123 }; const input = { title: 1234 };
expect.hasAssertions(); expect.hasAssertions();
@ -59,6 +59,39 @@ describe('Entity validator', () => {
expect(data).toEqual(input); expect(data).toEqual(input);
}); });
it('Returns casted data when possible', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
},
number: {
type: 'integer',
},
},
};
const input = { title: 'Test', number: '123' };
expect.hasAssertions();
const data = await entityValidator.validateEntity(model, input);
expect(data).toEqual({
title: 'Test',
number: 123,
});
});
test('Throws on required not respected', async () => { test('Throws on required not respected', async () => {
const errors = { const errors = {
badRequest: jest.fn(), badRequest: jest.fn(),

View File

@ -66,7 +66,7 @@ const addMaxFloatValidator = ({ max }, validator) =>
/* Type validators */ /* Type validators */
const stringValidator = composeValidators( const stringValidator = composeValidators(
() => yup.string(), () => yup.string().strict(),
addMinLengthValidator, addMinLengthValidator,
addMaxLengthValidator addMaxLengthValidator
); );