fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.6k stars 169 forks source link

Issue with enums #650

Closed ispyinternet closed 1 week ago

ispyinternet commented 3 weeks ago

tyring to use _enum

with javascript objects. It seems to me that it is only possible to successfully validate your value if the object key value pairs are the same. E.G:

This object works:

export const role = { 'Admin': 'Admin', 'User': 'User' } as const;

but this one doesn't:

export const role = { 'ADMIN': 'Admin', 'USER': 'User' } as const;

Unless of course I am doing something wrong 🫤

fabian-hiller commented 3 weeks ago

What does not work? It works for me. Take a look at this playground.

ispyinternet commented 3 weeks ago

hmm, I'm using this in conjuction with superforms. Do you think there is anywhere this could cause the problem, otherwise I guess it must be user error.

fabian-hiller commented 3 weeks ago

Are you using the latest version of Valibot and Superforms?

ispyinternet commented 3 weeks ago

It's a user error. I was expecting to be able to validate with the key 🤦‍♂️

Is there a pattern that would make this easy to do?

fabian-hiller commented 3 weeks ago

What is your use case? Have you read the enum guide?