question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date type handling?

See original GitHub issue

This lib looks like a really nice approach. I don’t see support for Date types, though. Do you have a plan to support them?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
colinhackscommented, Apr 9, 2020

Hey Joe,

So you want to be able to pass in, say, an ISO string to a Date field and have .parse return a proper Date object in its place?

const Person = z.object({
  createdAt: z.date()
})

const person = Person.parse({
  createdAt: `2020-04-09T22:11:52.520Z`
}) // passes

person.createdAt // Date object

I’m trying to keep this library from becoming a casting library, since casting is generally messy and difficult, especially when you’re dealing with static types. Currently the above code wouldn’t pass static validation, sincezod expects Person.createdAt to be a Date. To get it to work, I’d have to create a variant of the inferred schema type that changes all Date fields to Date | string, to accommodate the loosened restrictions on the input to .parse. Over time these “loosenings” would get messier and less intuitive.

I’d rather implement this is a refinement string type:

const Person = z.object({
  createdAt: z.string().refine({ is: 'isoString' });
})

I’m already planning to implement other built-in refinement validators ({ is: 'email' }, { is: 'url' }, etc.).

This way you could parse your schema, then be confident that passing Person.createdAt into Date.parse will return a valid date. Would that work for you?

0reactions
maneetgoyalcommented, Aug 25, 2020

Great 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

<input type="date"> - HTML: HyperText Markup Language
At the moment, the best way to deal with dates in forms in a cross-browser way is to have the user enter the...
Read more >
Handling date and time the right way | by Yuriy Ivon
The general rule for “pure” dates is pretty simple — such values must not be converted at any step of reading and writing....
Read more >
Date & Time Data Types - Snowflake Documentation
This topic describes the data types supported in Snowflake for managing dates, times, and timestamps (combined date + time). It also describes the...
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information. With sufficient knowledge...
Read more >
A Guide to Handling Date and Time for Full Stack JavaScript ...
Formatting the date ... One way to format a date is to use the getter functions like getFullYear, getMonth, getDate, etc. For example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found