-
Notifications
You must be signed in to change notification settings - Fork 11
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: add email
validator
#39
Draft
michael-small
wants to merge
4
commits into
timdeschryver:main
Choose a base branch
from
michael-small:email-validator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b8741f4
feat: add `email` validator
michael-small 8a80df9
fix: change invalid email error details to reflect current value
michael-small 01bb51b
fix: clarify tsdoc description of email validator regex
michael-small 564723b
fix: compare reactive forms email validation with new validator
michael-small File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Component, inject } from '@angular/core'; | ||
import { JsonPipe } from '@angular/common'; | ||
import { | ||
createFormField, | ||
createFormGroup, | ||
SignalInputDebounceDirective, | ||
SignalInputDirective, | ||
SignalInputErrorDirective, | ||
Validators, | ||
withErrorComponent, | ||
} from '@ng-signal-forms'; | ||
import { | ||
FormBuilder, | ||
FormControl, | ||
FormsModule, | ||
ReactiveFormsModule, | ||
Validators as ReactiveFormValidators, | ||
} from '@angular/forms'; | ||
import { CustomErrorComponent } from '../custom-input-error.component'; | ||
|
||
@Component({ | ||
selector: 'validators', | ||
standalone: true, | ||
imports: [ | ||
FormsModule, | ||
JsonPipe, | ||
SignalInputDirective, | ||
SignalInputErrorDirective, | ||
SignalInputDebounceDirective, | ||
// TODO - remove before PR | ||
ReactiveFormsModule, | ||
], | ||
template: ` | ||
<div> | ||
<label>Email (signal form)</label> | ||
<input ngModel [formField]="form.controls.email" /> | ||
</div> | ||
<pre>{{ form.value() | json }}</pre> | ||
<pre>{{ form.errorsArray() | json }}</pre> | ||
|
||
<!-- TODO - remove reactive forms point of reference before PR --> | ||
<form [formGroup]="reactiveForm"> | ||
<label>Email (reactive form)</label> | ||
<input formControlName="email" type="text" /> | ||
</form> | ||
<pre>{{ reactiveForm.value | json }}</pre> | ||
<pre>{{ reactiveForm.errors | json }}</pre> | ||
<pre>{{ reactiveForm.controls.email.errors | json }}</pre> | ||
`, | ||
styles: [], | ||
providers: [withErrorComponent(CustomErrorComponent)], | ||
}) | ||
export default class ValidatorsComponent { | ||
form = createFormGroup({ | ||
email: createFormField('', { validators: [Validators.email()] }), | ||
}); | ||
|
||
#fb = inject(FormBuilder); | ||
|
||
reactiveForm = this.#fb.group({ | ||
email: new FormControl('', { validators: ReactiveFormValidators.email }), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
/** | ||
* @description Email regex pattern is directly from the Angular Forms definition. | ||
* Note: other assumptions on validation differ from Angular reactive forms. | ||
* @link https://github.com/angular/angular/blob/17.3.2/packages/forms/src/validators.ts#L126 | ||
*/ | ||
const EMAIL_REGEXP = | ||
/^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; | ||
|
||
export function email(): ValidatorFn { | ||
return (value: unknown, setState: SetValidationState) => { | ||
const valid = | ||
value === null || | ||
value === undefined || | ||
typeof value !== 'string' || | ||
EMAIL_REGEXP.test(value); | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
email: { | ||
details: { | ||
currentValue: value, | ||
}, | ||
}, | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './email'; | ||
export * from './equals-to'; | ||
export * from './length'; | ||
export * from './max'; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about invoking the regexp validator with the email regexp?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make a lot more sense lol