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

Issue with Conditional Validation in Form Schema Using zod #248

Open
jchatard opened this issue Jun 28, 2023 · 1 comment · May be fixed by #294
Open

Issue with Conditional Validation in Form Schema Using zod #248

jchatard opened this issue Jun 28, 2023 · 1 comment · May be fixed by #294
Assignees
Labels
bug Something isn't working

Comments

@jchatard
Copy link

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 jchatard added the bug Something isn't working label Jun 28, 2023
@sea-grass
Copy link

I ran into this issue as well for a project I'm working on. I'm interested in contributing a fix, cool if I pick this up, @pablo-abc?

I did some testing already -- a union Zod issue has nested issues under a unionErrors property. It also has an empty path, which is why the validator is currently skipping it. I'm planning to update validateSchema/shapeErrors to check the issue code for 'invalid_union' and surface its nested issues as part of the AssignableErrors that's returned from validate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants