All files / apps/huella-legal/layers/contact/shared/schemas contact.ts

92.85% Statements 13/14
100% Branches 0/0
90% Functions 9/10
92.85% Lines 13/14

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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40              3x 3x   3x 22x 1x   5x 2x     3x 11x           1x 3x         1x 3x              
import type { FormError } from "@nuxt/ui";
import * as v from "valibot";
 
export type ContactIssue = "invalid" | "long" | "required";
 
export type ContactError = FormError & { message: ContactIssue };
 
const MAX_NAME_LENGTH = 245;
const MAX_MESSAGE_LENGTH = 5000;
 
const text = (message: (issue: ContactIssue) => string, maxLength: number) =>
    v.pipe(
        v.string(() => message("required")),
        v.trim(),
        v.nonEmpty(() => message("required")),
        v.maxLength(maxLength, () => message("long")),
    );
 
export const contactSchema = (message: (issue: ContactIssue) => string) =>
    v.config(
        v.object({
            name: text(message, MAX_NAME_LENGTH),
            email: v.pipe(
                v.string(() => message("required")),
                v.trim(),
                v.nonEmpty(() => message("required")),
                v.email(() => message("invalid")),
            ),
            message: text(message, MAX_MESSAGE_LENGTH),
            // A boolean that must be true, not `literal(true)`, so a checkbox's state fits the input
            consent: v.pipe(
                v.boolean(() => message("required")),
                v.value(true, () => message("required")),
            ),
        }),
        { abortPipeEarly: true },
    );
 
export type Contact = v.InferOutput<ReturnType<typeof contactSchema>>;