ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2.26k stars 67 forks source link

DeepPartial Objects for SuperValidate #449

Open CordlessWool opened 4 months ago

CordlessWool commented 4 months ago

Is your feature request related to a problem? Please describe. Currently SuperValidate accepts Partial<In>, but we have a nested object structure and want to set default values in these sub objects.

Describe the solution you'd like I know there is no default type for DeepPartial, but it would be nice to use DeepPartial<In> instead of just Partial<In>.

type DeepPartial<T> = T extends object ? {
    [P in keyof T]?: DeepPartial<T[P]>;
} : T;

Additional context Currently we work around with a type cast like

import { superValidate, InferIn } from 'sveltekit-superform';

superValidate({
  nested: {
    some: 'data'
  } as Partial<InferIn<TypeOfObject>>
});
ciscoheat commented 4 months ago

Good idea, will add to the next release if everything seems to work!

ciscoheat commented 4 months ago

It's a bit unfortunate, but since the default value of an array is an empty array, it won't traverse into arrays in the same way as objects. Changing the behavior would probably break some schemas (unions in arrays especially), and also require a bit of rewrite for the default value mapping, so I'm hesitant to add it to version 2.

I'll put it up on the v3 milestone for the time being, sorry.

ciscoheat commented 1 month ago

Another example of when DeepPartial is useful: https://www.sveltelab.dev/fharhjf6zn96b9u?files=.%2Fsrc%2Froutes%2F%2Bpage.svelte