-
Notifications
You must be signed in to change notification settings - Fork 0
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 zod generator #1
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a few minor nits, and can you run prettier?
src/server/templates/zod.ts
Outdated
'json', | ||
'jsonb', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is json a string?
src/server/templates/zod.ts
Outdated
if (["date", "time", "timetz", "timestamp", "timestamptz", "timestamp with time zone"].includes(pgType)) { | ||
return 'date()' | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not correct. dates must be strings. for with timezone types we also need to set the offset
parameter to true.
src/server/templates/zod.ts
Outdated
methods.push(`regex(/${UUID_REGEX}/)`) | ||
|
||
if (column.default_value === "gen_random_uuid()") { | ||
methods.push("default(uuidv4)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets not add default values to the zod schema -> they are added in the BE anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise you might have a case where someone queries the backend without the id
column, passes it through the zod schema and then has a wrong id
set.
src/server/templates/zod.ts
Outdated
|
||
// UUID | ||
if (column.format === "uuid") { | ||
methods.push(`regex(/${UUID_REGEX}/)`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not .uuid()
?
src/server/templates/zod.ts
Outdated
// Date and time types | ||
if (["date", "time", "timetz", "timestamp", "timestamptz"].includes(column.format)) { | ||
if (column.default_value === "now()") { | ||
methods.push("default(() => new Date())") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no default value please
return methods | ||
} | ||
|
||
function extractGeneralZodMethods(column: PostgresColumn): string[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets rename it to extractNullabesAndOptionals
to something better than "general"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's too specific in my opinion
src/server/templates/zod.ts
Outdated
|
||
const output = ` | ||
import * as z from 'zod' | ||
import { v4 as uuidv4 } from 'uuid' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove (see below)
@psteinroe to speed up your review process: You can ignore the "apply linter" commit, as it only applies the linter but doesn't change any logic |
Pre-Review
Mainly inspired by the typescript generator; added some extra zod related stuff