felte icon indicating copy to clipboard operation
felte copied to clipboard

Issue with Conditional Validation in Form Schema Using zod

Open jchatard opened this issue 1 year ago • 1 comments

Describe the bug

Hello,

I'm currently working on a project where I use zod to create and validate form schemas. My forms can have different fields as well as different validation rules, depending on the value of a preceding field.

Here's an illustration of the code I'm using:

MySchema() {
  const variantABC = z.object({
    ...CommonFields(),
    ...FieldA(),
    ...FieldB(),
    ...FieldC(),
  });

  const variantADE = z.object({
    ...CommonFields(),
    ...FieldA(),
    ...FieldD(),
    ...FieldE(),
  });

  return z.union([variantABC, variantADE]);
}

In this specific case, depending on the value of FieldA(), either FieldB() and FieldC() or FieldD() and FieldE() will be shown.

However, I'm encountering an issue where validation only seems to work for FieldA(), FieldB(), and FieldC(). If the value of FieldA() is set to hide FieldB() and FieldC(), as expected, FieldD() and FieldE() are displayed. Unfortunately, validation doesn't seem to occur for these fields.

Is there a solution or workaround that would allow me to dynamically validate these fields based on the conditions I have set? Any insights or suggestions would be greatly appreciated.

Best Regards, Jérémy

Which package/s are you using?

@felte/solid (SolidJS), @felte/reporter-solid, @felte/validator-zod

Environment

  • OS: macOS
  • Browser: Chrome latest

Version:

"@felte/reporter-solid": "^1.2.5",
"@felte/solid": "^1.2.7",
"@felte/validator-zod": "^1.0.13",

To reproduce

No response

Small reproduction example

No response

Screenshots

No response

Additional context

No response

jchatard avatar Jun 28 '23 07:06 jchatard