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

Open
wants to merge 2 commits into
base: beta
Choose a base branch
from

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.

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.

2 participants