sveltekit-superforms icon indicating copy to clipboard operation
sveltekit-superforms copied to clipboard

Defaults from schema are overwritten by provided default data (not deep)

Open ktarmyshov opened this issue 11 months ago • 1 comments

  • [X] Before posting an issue, read the FAQ and search the previous issues.

Description Maybe a bug, maybe is supposed to be so. Please close if the latter. I have a complex schema (work with dataType: json), which provides some of the default values. Additionally I generate more data and this is called superForm(defaults(initialData, zod(schema)). If the default values are not in the top level of the object, the default values from schema get overwritten by the provided generated initialData.

defaults.js in superforms

    return {
        id: options?.id ?? validator.id ?? '',
        valid: false,
        posted: false,
        errors: {},
        data: { ...optionDefaults, ...data }, // this line should be something like _.merge by lodash
        constraints: validator.constraints,
        shape: validator.shape
    };

If applicable, a MRE

	const complexSchema = z.object({
		some1: z.object({
			some2: z.string().default('some2'),
			some3: z.string()
		})
	});

	const initialData = {
		some1: {
			some3: 'some3'
		}
	}
	const superFormClient = superForm(defaults(initialData, zod(complexSchema)), {
		invalidateAll: false,
		dataType: 'json',
		validators: zod(complexSchema)
	})
	const formClient = superFormClient.form;
        console.log('formClient', $formClient)

ktarmyshov avatar Dec 17 '24 13:12 ktarmyshov