[Zod]: Default createSchemaFactory's coercion generic so refine callbacks keep their types - #6173
Open
ShreeBohara wants to merge 1 commit into
Open
Conversation
…acks keep their types Without a default, calling createSchemaFactory() without a `coerce` option left TCoerce at its CoerceOptions constraint, which distributes through the refine field types into a union of incompatible callbacks, so `(schema) => ...` refines became implicit any. Default to `undefined`, matching the top-level createSelectSchema/createInsertSchema exports. Fixes drizzle-team#5968
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5968.
createSchemaFactorydeclaresTCoerce extends CoerceOptionswith no default, so any call thatomits the
coercekey leavesTCoerceat the constraint itself rather than resolving to a concreteoption set. That constraint then distributes through the refine field types into a union of callbacks
with different parameter types, and TypeScript can no longer contextually type the callback:
It reproduces with
createSchemaFactory(),createSchemaFactory({}), andcreateSchemaFactory({ zodInstance: z })— anything withoutcoerce. Passingcoerce: trueor anobject works today, which is why the existing factory tests don't catch it: they all specify
coerce.Defaulting the parameter to
undefinedmatches what the top-levelcreateSelectSchema,createInsertSchemaandcreateUpdateSchemaexports already do — they are typedCreateSelectSchema<undefined>and friends — and it matches the runtime, which treats a missingcoerceas{}. Explicitcoerce: trueandcoerce: { date: true }still infer exactly as before.The test is added to
integration-tests/tests/validators/zod/pg.test.tsnext to the existingcoercion cases, and mirrors the shape of the
refine table - selecttest already there. Against thecurrent
rc5source it reports three errors without the change (the TS7006 above, plus thedownstream argument and
Expect<Equal<...>>failures) and none with it.drizzle-ormpackage tests pass: 1223 passed, 7 skipped.dprintandoxlintare clean on bothtouched files. The
type-testssuite reports 52 errors both with and without this change — they allcome from
effect/postgrestypings in my local install and are byte-identical on a cleanrc5checkout, so they're unrelated.
Two earlier PRs made the same one-line change, #5973 and #6031, but both targeted
mainand editeddrizzle-zod/src/schema.ts, which no longer exists on this branch; both were closed by their authorswithout review. This one is against
rc5and adds the regression test.