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

Make brand available as a runtime value #2480

Open
indianakernick opened this issue Jun 4, 2023 · 5 comments
Open

Make brand available as a runtime value #2480

indianakernick opened this issue Jun 4, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@indianakernick
Copy link

indianakernick commented Jun 4, 2023

I'm inspecting Zod values at runtime. I've run into a situation where I would like to use branded types but I've found that the brand isn't available at runtime. The brand function takes the brand as an optional runtime parameter. I'd like this value to be stored in the type definition. This is the code that I would like to be able to write:

const schema = z.number().brand('foo');

if (schema._def.typeName === 'ZodBranded') {
  if (schema._def.brand === 'foo') {
    // ...
  }
}

I'd also like this brand property’s type to be the brand type ('foo' in the above example) if a runtime value was provided or undefined if a value wasn’t provided.

As a workaround, I might be able to get away with using the description for my use case but I need the description as a description. The brand and the description serve different purposes.

Related: #1833

@stale
Copy link

stale bot commented Sep 3, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label Sep 3, 2023
@kasper573
Copy link

I think this would be a great feature

@stale stale bot removed the stale No activity in last 60 days label Sep 3, 2023
@JacobWeisenburger JacobWeisenburger added the enhancement New feature or request label Sep 23, 2023
@mpartel
Copy link

mpartel commented Dec 7, 2023

This could/would probably be used by various "Zod to X" converters that currently need the schema's name as a separate parameter.

@jpholtmeyer
Copy link

A PR for this issue has been created here: #2860

I also think this feature would be useful.
There doesn't currently seem to be an easy way to determine what 'type' a Zod object is at runtime.
Currently doing this by just checking if a unique property exists on an object by using the in operator.
This is cumbersome at times though because it occasionally requires unwrapping a Zod object to get to the shape of the business object.
Some objects also require checking a set of properties to determine uniqueness, which makes the code harder to follow.

@artalar
Copy link

artalar commented May 8, 2024

I will repeat it here too: the most reliable solution is to use "unique symbol" as runtime unique markers. I mean, it would be nice if the brand will be allow to accept a symbol for both runtime and type checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants