Question Details

No question body available.

Tags

nestjs swagger openapi zod

Answers (1)

Accepted Answer Available
Accepted Answer
March 8, 2026 Score: 2 Rep: 108 Quality: Medium Completeness: 50%

Use z.coerce.date() with a custom transformer, or use z.string().datetime() with serialization

The cleanest approach is to transform the Date to a string at the schema level, which satisfies both runtime validation and Swagger generation.

import { z } from "zod"
import { createZodDto } from "nestjs-zod"

export const registerResponseSchema = z.object({ id: z.string(), username: z.string(), created_at: z.preprocess( (val) => (val instanceof Date ? val.toISOString() : val), z.string().datetime() ), })

export class RegisterResponse extends createZodDto(registerResponseSchema) {}