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

Improve z.array(z.enum([])) and z.enum([]).array() #112

Open
bartoszgolebiowski opened this issue May 16, 2023 · 2 comments
Open

Improve z.array(z.enum([])) and z.enum([]).array() #112

bartoszgolebiowski opened this issue May 16, 2023 · 2 comments

Comments

@bartoszgolebiowski
Copy link

Hello, amazing lib. I see the field to improve it.

Currently, we have something like this:

const Colors = ["red", "green", "blue"] as const;

const FavoriteColor = z.enum(Colors).array(); // 
// const FavoriteColor = z.array().enum(Colors)

const mapping = [
  [FavoriteColor, MultiCheckbox],
] as const; 

const Schema = z.object({
  favoriteColor: FavoriteColor.describe("Favorite Color"),
});

const MultiCheckbox = (props: { options: string[] }) => {
  const { options } = props;
  ...
}

Const App = ()=> 
    <MyForm
      form={form}
      schema={Schema}
      props={{
        favoriteColor: {
          options: FavoriteColor._def.type.options,  
        },
      }}
    />

It works, but I want it to look like this!

const Colors = ["red", "green", "blue"] as const;

const FavoriteColor = z.enum(Colors).array(); // 
// const FavoriteColor = z.array().enum(Colors)

const mapping = [
  [FavoriteColor, MultiCheckbox],
] as const; 

const Schema = z.object({
  favoriteColor: FavoriteColor.describe("Favorite Color"),
});

const MultiCheckbox = () => {
  const options = useEnumValues();
  ...
}

Const App = ()=> 
    <MyForm
      form={form}
      schema={Schema}
    />

I added PR with a proposition for it.
Functionality + tests.

@bartoszgolebiowski bartoszgolebiowski changed the title Improve z.array(z.enum([])) and z.enum([]).array Improve z.array(z.enum([])) and z.enum([]).array() May 16, 2023
@bartoszgolebiowski
Copy link
Author

#113

@bartoszgolebiowski
Copy link
Author

I just linked my local repo with this newly added functionality and it looks like it works.

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

No branches or pull requests

1 participant