7.5 Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) (Week 7 • Angular Forms, RxJS & NgRx)
1. Core Concept & Internal Working
Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) (Angular Forms, RxJS & NgRx) covers Field `ValidatorFn` returning `ValidationErrors | null`, cross-field group validator (`password === confirmPassword`, `endDate >= startDate`), debounced `AsyncValidatorFn` checking email availability. In production software engineering, it is used to Validating Indian PAN/GSTIN format synchronously and checking employee-code uniqueness asynchronously on `updateOn: 'blur'`. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Validating Indian PAN/GSTIN format synchronously and checking employee-code uniqueness asynchronously on `updateOn: 'blur'`.
3. Granular Subtopics Covered
- Field `ValidatorFn` returning `ValidationErrors | null`: Core foundation of Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`): Validating Indian PAN/GSTIN format synchronously and checking employee-code uniqueness asynchronously on `updateOn: 'blur'`.
- Cross-field group validator (`password === confirmPassword`: Master 'Cross-field group validator (`password === confirmPassword`' inside Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) across real-world inputs.
- `endDate >= startDate`): Master '`endDate >= startDate`)' inside Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) across real-world inputs.
- Debounced `AsyncValidatorFn` checking email availability: Master 'Debounced `AsyncValidatorFn` checking email availability' inside Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) across real-world inputs.
- Production Pitfall Guard (Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`)): Prevent common defect: Running an HTTP `AsyncValidatorFn` on every keystroke without `timer(350)` / `updateOn: 'blur'`, hammering the API!
- Hands-On Verification (Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`)): Write a cross-field `dateRangeValidator` and a debounced `uniqueEmployeeCodeValidator` (`AsyncValidatorFn`).
4. Reference Implementation
export const matchPasswords: ValidatorFn = (g) => g.get('password')?.value === g.get('confirm')?.value ? null : { passwordMismatch: true };
5. Common Mistakes to Avoid
- Running an HTTP `AsyncValidatorFn` on every keystroke without `timer(350)` / `updateOn: 'blur'`, hammering the API!
- Skipping boundary, null/empty, or error-state verification when implementing Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`).
- Pushing untested Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) changes without clean naming and Git commit context.
6. Hands-On Exercise
Write a cross-field `dateRangeValidator` and a debounced `uniqueEmployeeCodeValidator` (`AsyncValidatorFn`).
7. Technical Interview Checkpoints
- Q: What problem does Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) solve in Angular Forms, RxJS & NgRx, and what fails if it is misused?
Key Focus: Validating Indian PAN/GSTIN format synchronously and checking employee-code uniqueness asynchronously on `updateOn: 'blur'`. Guard against: Running an HTTP `AsyncValidatorFn` on every keystroke without `timer(350)` / `updateOn: 'blur'`, hammering the API! - Q: Explain the internal working of Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) (Field `ValidatorFn` returning `ValidationErrors | null`, cross-field group validator (`password === confirmPassword`, `endDate >= startDate`), debounced `AsyncValidatorFn` checking email availability).
Key Focus: Walk step-by-step through Field `ValidatorFn` returning `ValidationErrors | null`, cross-field group validator (`password === confirmPassword`, `endDate >= startDate`), debounced `AsyncValidatorFn` checking email availability with a concrete code example. - Q: How did you apply and verify Custom Synchronous & Asynchronous Validators (`ValidatorFn`, `AsyncValidatorFn`) in your hands-on lab?
Key Focus: Write a cross-field `dateRangeValidator` and a debounced `uniqueEmployeeCodeValidator` (`AsyncValidatorFn`).