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

Support non-string array in picklist #378

Closed
hyunbinseo opened this issue Jan 21, 2024 · 5 comments
Closed

Support non-string array in picklist #378

hyunbinseo opened this issue Jan 21, 2024 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request priority This has priority workaround Workaround fixes problem

Comments

@hyunbinseo
Copy link
Sponsor

According to the docs, picklist does not seem to limit the element's type.

For a set of values represented by an array, you can use the picklist schema function.

import * as v from 'valibot';

const Index = [0, 3, 7] as const; // readonly [0, 3, 7]
const IndexSchema = v.picklist(Index); // Error ts(2345)

However, passing a number array shows the following error.

Type readonly [0, 3, 7] is not assignable to type readonly string[].

@fabian-hiller
Copy link
Owner

Thank you for reporting this issue. I will review and improve the documentation. What is your use case for using numbers? Maybe we should extend picklist to all primitive literal values.

@fabian-hiller fabian-hiller self-assigned this Jan 21, 2024
@fabian-hiller fabian-hiller added documentation Improvements or additions to documentation enhancement New feature or request labels Jan 21, 2024
@hyunbinseo
Copy link
Sponsor Author

I am currently using this type guard inside my application

export const grades = [1, 2, 5, 6] as const;
export type Grade = (typeof grades)[number];
export const isValidGrade = (grade: number): grade is Grade => grades.includes(grade as Grade);

When validating the submitted form data, this is the best I have replicated.

coerce(number([custom(isValidGrade)]), Number);

const grade = parsed as Grade; // type casting is required.

@fabian-hiller
Copy link
Owner

fabian-hiller commented Jan 21, 2024

Maybe an union is the right choice for your use case:

import * as v from 'valibot';

const Schema = v.union([
  v.literal(1),
  v.literal(2),
  v.literal(5),
  v.literal(6),
]);

@fabian-hiller fabian-hiller added the workaround Workaround fixes problem label Jan 21, 2024
@fabian-hiller
Copy link
Owner

I will nevertheless investigate whether it makes sense to extend picklist.

@fabian-hiller fabian-hiller added the priority This has priority label Jan 21, 2024
@fabian-hiller
Copy link
Owner

v0.28.0 with this change is available

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

No branches or pull requests

2 participants