sveltekit-superforms: Odd behavior: when missing a data field, uses server instead of custom validators
I’m developing a TypeBox adapter for Superform and encountered an odd behavior.
If I provide superForm with a validators object and my custom superValidate returns a data object that is missing a value for a property found in the form, superForm completely ignores the validators object and instead hits the server for validation.
Your superValidate provides defaults for all fields, and I can certainly do that, so this is not a problem for me.
I realize that the protocol between superForm and superValidate is an internal protocol that need not provide robust support for adapters like mine, but I thought I’d report the behavior in case you might be inclined to instead emit an error so that folks making adapters (all one of us?) aren’t spending hours debugging the problem.
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 20 (8 by maintainers)
The missing property problem that you initially reported should be fixed now with 1.5.1. All validators are now checked against missing properties, and a console error is now reported if a validator throws an exception.
I’m now questioning the suitability of using Superforms with TypeBox. The Zod ecosystem is rich, while there is very little for TypeBox. When people choose TypeBox, it is because they’re prioritizing performance.
To prioritize performance with Superforms, I would compile and cache each schema’s form-parsing code. But then even better performance could be gained by submitting JSON and relying on the built-in JSON-parsing library. And if we’re submitting JSON, we have JS on the client and so little need to use SvelteKit form actions.
But I still highly value the amazing work you’ve done on form usability, so I’ll be thinking of the best way to leverage that.
In case you’re curious, I’ve written probably 95% of what is needed to support TypeBox with the Superforms approach. I didn’t quite finish the async
superValidate(), but the missing bits are bits you’d understand better than I would. The port gave me quite an education. Thank you for that!Why do form actions assign defaults for missing values after submission to the server? This is an expensive process to perform, especially when the defaults should already have been provided client-side.
The main reason people choose TypeBox over Zod is that it benchmarks at about 200 times faster. I’d rather try to keep a TypeBox solution performant.
It’s already a part of the public interface, a type called
SuperValidated: https://github.com/ciscoheat/sveltekit-superforms/blob/54ac25fc49ad960085bcee1ba02145229d524f33/src/lib/index.ts#L147Its
datafield is the inferred schema type, so the data must always be set according to the types. Then it depends on the schema if a field has a default value or not. The rules are described here: https://superforms.rocks/default-valuesIf you can figure out where this silent error occurs, I can probably add an error in a future release.