Merge pull request #11808 from jakobsjoqvist/fix/skip-regex-validation-empty-string

Fix regex validation for empty string in non-required fields
This commit is contained in:
Alexandre BODIN 2021-12-13 11:52:36 +01:00 committed by GitHub
commit b372f8c185
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 2 deletions

View File

@ -338,7 +338,10 @@ const createYupSchemaAttribute = (type, validations, options) => {
break;
}
case 'regex':
schema = schema.matches(new RegExp(validationValue), errorsTrads.regex);
schema = schema.matches(new RegExp(validationValue), {
message: errorsTrads.regex,
excludeEmptyString: !validations.required,
});
break;
case 'lowercase':
if (['text', 'textarea', 'email', 'string'].includes(type)) {

View File

@ -20,6 +20,102 @@ describe('Entity validator', () => {
fakeFindOne.mockReset();
});
describe('String RegExp validator', () => {
const fakeModel = {
kind: 'contentType',
modelName: 'test-model',
uid: 'test-uid',
privateAttributes: [],
options: {},
attributes: {
attrStringRequiredRegex: { type: 'string', required: true },
attrStringNotRequiredRegex: { type: 'string', required: false },
},
};
test('It fails the validation of an empty string for a required field', () => {
const validator = strapiUtils.validateYupSchema(
entityValidator.string(
{
attr: { type: 'string', required: true, regex: '^\\d+$' },
model: fakeModel,
updatedAttribute: {
name: 'attrStringRequiredRegex',
value: '',
},
entity: null,
},
{ isDraft: false }
)
);
return expect(validator('')).rejects.toBeInstanceOf(YupValidationError);
});
test('It validates successfully for a string that follows regex for a required field', () => {
const value = '1234';
const validator = strapiUtils.validateYupSchema(
entityValidator.string(
{
attr: { type: 'string', required: true, regex: '^\\d+$' },
model: fakeModel,
updatedAttribute: {
name: 'attrStringRequiredRegex',
value,
},
entity: null,
},
{ isDraft: false }
)
);
return expect(validator(value)).resolves.toEqual(value);
});
test('It validates empty string successfully for non-required field with regex constraint', () => {
const value = '';
const validator = strapiUtils.validateYupSchema(
entityValidator.string(
{
attr: { type: 'string', required: false, regex: '^\\d+$' },
model: fakeModel,
updatedAttribute: {
name: 'attrStringNotRequiredRegex',
value,
},
entity: null,
},
{ isDraft: false }
)
);
return expect(validator(value)).resolves.toEqual(value);
});
test('It validates successfully for string that follows regex for a non-required field', () => {
const value = '1234';
const validator = strapiUtils.validateYupSchema(
entityValidator.string(
{
attr: { type: 'string', required: false, regex: '^\\d+$' },
model: fakeModel,
updatedAttribute: {
name: 'attrStringNotRequiredRegex',
value,
},
entity: null,
},
{ isDraft: false }
)
);
return expect(validator(value)).resolves.toEqual(value);
});
});
describe('String unique validator', () => {
const fakeModel = {
kind: 'contentType',

View File

@ -104,7 +104,9 @@ const addMaxFloatValidator = (validator, { attr }) =>
* @returns {StringSchema}
*/
const addStringRegexValidator = (validator, { attr }) =>
_.isUndefined(attr.regex) ? validator : validator.matches(new RegExp(attr.regex));
_.isUndefined(attr.regex)
? validator
: validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required });
/**
*