Kind: Interface
Source: packages/common/pipes/parse-array.pipe.ts
Part of: Common
ParseArrayPipeOptions configures how an array input is parsed and validated by ParseArrayPipe. It defines the expected item type, the delimiter used for string inputs, whether empty or missing values are allowed, and how parsing errors are converted into exceptions.
Properties
| Property | Type |
|---|---|
items | Type<unknown> |
separator | string |
optional | boolean |
exceptionFactory | (error: any) => any |
Diagram
mermaidgraph LR Input[Request input] --> Pipe[ParseArrayPipe] Pipe --> Separator[separator: split string input] Separator --> Items[items: validate each item type] Pipe --> Optional[optional: allow missing value] Items --> Result[Parsed array] Pipe --> Errors[Validation error] Errors --> Factory[exceptionFactory] Factory --> Exception[Custom exception]
Usage
tsimport { ParseArrayPipe, ParseArrayPipeOptions } from '@nestjs/common';
const options: ParseArrayPipeOptions = {
items: Number,
separator: ',',
optional: false,
exceptionFactory: (error) => {
throw new Error(`Invalid ID list: ${error}`);
},
};
const parseIds = new ParseArrayPipe(options);
// "1,2,3" becomes [1, 2, 3]
const ids = await parseIds.transform('1,2,3', {
type: 'query',
data: 'ids',
metatype: String,
});
AI Coding Instructions
- Provide
itemswhen each array element should be validated or transformed as a specific runtime type. - Use
separatorfor comma-separated query parameters or other delimited string inputs; ensure it matches the API contract. - Set
optional: trueonly when omitted or empty input should pass through without raising a validation error. - Use
exceptionFactoryto align parsing failures with the application's standard HTTP exception format. - Keep custom exception factories deterministic and avoid exposing raw validation details to clients.
Was this page helpful?