Skip to content

Commit

Permalink
Merge pull request #334 from rvsia/addFlagsToPatternValidator
Browse files Browse the repository at this point in the history
fix(renderer): add flags for pattern validator
  • Loading branch information
Hyperkid123 authored Feb 27, 2020
2 parents a66e433 + c50403d commit ed1d7e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ describe('New validators', () => {
expect(validatorMapper(validators.PATTERN_VALIDATOR)({ pattern: '^Foo$' })('Foo')).toBeUndefined();
});

it('should pass pattern validation with configured regexp pattern as string and with flags', () => {
expect(validatorMapper(validators.PATTERN_VALIDATOR)({ pattern: '^Foo$', flags: 'i' })('foo')).toBeUndefined();
});

it('should fail pattern validation with configured regexp pattern as string', () => {
expect(validatorMapper(validators.PATTERN_VALIDATOR)({ pattern: '^Foo$' })('Bar')).toBe('Value does not match pattern: ^Foo$.');
});
Expand Down
4 changes: 2 additions & 2 deletions packages/react-form-renderer/src/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const length = memoize(({
});
});

export const pattern = memoize(({ pattern, message } = {}) => {
const verifiedPattern = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
export const pattern = memoize(({ pattern, message, flags } = {}) => {
const verifiedPattern = typeof pattern === 'string' ? new RegExp(pattern, flags) : pattern;
return prepare(value => {
if (!value) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const schema = {
helperText: 'Value must be equal to Foo',
validate: [{
type: validatorTypes.PATTERN_VALIDATOR,
pattern: /^Foo$/,
pattern: /^Foo$/i,
}],
}, {
component: componentTypes.TEXT_FIELD,
Expand All @@ -21,6 +21,7 @@ const schema = {
validate: [{
type: validatorTypes.PATTERN_VALIDATOR,
pattern: '^Foo$',
flags: 'i',
}],
}],
};
Expand Down

0 comments on commit ed1d7e2

Please sign in to comment.