Skip to content

ParseArrayPipeOptions

reference
1 min readUpdated

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

PropertyType
itemsType<unknown>
separatorstring
optionalboolean
exceptionFactory(error: any) => any

Diagram

mermaid
graph 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

ts
import { 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 items when each array element should be validated or transformed as a specific runtime type.
  • Use separator for comma-separated query parameters or other delimited string inputs; ensure it matches the API contract.
  • Set optional: true only when omitted or empty input should pass through without raising a validation error.
  • Use exceptionFactory to 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?

Download as PDF
ParseArrayPipeOptions — NestJS head-to-head