felte icon indicating copy to clipboard operation
felte copied to clipboard

Zod Transforms not applying to values passed to onSubmit

Open alex-way opened this issue 3 years ago • 3 comments

Describe the bug

When overriding the onSubmit event handler, it seems that the values passed through are the raw values and aren't the values returned by the validator.

In my case I'm applying a simple transform to make the value 100 times it's original value.

I will say this may be expected functionality. However, my initial instinct is that the values passed through to the onSubmit handler, are already validated and therefore have ran through the parsing of the schema.

Happy to be told that the expectation is to parse the values again in the onSubmit handler.

Which package/s are you using?

@felte/validator-zod

Environment

  • OS: Windows 10
  • Browser: Microsoft Edge (No hate please)
  • Version: N/A

To reproduce

Please refer to the codesandbox example.

The expected alert should contain the value in the input box * 100.

Small reproduction example

https://codesandbox.io/s/modest-christian-8zygm8

Screenshots

No response

Additional context

No response

alex-way avatar Apr 25 '22 15:04 alex-way

Due to how data moves in Felte, Felte has no access to transformed values! Although there is a way to do this (validator-yup does this) I'm not sure why I had not added this on validator-zod. I'll keep this open and work on this. I need to see how this works with the fact that transforms on Felte must be synchronous.

pablo-abc avatar Apr 27 '22 14:04 pablo-abc

This would be great to have! If there's any way we can help via a PR, please us know.

jeremyjacob avatar Jan 09 '23 21:01 jeremyjacob

Well, apparenly @felte/validator* only does a validation to check if the data complies. A simple workaround would be to re-apply the schema.parse to values as shown below:

const {form} = createForm<z.infer<typeof xSchema>> ({
    validate: validateSchema(xSchema),
    onSubmit: (values) => {
       const pValues =  xSchema.parse(values);
       ...
    }
})

altunyurt avatar Nov 16 '23 08:11 altunyurt