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
| Property | Type |
|---|---|
optional | boolean |
default | () => Date |
errorHttpStatusCode | ErrorHttpStatusCode |
exceptionFactory | (error: string) => any |
Diagram
mermaidgraph 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
tsimport { 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: trueonly when an absent input should be accepted; provide adefaultcallback for the resulting value. - Keep
defaultas a function so a freshDateinstance is created for every pipe invocation. - Use
errorHttpStatusCodeconsistently with the API's validation-error conventions. - Ensure
exceptionFactoryreturns an exception shape recognized by the surrounding HTTP framework. - Include actionable parsing details in the
errormessage without exposing sensitive request data.
Was this page helpful?