- install these pacakges
npm i --save class-validator class-transformer
npm i @nestjs/mapped-types
- we use them to reject a Bad Request.
- A Pipe is Something a request passes through on it’s way to Controller
- Two Jobs → 1. Check the Request data ( rejects if it not good ) and 2 . Transform the Incoming Data
- let work by Buiding a small project.
- Patients, Doctors and Appionemnts - appiontment need both Patients and Doctors
concpets -
- Module — a labeled box that groups related things and controls what it shares. Your building's departments: Patients, Doctors, Appointments.
- Controller — the front desk. Receives HTTP requests and routes them to a service; does no real work. The receptionist who forwards you, never treats you.
- Service — the worker. Holds the real logic and data. The person who actually does the job.
- Dependency Injection (DI) — you name what you need in the constructor and Nest hands it to you; you never new it yourself. The receptionist asks for a doctor; the clinic provides one.
- @Injectable() — the worker signing up to be managed by Nest, so it can be handed out. Getting on the staff roster (providers).
- REST — the noun goes in the URL (/patients), the verb is the HTTP method. POST = create, GET = read, PATCH = update, DELETE = remove.
- @Param() vs @Body() — @Param pulls a value from the URL; @Body pulls the JSON payload from the request body. House number on the envelope vs. the letter inside.
- DTO (Data Transfer Object) — a class describing what a valid incoming request must look like. The entry form a visitor fills out. It only describes rules — a pipe enforces them.
- Validation (class-validator) — decorator rules on DTO fields: @IsString, @IsEmail, @IsUUID, @IsDateString, etc.
- PartialType — builds the update DTO from the create DTO with all fields optional, no rewriting. Photocopy the form, stamp "only what's changing." Keeps you DRY.
- Pipe — a checkpoint a request passes through before the controller; it can check (reject) and transform (clean up). A guard in the doorway. If it throws, the controller never runs.
- ValidationPipe — the built-in global pipe (set in main.ts) that enforces DTO rules everywhere. Options: whitelist (strip unknown fields), forbidNonWhitelisted (reject them), transform (fix types).
- Custom pipe — your own rule (FutureDatePipe: no booking in the past). The booking-desk guard who knows one extra rule. Format = DTO's job; business rule = pipe's job.