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: patternNot #564

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

endlacer
Copy link

Description

Currently, Angular's form validation system provides Validators.pattern() to validate that a form control matches a specific regular expression pattern. However, when we need to validate that a value does NOT match a pattern, developers have to write negated regular expressions (e.g., using negative lookahead or inverse character classes).

This approach has several drawbacks:

  • Writing negated regex patterns is complex and error-prone
  • Negated patterns can be significantly less performant, especially for complex patterns
  • The intention of the code becomes less clear and harder to maintain
  • Some patterns are extremely difficult or impossible to negate correctly using regex alone

Example of current workaround:

Validators.pattern(/^(?!.*badword).+$/) // Prevents 'badword' from appearing anywhere

Proposed solution

A new validator patternNot() that validates that a form control value does NOT match the given pattern.

// Proposed API
Validators.patternNot(pattern: string | RegExp): ValidatorFn

// Usage example
formControl = new FormControl('', [
  Validators.patternNot(/badword/), // Fails if 'badword' is found
]);

Benefits:

  • More intuitive API for negative pattern validation
  • Better performance as it doesn't require complex negated regex
  • Clearer code intention
  • Easier to understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant