diff --git a/src/process/validation/rules/__tests__/validateRegexPattern.test.ts b/src/process/validation/rules/__tests__/validateRegexPattern.test.ts index 2c7b0815..c51ff40b 100644 --- a/src/process/validation/rules/__tests__/validateRegexPattern.test.ts +++ b/src/process/validation/rules/__tests__/validateRegexPattern.test.ts @@ -36,7 +36,7 @@ it('Validating a component with a pattern parameter will return null if the valu }); it('Validating a component with an empty value will not trigger the pattern validation', async () => { - const component = {...simpleTextField, validate: { pattern: '\\d' } }; + const component = { ...simpleTextField, validate: { pattern: '\\d' } }; const data = { component: '' }; @@ -44,4 +44,15 @@ it('Validating a component with an empty value will not trigger the pattern vali const context = generateProcessorContext(component, data); const result = await validateRegexPattern(context); expect(result).to.equal(null); -}) +}); + +it('Validating a component with a pattern parameter and a pattern message will return a FieldError if the value does not match the pattern', async () => { + const component = { ...simpleTextField, validate: { pattern: '\\d', patternMessage: 'Can only contain digits.' } }; + const data = { + component: 'abc', + }; + const context = generateProcessorContext(component, data); + const result = await validateRegexPattern(context); + expect(result).to.be.instanceOf(FieldError); + expect(result).to.have.property('errorKeyOrMessage', 'Can only contain digits.'); +}); diff --git a/src/process/validation/rules/validateRegexPattern.ts b/src/process/validation/rules/validateRegexPattern.ts index c7ab9403..cd76596a 100644 --- a/src/process/validation/rules/validateRegexPattern.ts +++ b/src/process/validation/rules/validateRegexPattern.ts @@ -35,7 +35,7 @@ export const validateRegexPatternSync: RuleFnSync = (context: ValidationContext) const regex = new RegExp(`^${pattern}$`); return typeof value === 'string' && regex.test(value) ? null - : new FieldError(component.validate?.pattern || 'pattern', { ...context, regex: pattern, pattern: pattern, setting: pattern }, 'pattern'); + : new FieldError(component.validate?.patternMessage || 'pattern', { ...context, regex: pattern, pattern: pattern, setting: pattern }, 'pattern'); }; export const validateRegexPatternInfo: ProcessorInfo = { diff --git a/src/types/Component.ts b/src/types/Component.ts index 36a287bc..c050eb8d 100644 --- a/src/types/Component.ts +++ b/src/types/Component.ts @@ -71,6 +71,7 @@ export type TextFieldComponent = BaseComponent & { minWords?: number | string; maxWords?: number | string; pattern?: string; + patternMessage?: string; }; };