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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 5x 5x 5x 5x 26x 2x 5x 2x 1x 1x 13x 1x 2x 5x 12x 5x 1x | import type { FormError } from "@nuxt/ui";
import * as v from "valibot";
export type CommentIssue = "invalid" | "long" | "required";
export type CommentError = FormError & { message: CommentIssue };
export const MAX_COMMENT_LENGTH = 5000;
// WordPress's own column widths, which it would refuse past with a bare 400
const MAX_NAME_LENGTH = 245;
const MAX_EMAIL_LENGTH = 100;
type Message = (issue: CommentIssue) => string;
const text = (message: Message, maxLength: number) =>
v.pipe(
v.string(() => message("required")),
v.trim(),
v.nonEmpty(() => message("required")),
v.maxLength(maxLength, () => message("long")),
);
const id = (message: Message) =>
v.pipe(
v.string(() => message("invalid")),
v.regex(/^\d+$/, () => message("invalid")),
);
const fields = (message: Message) => ({
name: text(message, MAX_NAME_LENGTH),
email: v.pipe(
v.string(() => message("required")),
v.trim(),
v.nonEmpty(() => message("required")),
v.email(() => message("invalid")),
v.maxLength(MAX_EMAIL_LENGTH, () => message("long")),
),
content: text(message, MAX_COMMENT_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")),
),
});
/** What the reader types: the form validates this. */
export const commentSchema = (message: Message) =>
v.config(v.object(fields(message)), { abortPipeEarly: true });
/** What the server accepts: the reader's fields, the article, and the comment it replies to. */
export const newCommentSchema = (message: Message) =>
v.config(v.object({ ...fields(message), post: id(message), parent: id(message) }), {
abortPipeEarly: true,
});
export type CommentFields = v.InferOutput<ReturnType<typeof commentSchema>>;
export type NewComment = v.InferOutput<ReturnType<typeof newCommentSchema>>;
|