Open lifeiscontent opened 3 months ago
@bjacobgordon there's an error in your code btw, if a user passed an invalid date you'd blindly consume it you can check for this as follows: new Date('0000-25-10 00:00:00').toString() === 'Invalid Date'
@bjacobgordon me personally, I don't think your problem overlaps with the ask in my initial issue. I don't have the ability to remove your comments, only you or the maintainers do.
and for what its worth, this is how I would of written your schema:
import { z } from "zod";
const z_iso9075DateTimeToDate = z
.string()
.regex(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)
.transform(
(dateInISO9075Format) => new Date(dateInISO9075Format.replace(" ", "T")),
)
.refine((date) => String(date) !== "Invalid Date", "Invalid Date");
const dateInISO9075Format = "2021-01-01 00:00:00";
const date = z_iso9075DateTimeToDate.parse(dateInISO9075Format);
console.log(date); // 2021-01-01T00:00:00.000Z
it's also unclear to me if you care about UTC, if you do, then I would definitely add a Z
at the end of the dateInISO9075Format.replace(" ", "T")
otherwise I believe the local time would be used instead
it would be nice to see the actual value when a regex doesn't match rather than having to look at the full object value for the schema that didn't parse
any objections to adding something like this?