I have an enum mapping an external status string to an internal status string:
enum STATUS_MAP {
pending = internal.Pending,
engaged = internal.Pending,
pickup_confirmed = internal.Pending,
canceled = internal.Cancelled,
on_route = internal.Pending,
on_delivery = internal.Shipped,
delivered_recipients_here = internal.Delivered,
delivered_recipients_not_here = internal.Delivered,
}
If I understand correctly there is no combinator out of the box that takes this enum and returns a schema for the keys (pending
, engaged
, etc.) Am I correct?
If so, I’ll try to implement such a combinator and submit it as a PR this week-end.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Documentation - Keyof Type Operator - TypeScript
The keyof operator takes an object type and produces a string or numeric literal union of its keys. The following type P is...
Read more >TypeScript Keyof - W3Schools
keyof is a keyword in TypeScript which is used to extract the key type from an object type. keyof with explicit keys. When...
Read more >How to use the keyof operator in TypeScript - LogRocket Blog
The keyof operator takes an object type and produces a string or numeric literal union of its keys. A simple usage is shown...
Read more >How to Use the `keyof` Type Operator in TypeScript
In TypeScript 2.1, they introduced the keyof type operator. The keyof type operator takes an object type and creates a union type of...
Read more >In TypeScript, what do "extends keyof" and "in keyof" mean?
For any type T , keyof T is the union of known, public property names of T . Example: interface Person { age:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s one approach! But if I understand correctly, methods on schema objects are usually handy refinements, while this would be (conceptually, and probably implementation-wise too) a new primitive. Accordingly, I thought this would be neat as a method on the top-level object.
Here’s my proposal:
z.keyof(myEnum)
should infer a type equivalent tokeyof typeof myEnum
.z.keyof
should take anyRecord
, or subtype thereof (worst case scenario it infersstring
orstring | number
or whatever).ZodEnum
.In an ideal world we wouldn’t need this primitive, it would just be done via
z.enum(Object.keys(myEnum)
. HoweverObject.keys
returns notkeyof typeof myEnum
butstring[]
. This is because some objects have more keys than is known at compile-time.. I claim that this issue is not likely to be encountered in Zod, but if you disagree then I’m happy to table that and look for ways to only work with/support native enums.My apologies, I wrote this first thing in the morning and it looks like I hadn’t completely woken up. I looked at your example and I understand exactly what you mean. I think your best bet is a call to
refine
on the top-level object, i.e. the one that contains both the record and the key-valued fields.I would tentatively advocate against adding a combinator addressing this particular use case to the core Zod library. The issue is that this is a kind of validation that’s both non-local (the result can depend on the value of another field that can be arbitrarily far) and dynamic (the specific validity conditions aren’t known until runtime). This would make it very difficult to report clear errors. It’s also difficult for me to conceive of a “best” design that would cleanly address this and related use cases; the more likely result is a proliferation of special-case combinators, which is something we’re trying to avoid.