mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 03:43:34 +00:00 
			
		
		
		
	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:
		
						commit
						b372f8c185
					
				@ -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)) {
 | 
			
		||||
 | 
			
		||||
@ -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',
 | 
			
		||||
 | 
			
		||||
@ -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 });
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user