Okay, let's break down how to define a Zod schema for values that will be compatible with PostgreSQL's TIMESTAMP data type.
PostgreSQL's TIMESTAMP (or more commonly TIMESTAMPTZ for "timestamp with time zone") stores a point in time. When you interact with it in Node.js using a library like pg, it's typically converted into a JavaScript Date object.
Therefore, the most natural and robust way to validate data intended for a PostgreSQL TIMESTAMP column using Zod is to validate that it is a valid JavaScript Date object.
Here are the common scenarios and how to handle them with Zod:
Scenario 1: You expect the input to already be a JavaScript Date object
This is the simplest case.
import { z } from 'zod';
// Validates that the input is a Date instance AND that it's not "Invalid Date"
const postgresTimestampSchema = z.date();
// Example usage:
const validDate = new Date();
const invalidDate = new Date('invalid date string'); // This creates a Date object, but getTime() is NaN
console.log(postgresTimestampSchema.safeParse(validDate).success); // true
console.log(postgresTimestampSchema.safeParse(invalidDate).success); // false
console.log(postgresTimestampSchema.safeParse("2023-10-27").success); // false (not a Date object)
Scenario 2: You expect the input to be a string (e.g., ISO 8601) and want to validate and potentially transform it to a Date object
This is very common when receiving data from APIs (JSON uses strings for dates). You want to ensure the string is a valid date string and then maybe work with it as a Date object within your application.
You can use z.preprocess for this, which attempts to parse the input before passing it to another schema (z.date() in this case).
import { z } from 'zod';
const postgresTimestampSchema = z.preprocess((arg) => {
if (typeof arg === 'string' || arg instanceof Date) {
const date = new Date(arg);
// Return the Date object if valid, otherwise return something
// that z.date() will fail on (like undefined)
return isNaN(date.getTime()) ? undefined : date;
}
// If it's not a string or Date, return undefined so z.date() fails
return undefined;
}, z.date()); // Now validate the result as a valid Date object
// Example usage:
console.log(postgresTimestampSchema.safeParse("2023-10-27T10:00:00Z").success); // true
console.log(postgresTimestampSchema.safeParse(new Date()).success); // true
console.log(postgresTimestampSchema.safeParse("invalid date string").success); // false
console.log(postgresTimestampSchema.safeParse(1678886400000).success); // false (can be extended to handle numbers if needed)
Scenario 3: You want to validate it's an ISO 8601 string and keep it as a string
Less common if you're immediately sending to a DB driver that accepts Date objects, but useful if you need to validate the string format specifically.
import { z } from 'zod';
const isoStringTimestampSchema = z.string().refine((val) => {
const date = new Date(val);
// Check if creating a Date object from the string results in a valid date
return !isNaN(date.getTime());
}, {
message: "Invalid date string (expected ISO 8601 compatible format)"
});
// Example usage:
console.log(isoStringTimestampSchema.safeParse("2023-10-27T10:00:00Z").success); // true
console.log(isoStringTimestampSchema.safeParse("2023-10-27").success); // true (Date constructor handles YYYY-MM-DD)
console.log(isoStringTimestampSchema.safeParse("invalid date string").success); // false
console.log(isoStringTimestampSchema.safeParse(new Date()).success); // false (not a string)
Recommendation:
For compatibility with most Node.js PostgreSQL drivers and for working with dates as actual date objects in your application, the z.preprocess approach (Scenario 2) is often the most flexible and robust. It allows you to accept both strings and Date objects as input while guaranteeing that the output (if validation passes) is a valid JavaScript Date object ready to be sent to the database.
If you know your input will always be a Date object, Scenario 1 (z.date()) is perfectly fine.