All files / apps/huella-legal/layers/forms/server/utils assertForm.ts

100% Statements 6/6
75% Branches 3/4
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22        5x       23x   23x   14x     32x            
import type { FormError } from "@nuxt/ui";
import * as v from "valibot";
 
/** Returns `input` parsed by `schema`, or throws `422` with one `FormError` per issue. */
export const assertForm = <S extends v.GenericSchema>(
    schema: S,
    input: unknown,
): v.InferOutput<S> => {
    const result = v.safeParse(schema, input);
 
    if (result.success) return result.output;
 
    throw createError({
        statusCode: 422,
        statusMessage: "Invalid form",
        data: result.issues.map((issue): FormError => ({
            name: v.getDotPath(issue) ?? "",
            message: issue.message,
        })),
    });
};