ciscoheat / sveltekit-superforms

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

Export types `ZodValidation` and `ZodObjectTypes` from `sveltekit-superforms/adapters` #479

Closed bbtgnn closed 2 months ago

bbtgnn commented 2 months ago

Hello! I have a small request: is it possible to export the types ZodValidation and ZodObjectTypes? I am writing a wrapper around superforms and I need those types to properly handle generics.

bbtgnn commented 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
    });
}
ciscoheat commented 2 months ago

Sure, I'll export them in the next release.

ciscoheat commented 2 months ago

Added in 2.18.0