Skip to content

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

@bartoszgolebiowski

Description

@bartoszgolebiowski

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions