Kind: Interface
Source: packages/common/pipes/parse-int.pipe.ts
Part of: Common
ParseIntPipeOptions configures how NestJS's ParseIntPipe validates and transforms incoming values into integers. It lets you customize the HTTP status code or exception factory used for invalid values, and optionally allow null or undefined values to pass through unchanged.
Properties
| Property | Type |
|---|---|
errorHttpStatusCode | ErrorHttpStatusCode |
exceptionFactory | (error: string) => any |
optional | boolean |
Diagram
mermaidgraph LR A[Incoming request value] --> B[ParseIntPipe] B --> C{Value is null/undefined<br/>and optional?} C -->|Yes| D[Return original value] C -->|No| E{Valid integer?} E -->|Yes| F[Return parsed integer] E -->|No| G[Create validation exception] G --> H[errorHttpStatusCode] G --> I[exceptionFactory]
Usage
tsimport { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import type { ParseIntPipeOptions } from '@nestjs/common';
const parseIdOptions: ParseIntPipeOptions = {
errorHttpStatusCode: 422,
exceptionFactory: (error) => ({
statusCode: 422,
message: error,
error: 'Validation Error',
}),
optional: false,
};
@Controller('users')
export class UsersController {
@Get(':id')
findOne(
@Param('id', new ParseIntPipe(parseIdOptions))
id: number,
) {
return { id };
}
}
AI Coding Instructions
- Pass
ParseIntPipeOptionstonew ParseIntPipe(options)when endpoint-specific integer validation behavior is needed. - Use
errorHttpStatusCodefor standard HTTP error customization; useexceptionFactorywhen the application requires a custom exception or response shape. - Set
optional: trueonly for parameters that may legitimately benullorundefined; it does not make invalid non-empty strings valid integers. - Keep custom exception factories consistent with the application's global error-response conventions.
- Type option objects as
ParseIntPipeOptionswhen defining reusable pipe configuration.
How it works
ParseIntPipeOptions is the optional configuration interface accepted by ParseIntPipe’s constructor. The constructor replaces an omitted options object with {}. packages/common/pipes/parse-int.pipe.ts:17-34 packages/common/pipes/parse-int.pipe.ts:47-50
-
errorHttpStatusCode?: ErrorHttpStatusCodeselects the exception class used when integer validation fails, unlessexceptionFactoryis set. It defaults toHttpStatus.BAD_REQUEST(400). packages/common/pipes/parse-int.pipe.ts:21 packages/common/pipes/parse-int.pipe.ts:49-54 packages/common/enums/http-status.enum.ts:26
ErrorHttpStatusCodeis limited to the status codes that indexHttpErrorByCode; that map associates each allowed code with an exception class. packages/common/utils/http-error-by-code.util.ts:27-50 -
exceptionFactory?: (error: string) => anyoverrides status-code-based exception creation. On validation failure, the pipe calls it with the exact messageValidation failed (numeric string is expected)and throws its return value. packages/common/pipes/parse-int.pipe.ts:23-28 packages/common/pipes/parse-int.pipe.ts:52-54 packages/common/pipes/parse-int.pipe.ts:68-72 -
optional?: booleandefaults tofalse. When it is truthy and the input isnullorundefined,transform()returns that input without numeric validation or parsing. packages/common/pipes/parse-int.pipe.ts:30-33 packages/common/pipes/parse-int.pipe.ts:64-67 packages/common/utils/shared.utils.ts:48-49
Without the optional nullish branch, the pipe accepts only string or number values whose complete text matches an optional - followed by one or more digits and for which isFinite() is true; other values trigger the configured exception. Accepted values are returned as parseInt(value, 10). packages/common/pipes/parse-int.pipe.ts:68-85
Was this page helpful?