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

Provide reducers/reviviers for devalue in superForm/superValidate functions #423

Open
ktarmyshov opened this issue May 6, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ktarmyshov
Copy link

Is your feature request related to a problem? Please describe.
I'm using Decimal.js in zod schemas. When I use the superForm with dataType='json', posting data fails during devalue.stringify, because it cannot serialize custom type "Decimal".

Describe the solution you'd like
Feature request: provide possibility to pass reducers/revivers to superForm/superValidate to parse the request data accordingly.

Describe alternatives you've considered

    async onSubmit(input) {
      // Reduce custom types before submitting
      const formData = await superFormResult.validateForm();
      if (formData.valid) {
        const formJsonData = JSON.parse(JSON.stringify(formData.data));
        input.jsonData(formJsonData);
      }
    },

I also tried to fork and npm-link the devalue package for sveltekit (to provide default reducers/revivers for sveltekit), but superforms is using 4.3.3 and sveltekit 5.0.0 (which may still be different in the future). Even if I "override" the dependency to use 5.0.0, this one instantiated in a different (memory) context and default reducers/revivers working for sveltekit are empty for superforms - so "no go".

Additional context
None

@ktarmyshov ktarmyshov added the enhancement New feature or request label May 6, 2024
@AndreasHald
Copy link
Contributor

AndreasHald commented May 8, 2024

This would be really interesting and it's something we wan't aswell. We use the MongoDB ObjectId class.

There is a discussion about this in svelte kit aswell sveltejs/kit#9401 maybe if implemented there it would solve these issues aswell?

@AndreasHald
Copy link
Contributor

I tried creating a PR for this, by creating an object like this

export class Vector {
    x: number;
    y: number;
	constructor(x: number, y: number) {
		this.x = x;
		this.y = y;
	}
	magnitude() {
		return Math.sqrt(this.x * this.x + this.y * this.y);
	}
}


export const complexTypes: CustomTypeOptions = {
    reducers: {
        Vector: (value) => value instanceof Vector && [value.x, value.y]
    },
    revivers: {
        Vector: ([x,y]) => new Vector(x,y)
    },
    defaults: {
        Vector: () => new Vector(0,0)
    }
}

and providing it to SuperForms, a lot of stuff ends up working. But it breaks when you return data from a load function to the form, in the end at least as far as I can see, this needs to happen at a svelte kit level.

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