NestJS / Intermediate: policies, adapters and sources / Lesson 14
Route parameter pipes and boundary validation
Parse and validate a route parameter before applying resource authorization and domain rules.
On this page
Lesson goals
- Use a built-in pipe for a route parameter.
- Distinguish parsing from authorization and existence checks.
- Test invalid values and unchanged state.
Define the parameter contract
A route parameter arrives as client-controlled text. Define its accepted form and range before using it as an identifier or quantity. A pipe can transform or reject a value at the controller boundary, but successful parsing does not prove that a resource exists or that the caller may access it. Keep authorization and domain rules explicit after parsing.
Apply a built-in pipe
The first fragment uses ParseIntPipe on an id parameter in an existing controller. A non-integer value is rejected before the controller method receives it. The service is application-defined and must still scope the lookup to the authenticated principal. Check the installed Nest version and configured exception behavior when documenting the exact error response; do not rely on a TypeScript number annotation alone for runtime validation.
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
return this.service.findOne(id);
}
The controller and service are application-specific. Parsing must be followed by resource authorization.
Validate domain constraints
The second fragment shows a small custom pipe rejecting non-positive numeric values. It assumes a number has already been parsed and is applied after the parsing pipe. Decide whether zero or other ranges are valid for the specific domain rather than treating this illustrative rule as universal. Keep error messages stable and safe, and avoid returning internal database or authorization details.
@Injectable()
class PositiveIdPipe implements PipeTransform<number, number> {
transform(value: number): number {
if (value <= 0) throw new BadRequestException('Invalid identifier');
return value;
}
}
Apply this illustrative pipe after numeric parsing. The correct range depends on the application domain.
Authorize the selected resource
After parsing, retrieve the resource within the principal’s permitted tenant or ownership scope. Test an existing ID owned by a different user as well as a nonexistent ID and an allowed ID. A valid integer and a successful database lookup do not grant access. Choose the public response for denial and absence according to the application’s disclosure policy.
Exercise the real pipeline
In a test application, send a valid parameter, a non-integer, an out-of-range value and an unauthorized resource identifier. Verify the actual configured pipes, status and safe response, and check that rejected writes leave durable state unchanged. Add a permitted control. A direct service unit test is useful for domain rules but does not prove that the HTTP parameter passed through the controller’s pipes.
Common errors and fixes
Text reaches numeric business logic unchecked
- Why it happens
- A TypeScript type annotation was treated as runtime validation.
- How to fix it
- Use a parsing pipe and explicit domain validation.
- Prevent it next time
- Test malformed path values through HTTP.
A valid ID exposes another tenant’s record
- Why it happens
- Parsing was confused with authorization.
- How to fix it
- Scope the lookup to the authenticated principal.
- Prevent it next time
- Test allowed and cross-tenant identifiers.
A pipe returns sensitive details
- Why it happens
- The error message includes internal state or raw secrets.
- How to fix it
- Return a stable safe validation error.
- Prevent it next time
- Inspect public responses for synthetic sensitive input.
2 practice exercises
1. Does ParseIntPipe prove that record 42 belongs to the caller?
Show worked solution
No. It establishes a parsed numeric value under its parsing rules. Retrieve the record within the authenticated principal’s authorized scope and test a different owner.
2. Why test a non-integer through HTTP instead of only calling the service?
Show worked solution
The HTTP test exercises the configured pipe and response mapping. The service unit test may bypass that boundary; use both for their distinct purposes.
Key takeaways
- A pipe enforces an input boundary, not resource authorization.
- Test parsing, domain constraints and access policy separately.