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

Fix: Zod instanceof not working in lib #50

Merged
merged 7 commits into from
Jul 23, 2024

Conversation

cau777
Copy link
Contributor

@cau777 cau777 commented Jul 11, 2024

Task: https://www.notion.so/Fix-instanceof-on-zod-9c21631b782043fdb67a42e8263a40dc?pvs=4

This is the added code to ZodType (there is no syntax highlighting on the GitHub diff):

// The instanceof operator does not work properly when ts-mongo is imported from another library
// https://stackoverflow.com/questions/59265098/instanceof-not-work-correctly-in-typescript-library-project
// This alternative implementation checks the constructor names of the object and its inherited classes
static [Symbol.hasInstance](instance) {
    if (
        typeof instance !== 'object' ||
        instance === null ||
        !('_def' in instance)
    )
        return false

    // Also check inherited prototypes
    const getPrototypeChain = (prototype) => {
        if (!prototype) return []
        return [prototype, ...getPrototypeChain(prototype.__proto__)]
    }

    const prototypes = getPrototypeChain(instance)
    const className = this.name
    return prototypes.some((proto) => proto.constructor.name === className)
}

The idea is to overwrite the instanceof operator on ZodType (all zod classes inherit it) code to check class names instead of constructor references (which are different when ts-mongo is used as a library).
We could submit this fix to zod but it seems PRs there take a while to reach production.
This PR was tested locally by importing it from the app using a dependency link.

@cau777 cau777 changed the title Fix: Do not use instanceof Fix: Make Zod a peer dependecy Jul 11, 2024
@cau777 cau777 changed the title Fix: Make Zod a peer dependecy Fix: Change instanceof usages Jul 11, 2024
@cau777 cau777 force-pushed the fix/do-not-use-instanceof branch from 9861245 to fe62e4c Compare July 22, 2024 17:56
@cau777 cau777 changed the title Fix: Change instanceof usages Fix: Zod instanceof not working in lib Jul 22, 2024
@cau777 cau777 force-pushed the fix/do-not-use-instanceof branch from 7928755 to fde9d24 Compare July 22, 2024 19:53
Copy link
Owner

@tianhuil tianhuil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great tests @cau777 !

@tianhuil tianhuil merged commit 36bd442 into tianhuil:main Jul 23, 2024
1 check passed
cau777 added a commit to cau777/ts-mongo that referenced this pull request Jul 23, 2024
tianhuil pushed a commit that referenced this pull request Jul 23, 2024
* Revert "Fix: Zod instanceof not working in lib (#50)"

This reverts commit 36bd442.

* Use zod from fork

* Test back

* Use commit
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