Skip to content

ParseDatePipeOptions

reference
1 min readUpdated

Kind: Interface

Source: packages/common/pipes/parse-date.pipe.ts

Part of: Common

ParseDatePipeOptions configures how a date-parsing pipe handles incoming values and validation failures. It controls whether values may be omitted, supplies a fallback date, and defines how parsing errors are converted into HTTP exceptions.

Properties

PropertyType
optionalboolean
default() => Date
errorHttpStatusCodeErrorHttpStatusCode
exceptionFactory(error: string) => any

Diagram

mermaid
graph LR
  Input[Incoming date value] --> Pipe[ParseDatePipe]
  Pipe --> Optional{optional?}
  Optional -- Missing and allowed --> Default[default(): Date]
  Optional -- Value provided --> Parse[Parse and validate date]
  Parse -- Valid --> Date[Date result]
  Parse -- Invalid --> Factory[exceptionFactory(error)]
  Factory --> Error[HTTP exception]
  Status[errorHttpStatusCode] --> Factory

Usage

ts
import { ParseDatePipeOptions } from '@nestjs/common';

const datePipeOptions: ParseDatePipeOptions = {
  optional: true,
  default: () => new Date(),
  errorHttpStatusCode: 400,
  exceptionFactory: (error: string) => ({
    statusCode: 400,
    message: `Invalid date: ${error}`,
  }),
};

// Example: pass these options when configuring the date parsing pipe.

AI Coding Instructions

  • Set optional: true only when an absent input should be accepted; provide a default callback for the resulting value.
  • Keep default as a function so a fresh Date instance is created for every pipe invocation.
  • Use errorHttpStatusCode consistently with the API's validation-error conventions.
  • Ensure exceptionFactory returns an exception shape recognized by the surrounding HTTP framework.
  • Include actionable parsing details in the error message without exposing sensitive request data.

Was this page helpful?

Download as PDF
ParseDatePipeOptions — NestJS head-to-head