git-zodyac / mongoose

Converts your Zod schemas into fully-functional Mongoose schemas
https://www.npmjs.com/package/@zodyac/zod-mongoose
24 stars 5 forks source link

unusable due to errors with `extendZod(z)` #17

Closed ttytm closed 3 days ago

ttytm commented 2 weeks ago

Calling extendZod(z) results in:

/<project_path>/node_modules/@zodyac/zod-mongoose/dist/index.cjs:38
  z_0.__zm_extended = true;
                    ^

TypeError: Cannot add property __zm_extended, object is not extensible
    at extendZod (/<project_path>/node_modules/@zodyac/zod-mongoose/dist/index.cjs:38:21)
    at file:///<project_path>/index.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:485:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5)
MikeWarriner commented 2 weeks ago

phew, I thought this was just me. Broken on Node 20,21 and 22 too.

bebrasmell commented 2 weeks ago

Hi! On it

bebrasmell commented 2 weeks ago

There's a fix in patch 2.0.1. Please let me know if it works

ttytm commented 2 weeks ago

Thanks for investigating!

Not quite there yet. The error changed though. Now the complain is related to objectId.

Dev/Kitchensink/mongodb-mongoose/node_modules/@zodyac/zod-mongoose/dist/index.cjs:64
  z_0.objectId = (ref) => {
               ^

TypeError: Cannot add property objectId, object is not extensible
    at extendZod (Dev/Kitchensink/mongodb-mongoose/node_modules/@zodyac/zod-mongoose/dist/index.cjs:64:16)
    at file://Dev/Kitchensink/mongodb-mongoose/index.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:106:12)

Tested with node 18-22 and bun

MikeWarriner commented 2 weeks ago

Yes, still broken.

I don't think you can modify the Zod z object in this way, at least this typescript code gives the error above and returns isSealed=true, isFrozen=true, isExtensible=false for the z object.

import { z } from "zod"; console.log("s",Object.isSealed(z)); console.log("f",Object.isFrozen(z)); console.log("e",Object.isExtensible(z)); import { extendZod } from "@zodyac/zod-mongoose"; extendZod(z);

bebrasmell commented 2 weeks ago

That's really strange thought all the tests are passing. Is there any specific typescript or bundler configuration you're using? It would be great to reproduce the bug

bebrasmell commented 2 weeks ago

That's a serious problem that seems to be affecting z.objectId() and z.mongoUUID() APIs only, so I guess the library has to take a step back to using zId and zUUID for now. Will rework it asap.

ttytm commented 2 weeks ago

That's really strange thought all the tests are passing. Is there any specific typescript or bundler configuration you're using? It would be great to reproduce the bug

Happens with a regular clean module. I'd usually use typescript, below is a simple repro with just JS.

Full repro

mkdir tmp && cd tmp \ 
  && npm init es6 -y \
  && npm i @zodyac/zod-mongoose
// index.js
import { z } from 'zod';
import { extendZod } from '@zodyac/zod-mongoose';

extendZod(z);
node index.js

Node v22.4.1
6.6.48-1-lts x86_64 GNU/Linux
MikeWarriner commented 2 weeks ago

Thank you - appreciate the quick response. I can't see why the tests pass, but if you add a simple file such as this in the example directory as simple.ts, the command

npx tsx example/simple.ts

returns

s truef truee false/Users/mikewarriner/Projects/git-zodyac-mongoose/src/extension.ts:75 (z_0).objectId = (ref?: string) => { ^TypeError: Cannot add property objectId, object is not extensible at extendZod (/Users/mikewarriner/Projects/git-zodyac-mongoose/src/extension.ts:75:14) at (/Users/mikewarriner/Projects/git-zodyac-mongoose/example/simple.ts:10:1) at ModuleJob.run (node:internal/modules/esm/module_job:262:25) at async onImport.tracePromise.proto (node:internal/modules/esm/loader:482:26) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)Node.js v22.7.0

import { z } from "zod"; import { extendZod } from "../src/extension";

console.log("s",Object.isSealed(z)); console.log("f",Object.isFrozen(z)); console.log("e",Object.isExtensible(z));

extendZod(z);

console.log("OK");

On Sat, 31 Aug 2024 at 15:15, Zak @.***> wrote:

That's a serious problem that seems to be affecting z.objectId() and z.mongoUUID() APIs only, so I guess the library has to take a step back to using zId and zUUID for now. Will rework it asap.

— Reply to this email directly, view it on GitHub https://github.com/git-zodyac/mongoose/issues/17#issuecomment-2322909969, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEZWB2PWB47ENCI3ELEL3DZUHFZTAVCNFSM6AAAAABNLQ5TPOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSHEYDSOJWHE . You are receiving this because you commented.Message ID: @.***>

bebrasmell commented 1 week ago

Please let me know if version 2.1.0 works for you

ttytm commented 1 week ago

Thanks for looking into it!

It doesn't fail with extendZod anymore, though trying to use a simple z.object results in:

114 |   for (const [key, field] of Object.entries(obj.shape)) {
115 |     if (field instanceof import_zod2.ZodObject) {
116 |       object[key] = parseObject(field);
117 |     } else {
118 |       const f = parseField(field);
119 |       if (!f) throw new Error(`Unsupported field type: ${field.constructor}`);
                          ^
error: Unsupported field type: class ZodString extends ZodType {
...
      at parseObject (Dev/Kitchensink/mongodb-mongoose/node_modules/@zodyac/zod-mongoose/dist/index.cjs:119:21)
      at zodSchema (Dev/Kitchensink/mongodb-mongoose/node_modules/@zodyac/zod-mongoose/dist/index.cjs:104:22)
      at Dev/Kitchensink/mongodb-mongoose/model/Blog.zod.ts:40:16

[..] That's really strange thought all the tests are passing.

Are there any actual tests?

bebrasmell commented 3 days ago

Okay, that is now fixed in 2.3.1. Please let me know if it works for you