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

Unable to use a union() as a record() key value? #201

Closed
Saeris opened this issue Oct 6, 2023 · 4 comments
Closed

Unable to use a union() as a record() key value? #201

Saeris opened this issue Oct 6, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request priority This has priority workaround Workaround fixes problem

Comments

@Saeris
Copy link
Contributor

Saeris commented Oct 6, 2023

Trying to port some code from Zod and noticed this pattern doesn't appear to translate as easily as I'd like.

const localesSchema = z.union([
  z.literal(`en-US`),
  z.literal(`en-GB`),
  z.literal(`es-ES`),
  z.literal(`de`),
  z.literal(`fr`),
  z.literal(`it`),
  z.literal(`zh-CN`)
]) // type Locales = "en-US" | "en-GB" | "es-ES" | "de" | "fr" | "it" | "zh-CN"

const nameLocalizationsSchema = z.record(
  localesSchema,
  z.string().min(1).max(100)
) // type NameLocalizations = Record<Locales, string>

But what I would assume to be the equivalent code in Valibot doesn't appear to be valid

const localesSchema = union([
  literal(`en-US`),
  literal(`en-GB`),
  literal(`es-ES`),
  literal(`de`),
  literal(`fr`),
  literal(`it`),
  literal(`zh-CN`)
])

const nameLocalizationsSchema = record(
  localesSchema, // Error, not assignable to RecordKey
  string([minLength(1), maxLength(100)])
)

While you could argue that an enum is a better fit here than a union, it's still perfectly valid Typescript to use a string union as a Record key type. Would appreciate it if Valibot could support this!

@fabian-hiller fabian-hiller self-assigned this Oct 6, 2023
@fabian-hiller fabian-hiller added enhancement New feature or request priority This has priority labels Oct 6, 2023
@fabian-hiller
Copy link
Owner

I will try to improve in the next version. For now I recommend to use enumType as a workaround:

const localesSchema = enumType([
  'en-US',
  'en-GB',
  'es-ES',
  'de',
  'fr',
  'it',
  'zh-CN',
]);

const nameLocalizationsSchema = record(
  localesSchema,
  string([minLength(1), maxLength(100)])
);

@fabian-hiller fabian-hiller added the workaround Workaround fixes problem label Oct 6, 2023
@Saeris
Copy link
Contributor Author

Saeris commented Oct 6, 2023

Thanks that seems to work for now! Of course I'm reading through the source code to try and work out the API and usage information for all the functions so it wasn't clear how to do this migration.

@fabian-hiller
Copy link
Owner

We will try to provide a guide for migration in the future.

@fabian-hiller
Copy link
Owner

Fixed in v0.19.0

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

No branches or pull requests

2 participants