pablo-abc / felte

An extensible form library for Svelte, Solid and React
https://felte.dev
MIT License
1.01k stars 44 forks source link

Validation for field with a name contains dot will fail #220

Open winston0410 opened 1 year ago

winston0410 commented 1 year ago

Describe the bug

Validation for field with a name contains dot will fail

I notice that @felte/validator-zod will parse the name of field automatically, which resulted in unexpected validation result.

For example, having a field called traits.email, I expect this is the structure of the $error

  <!-- {"traits.email": null} -->

But this is what I am getting now

  <!-- {"traits":{"email":null}} -->

This make all validations fail and the form cannot be submitted.

Which package/s are you using?

felte (Svelte), @felte/validator-zod

Environment

    "@felte/reporter-svelte": "^1.1.5",
    "@felte/validator-zod": "^1.0.13",
    "felte": "^1.2.7",
    "zod": "^3.20.2"

To reproduce

Check https://github.com/winston0410/felte-zod-repo/blob/c39d253e5b6bd8db5b3e4fcc60a3213dbfac15d7/src/routes/%2Bpage.svelte#L24-L31

Small reproduction example

https://github.com/winston0410/felte-zod-repo

Screenshots

No response

Additional context

No response

winston0410 commented 1 year ago

digging into this deepr myself, I realise that the issue was the main package of felte parsed the string key into an object. To workaround this issue right now, adjusting the schema is the simplist fix

winston0410 commented 1 year ago

@pablo-abc just wondering is this behavior of felte expected?

pablo-abc commented 1 year ago

Yep, this is expected behaviour. Felte (and specially its validators) are made to handle what will eventually be a JSON payload. Dot notation is used in this case to represent nested objects. Validation in this case should be done by validating a nested object itself, not a property with a . in its name.