Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
ktarmyshov opened this issue Dec 17, 2024 · 1 comment
Open
1 task done
Labels
enhancement New feature or request
Milestone

Comments

@ktarmyshov
Copy link

  • 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 ktarmyshov added the bug Something isn't working label Dec 17, 2024
@ktarmyshov ktarmyshov changed the title Defaults from schema are overwritten by provided default data (Deep) Defaults from schema are overwritten by provided default data (not deep) Dec 17, 2024
@ciscoheat
Copy link
Owner

Related to #449

@ciscoheat ciscoheat added enhancement New feature or request and removed bug Something isn't working labels Dec 26, 2024
@ciscoheat ciscoheat added this to the v3 milestone Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants