Skip to content

Commit

Permalink
minor plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanbond committed Apr 16, 2024
1 parent 3acbc0c commit 5c776f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ 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: ''
};

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.');
});
2 changes: 1 addition & 1 deletion src/process/validation/rules/validateRegexPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationContext, FieldError | null> = {
Expand Down
1 change: 1 addition & 0 deletions src/types/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type TextFieldComponent = BaseComponent & {
minWords?: number | string;
maxWords?: number | string;
pattern?: string;
patternMessage?: string;
};
};

Expand Down

0 comments on commit 5c776f1

Please sign in to comment.