Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forms): handle string with and without line boundary on pattern validator #19256

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/forms/src/validators.ts
Expand Up @@ -145,7 +145,14 @@ export class Validators {
let regex: RegExp;
let regexStr: string;
if (typeof pattern === 'string') {
regexStr = `^${pattern}$`;
regexStr = '';

if (pattern.charAt(0) !== '^') regexStr += '^';

regexStr += pattern;

if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';

regex = new RegExp(regexStr);
} else {
regexStr = pattern.toString();
Expand Down
6 changes: 6 additions & 0 deletions packages/forms/test/validators_spec.ts
Expand Up @@ -270,6 +270,12 @@ export function main() {

it('should not error on "undefined" pattern',
() => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull());

it('should work with string containing line boundary',
() => expect(Validators.pattern('^[aA]*$')(new FormControl('aaAA'))).toBeNull());

it('should work with string not containing line boundary',
() => expect(Validators.pattern('[aA]*')(new FormControl('aaAA'))).toBeNull());
});

describe('compose', () => {
Expand Down