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): Validator.pattern accepts a RegExp #12323

Merged
merged 1 commit into from
Oct 19, 2016

Conversation

DzmitryShylovich
Copy link
Contributor

Fixes #10150

@@ -7,6 +7,7 @@
*/

import {OpaqueToken} from '@angular/core';
import {isString} from '@angular/facade/src/lang';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use facades, we are in the process of removing them

return (control: AbstractControl): {[key: string]: any} => {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
const regex = new RegExp(`^${pattern}$`);
const regex: RegExp = isString(pattern) ? new RegExp(`^${pattern}$`) : <RegExp>pattern;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type necessary in const regex: RegExp?

@DzmitryShylovich DzmitryShylovich force-pushed the feature/10150 branch 5 times, most recently from 02cd004 to eb04c19 Compare October 15, 2016 15:47
return (control: AbstractControl): {[key: string]: any} => {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
const regex = new RegExp(`^${pattern}$`);
const regex = typeof pattern === 'string' ? new RegExp(`^${pattern}$`) : <RegExp>pattern;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<RegExp> should not be needed

Copy link
Contributor Author

@DzmitryShylovich DzmitryShylovich Oct 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsc complains (at least in ide)

const value: string = control.value;
return regex.test(value) ?
return regex && regex.test(value) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure of the !regex case ?
Should it rather returns an empty ValidatorFn ? (_) => null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test

@vicb vicb added pr_state: LGTM action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Oct 16, 2016

it('should error on failure to match string', () => {
expect(Validators.pattern('[a-zA-Z ]*')(new FormControl('aaa0'))).toEqual({
'pattern': {'requiredPattern': '^[a-zA-Z ]*$', 'actualValue': 'aaa0'}
'pattern': {'requiredPattern': '/^[a-zA-Z ]*$/', 'actualValue': 'aaa0'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove this breaking change ? (we would have to delay the PR until 3.0). Thanks

@DzmitryShylovich DzmitryShylovich force-pushed the feature/10150 branch 2 times, most recently from f26e4ab to 58231d4 Compare October 16, 2016 22:50
@kara kara added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Oct 17, 2016
const value: string = control.value;
return regex.test(value) ?
null :
{'pattern': {'requiredPattern': `^${pattern}$`, 'actualValue': value}};
{'pattern': {'requiredPattern': `${regexStr}`, 'actualValue': value}};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just directly put regexStr?

@alxhub alxhub merged commit bf60418 into angular:master Oct 19, 2016
btrigueiro pushed a commit to btrigueiro/angular that referenced this pull request Oct 21, 2016
@DzmitryShylovich DzmitryShylovich deleted the feature/10150 branch October 28, 2016 10:11
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validator.pattern should accept a Regular Expression instead of a String
7 participants