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

zod validation behaving differently if component is multipleCapable #115

Open
Viicos opened this issue Jan 24, 2024 · 0 comments
Open

zod validation behaving differently if component is multipleCapable #115

Viicos opened this issue Jan 24, 2024 · 0 comments

Comments

@Viicos
Copy link
Contributor

Viicos commented Jan 24, 2024

The following is the (simplified) cosignv2 component schema:

const buildDefaultValueSchema = (intl: IntlShape) => {
  const emailSchema = z
    .string()
    .email()
    .optional();

  return z.object({defaultValue: emailSchema});
};

const schema: EditSchema = ({intl}) => buildCommonSchema(intl).and(buildDefaultValueSchema(intl));

export default schema;

As it is, it does not allow a defaultValue of null.

This is the (simplified) email component schema, which is multiple capable:

const buildDefaultValueSchema = (intl: IntlShape) => {
  const emailSchema = z
    .string()
    .email()
    .optional();

  // case for when component.multiple=false
  const singleValueSchema = z
    .object({multiple: z.literal(false)})
    .and(z.object({defaultValue: emailSchema}));

  // case for when component.multiple=true
  const multipleValueSchema = z
    .object({multiple: z.literal(true)})
    .and(z.object({defaultValue: emailSchema.array()}));

  return singleValueSchema.or(multipleValueSchema);
};

const schema: EditSchema = ({intl}) => buildCommonSchema(intl).and(buildDefaultValueSchema(intl));

export default schema;

Similarly, defaultValue shouldn't accept null (or [null, ...]), but it does.

I wasn't really able to reproduce (see playground), so I suspect this is an (other) issue with the formik adapter library

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