-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 brand to ZodBranded def at runtime #2860
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
@colinhacks , please make branded types programmatically distinguishable. |
I was dreaming about it. Unfortunately `zod` does not provide any way to make a custom or branded or third-party schema that can be identified as one programmatically. Developer of `zod` also does not want to make any method to store metadata in schemas, instead, recommends to wrap schemas in some other structures, which is not suitable for the purposes of `express-zod-api`. There are many small inconvenient things in making custom schema classes, that I'd like to replace into native methods, and use `withMeta` wrapper for storing proprietary identifier, so the generators and walkers could still handle it. Related issues: ``` colinhacks/zod#1718 colinhacks/zod#2413 colinhacks/zod#273 colinhacks/zod#71 colinhacks/zod#37 ``` PR I've been waiting for months to merged (programmatically distinguishable branding): ``` colinhacks/zod#2860 ```
The fact that the type-only If you need this for your own purposes, Zod 4's plugin system will let you hack this a bit by adding another overload to |
Can not express my disappointment in this regard. colinhacks/zod#2860 (comment)
@colinhacks What do you think about allowing to put "unique symbol" from a user space code? |
Due to #1721, colinhacks/zod#2860 (comment), and #1719 I'm exploring the possibility to alter the behaviour of `.brand()` for storing the `brand` property as a way to distinguish the proprietary schemas in runtime. This can replace the `proprietary()` function with a call of `.brand()`. However, so far it turned out to be breaking because `ZodBranded` does not expose the methods of the wrapped schema, such as `.extend()`, which is used in `accept-raw.ts` endpoint for possible route params. —— After a series of serious considerations I realized that exposing brand to consumers of `express-zod-api` could be a beneficial feature. Runtime brand can be accessed via `._def[Symbol.for("express-zod-api")].brand`
Make brand available at runtime when used as a parameter for .brand()
With this brand() can be used to enable external code to operate on types based on definition.
Typing is left as-is to be backwards-compatible so instead of overriding the BRAND symbol this just puts the brand parameter of the constructor in _def.
Let me know what you think! 🙂