Closed bbtgnn closed 2 months ago
This is the wrapper I'm trying to build:
import type { FormOptions as SuperformOptions } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import type { ZodObjectTypes, ZodValidation } from './types.temp';
import { defaults, superForm } from 'sveltekit-superforms/client';
import { z } from 'zod';
//
export type FormOptions<T extends ZodValidation<ZodObjectTypes>> = SuperformOptions<z.TypeOf<T>>;
export type SubmitFunction<T extends ZodValidation<ZodObjectTypes>> = NonNullable<
FormOptions<T>['onUpdate']
>;
export type CreateFormProps<T extends ZodValidation<ZodObjectTypes>> = {
schema: T;
onSubmit?: SubmitFunction<T>;
initialData?: Partial<z.TypeOf<T>>;
options?: FormOptions<T>;
};
export function createForm<T extends ZodValidation<ZodObjectTypes>>(props: CreateFormProps<T>) {
const { schema, initialData = {}, options = {}, onSubmit = () => {} } = props;
const adapter = zod(schema);
return superForm(defaults(initialData, adapter), {
SPA: true,
applyAction: false,
scrollToError: 'smooth',
validators: adapter,
dataType: 'json',
taintedMessage: null,
onUpdate: (e) => {
try {
if (e.form.valid) onSubmit(e);
} catch (e) {
console.log(e);
}
},
...options
});
}
Sure, I'll export them in the next release.
Added in 2.18.0
Hello! I have a small request: is it possible to export the types
ZodValidation
andZodObjectTypes
? I am writing a wrapper around superforms and I need those types to properly handle generics.