postSchema

tcmts.content.test.postSchema · #va5j6mji · constant · runs anywhere

A post's data, checked by hand as a Standard Schema would: a title, and a day.

import type { StandardSchema } from "#codebase/tcmts/content";

export const postSchema: StandardSchema<{ title: string; date: string }> = {
  "~standard": {
    version: 1,
    vendor: "test",
    validate: (v) => {
      const o = (v ?? {}) as Record<string, unknown>;
      const issues = [
        ...(typeof o.title === "string" ? [] : [{ message: "a title, as text", path: ["title"] }]),
        ...(typeof o.date === "string" && /^\d{4}-\d{2}-\d{2}$/.test(o.date)
          ? []
          : [{ message: "a day, YYYY-MM-DD", path: [{ key: "date" }] }]),
      ];
      return issues.length
        ? { issues }
        : { value: { title: o.title as string, date: o.date as string } };
    },
  },
}

uses

used by