Skip to content

patch undefined over null type error #87

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

Open
franckdsf opened this issue May 6, 2025 · 1 comment
Open

patch undefined over null type error #87

franckdsf opened this issue May 6, 2025 · 1 comment

Comments

@franckdsf
Copy link

When creating a table with a null variable, it's possible to pass an undefined value to the patch function without TypeScript raising an error — even though this will crash at runtime.

export const test = defineTable({
  nullVariable: v.union(v.null(), v.string()),
  undefinedVariable: v.optional(v.string()),
})
await ctx.db.patch(testId, {
  nullVariable: undefined,   // <--------- no TypeScript error
  undefinedVariable: null    // <--------- TypeScript error
})

Expected behavior: TypeScript should warn that undefined is not a valid value for nullVariable.

@nipunn1313
Copy link
Collaborator

nipunn1313 commented May 8, 2025

This seems reasonable!

I was able to repro this failing at runtime

just convex run simple:nipunntest
✖ Failed to run function "simple:nipunntest":
Error: [Request ID: b6e6f4f5179064a9] Server Error
Uncaught Error: Failed to insert or update a document in table "test" because it does not match the schema: Object is missing the required field `nullVariable`. Consider wrapping the field validator in `v.optional(...)` if this is expected.

Object: {}
Validator: v.object({nullVariable: v.union(v.null(), v.string()), undefinedVariable: v.optional(v.string())})
    at async handler (../convex/simple.ts:36:27)

so having it fail at compile time seems like a benefit.

If someone would like to give it a shot -

The logic for this is in here

https://github.com/get-convex/convex-backend/blob/main/npm-packages/convex/src/server/data_model.ts

with tests here

https://github.com/get-convex/convex-backend/blob/main/npm-packages/convex/src/server/database.test.ts

I am unlikely to have time to work on this - but we'll take help if someone wants to try and figure it out.

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

No branches or pull requests

2 participants