typescript
35 lines · 6 steps
How an async username validator works in Angular
A debounced, HTTP-backed reactive form validator that checks whether a username is already taken.
Explained by
highlit
1import { Injectable } from '@angular/core';
2import { HttpClient } from '@angular/common/http';
3import {
4 AsyncValidator,
5 AbstractControl,
6 ValidationErrors,
7} from '@angular/forms';
8import { Observable, of, timer } from 'rxjs';
9import { catchError, map, switchMap } from 'rxjs/operators';
10
11@Injectable({ providedIn: 'root' })
12export class UsernameAvailabilityValidator implements AsyncValidator {
13 constructor(private http: HttpClient) {}
14
15 validate(
16 control: AbstractControl,
17 ): Observable<ValidationErrors | null> {
18 const username = (control.value ?? '').trim();
19 if (!username) {
20 return of(null);
21 }
22
23 return timer(400).pipe(
24 switchMap(() =>
25 this.http.get<{ available: boolean }>('/api/users/availability', {
26 params: { username },
27 }),
28 ),
29 map(({ available }) =>
30 available ? null : { usernameTaken: { value: username } },
31 ),
32 catchError(() => of({ availabilityCheckFailed: true })),
33 );
34 }
35}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Async validators return an Observable so form validity can depend on a server round-trip.
- 2A leading timer plus switchMap debounces input and cancels stale in-flight requests.
- 3catchError keeps a failed network call from silently blocking form submission.
Related explainers
typescript
import { CanActivate, ExecutionContext, Injectable,
Role-based route guards in NestJS
authorization
decorators
metadata-reflection
Intermediate
8 steps
go
package auth import ( "net/http"
Building a JWT auth middleware in Gin
middleware
jwt
authentication
Intermediate
7 steps
typescript
import { CanActivate, ExecutionContext, Injectable,
A rate-limiting guard in NestJS
rate-limiting
guards
metadata
Intermediate
7 steps
typescript
interface Page<T> { items: T[]; nextCursor: string | null; }
Streaming cursor pagination with async generators
async-generators
pagination
generics
Intermediate
8 steps
typescript
type RequestState<T, E = string> = | { status: "idle" } | { status: "loading" } | { status: "success"; data: T; fetchedAt: number }
Modeling request state with discriminated unions
discriminated-unions
exhaustiveness-checking
state-machine
Intermediate
8 steps
typescript
import { Directive, Input, TemplateRef,
Building a structural *appUnless directive in Angular
structural-directive
template-rendering
dependency-injection
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-an-async-username-validator-works-in-angular-explained-typescript-d2b4/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.