Skip to content
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

Open
lifeiscontent opened this issue Jun 7, 2024 · 2 comments
Open

add actual for invalid_string error in .regex() #3552

lifeiscontent opened this issue Jun 7, 2024 · 2 comments

Comments

@lifeiscontent
Copy link

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?

@lifeiscontent
Copy link
Author

lifeiscontent commented Jun 10, 2024

@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'

@lifeiscontent
Copy link
Author

lifeiscontent commented Jun 11, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant