Skip to content

Commit 478fb13

Browse files
authored
Merge pull request #81 from formio/fix/implement-patternMessage
Fix/implement pattern message
2 parents c934c3f + 5c776f1 commit 478fb13

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/process/validation/rules/__tests__/validateRegexPattern.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ it('Validating a component with a pattern parameter will return null if the valu
3636
});
3737

3838
it('Validating a component with an empty value will not trigger the pattern validation', async () => {
39-
const component = {...simpleTextField, validate: { pattern: '\\d' } };
39+
const component = { ...simpleTextField, validate: { pattern: '\\d' } };
4040
const data = {
4141
component: ''
4242
};
4343

4444
const context = generateProcessorContext(component, data);
4545
const result = await validateRegexPattern(context);
4646
expect(result).to.equal(null);
47-
})
47+
});
48+
49+
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 () => {
50+
const component = { ...simpleTextField, validate: { pattern: '\\d', patternMessage: 'Can only contain digits.' } };
51+
const data = {
52+
component: 'abc',
53+
};
54+
const context = generateProcessorContext(component, data);
55+
const result = await validateRegexPattern(context);
56+
expect(result).to.be.instanceOf(FieldError);
57+
expect(result).to.have.property('errorKeyOrMessage', 'Can only contain digits.');
58+
});

src/process/validation/rules/validateRegexPattern.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ export const validateRegexPattern: RuleFn = async (context: ValidationContext) =
2727

2828
export const validateRegexPatternSync: RuleFnSync = (context: ValidationContext) => {
2929
const { component, value } = context;
30-
if (!shouldValidate(context)) {
30+
if (!shouldValidate(context) || !isValidatableTextFieldComponent(component)) {
3131
return null;
3232
}
3333

3434
const pattern = (component as TextAreaComponent).validate?.pattern;
3535
const regex = new RegExp(`^${pattern}$`);
3636
return typeof value === 'string' && regex.test(value)
3737
? null
38-
: new FieldError('pattern', { ...context, regex: pattern, pattern: pattern, setting: pattern });
38+
: new FieldError(component.validate?.patternMessage || 'pattern', { ...context, regex: pattern, pattern: pattern, setting: pattern }, 'pattern');
3939
};
4040

4141
export const validateRegexPatternInfo: ProcessorInfo<ValidationContext, FieldError | null> = {
4242
name: 'validateRegexPattern',
4343
process: validateRegexPattern,
4444
processSync: validateRegexPatternSync,
4545
shouldProcess: shouldValidate,
46-
};
46+
};

src/types/Component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export type TextFieldComponent = BaseComponent & {
7171
minWords?: number | string;
7272
maxWords?: number | string;
7373
pattern?: string;
74+
patternMessage?: string;
7475
};
7576
};
7677

0 commit comments

Comments
 (0)