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 type coercion support to drizzle-zod #3852

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

L-Mario564
Copy link
Collaborator

@L-Mario564 L-Mario564 commented Dec 26, 2024

Addresses #3842 & #776.

This PR adds support for type coercion in schemas automatically generated with drizzle-zod.

Type coercion is disabled by default. We can configure it using the createSchemaFactory function:

const users = pgTable('users', {
  id: serial().primaryKey(),
  createdAt: timestamp().notNull().defaultNow()
});

// This configuration will only coerce dates
const { createSelectSchema } = createSchemaFactory({
  coerce: {
    date: true
  }
});

/*
  Same as:
  const selectUserSchema = z.object({
    id: z.number().min(...).max(...).int(),
    createdAt: z.coerce.date()
  });
*/
const selectUserSchema = createSelectSchema(users);

Docs: drizzle-team/drizzle-orm-docs#470.

@cybercoder-naj
Copy link

Any updates on this PR? I'm looking forward to this feature soon.

@slimshreydy
Copy link

who needs to review this PR for it to go in?

@nizarfadlan
Copy link

Is there any progress on this PR update, because I also have a similar problem with coerce.boolean

@AndriiSherman AndriiSherman merged commit 3dff816 into drizzle-team:beta Jan 23, 2025
7 checks passed
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

Successfully merging this pull request may close these issues.

5 participants