Kind: Interface
Source: packages/common/pipes/parse-bool.pipe.ts
Part of: Common
ParseBoolPipeOptions configures the behavior of NestJS's ParseBoolPipe, which converts incoming request values into boolean values. It lets applications customize the HTTP status code, exception creation, and whether missing values should be accepted without parsing.
Properties
| Property | Type |
|---|---|
errorHttpStatusCode | ErrorHttpStatusCode |
exceptionFactory | (error: string) => any |
optional | boolean |
Diagram
mermaidgraph LR A[Incoming request value] --> B[ParseBoolPipe] B --> C{Value is boolean-compatible?} C -->|Yes| D[Return boolean] C -->|No| E[exceptionFactory] E --> F[Throw configured exception] G[ParseBoolPipeOptions] --> B G --> H[errorHttpStatusCode] G --> I[exceptionFactory] G --> J[optional]
Usage
tsimport {
BadRequestException,
ParseBoolPipe,
type ParseBoolPipeOptions,
} from '@nestjs/common';
const options: ParseBoolPipeOptions = {
errorHttpStatusCode: 422,
optional: true,
exceptionFactory: (error) =>
new BadRequestException({
message: 'Expected a boolean query parameter.',
details: error,
}),
};
const parseBoolean = new ParseBoolPipe(options);
// Useful for values such as "true", "false", true, or false.
const enabled = await parseBoolean.transform('true', {
type: 'query',
data: 'enabled',
metatype: Boolean,
});
// enabled === true
AI Coding Instructions
- Use
optional: truewhen the value may benullorundefined; otherwise, invalid or absent values should trigger the configured exception. - Provide an
exceptionFactorywhen the application needs a consistent custom error response format. - Use
errorHttpStatusCodefor the default exception behavior; it may be unnecessary whenexceptionFactorycreates its own HTTP exception. - Apply these options when constructing
ParseBoolPipefor route parameters, query parameters, or request body fields that must be parsed as booleans.
Was this page helpful?