-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add actual
for invalid_string
error in .regex()
#3552
Comments
@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: |
@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 |
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?
The text was updated successfully, but these errors were encountered: