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

added z.date documentation to README.md. fixes #880 #881

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ These docs have been translated into [Chinese](./README_ZH.md).
- [Strings](#strings)
- [Numbers](#numbers)
- [Booleans](#booleans)
- [Dates](#dates)
- [Zod enums](#zod-enums)
- [Native enums](#native-enums)
- [Optionals](#optionals)
@@ -402,6 +403,29 @@ const isActive = z.boolean({
});
```

## Dates
z.date() accepts a date, not a date string
```ts
z.date().safeParse( new Date() ) // success: true
z.date().safeParse( '2022-01-12T00:00:00.000Z' ) // success: false
```

To allow for dates or date strings, you can use preprocess
```ts
const dateSchema = z.preprocess(
arg => {
if ( typeof arg == 'string' || arg instanceof Date )
return new Date( arg )
},
z.date()
)
type DateSchema = z.infer<typeof dateSchema>
// type DateSchema = Date

dateSchema.safeParse( new Date( '1/12/22' ) ) // success: true
dateSchema.safeParse( '2022-01-12T00:00:00.000Z' ) // success: true
```

## Zod enums

```ts