managarten/packages/website-blocks/src/schemas.ts
Till JS f20ace0358 test(website): broad automated coverage across the builder surface
83 new tests across 5 files — pure-logic, fast, run on every
push. Caught one real bug + motivated one small refactor.

Coverage:

- apps/mana/.../website/constants.test.ts (8): isValidSlug + RESERVED_SLUGS
  + isValidPath. Caught the 1-char-slug bug (regex allowed length 1;
  UI + plan say min 2). Fixed the regex in both the webapp and the
  mirrored server list.
- apps/mana/.../website/publish.test.ts extended (8 total): adds
  self-parent cycle, 3-level nesting, all-orphans, empty-input cases
  on top of the original determinism + orphan-drop tests.
- apps/mana/.../website/templates.test.ts (7): parameterised over each
  of the 4 bundled templates — clone produces fresh UUIDs, page +
  block counts match, navConfig populated. Plus unknown-template and
  duplicate-slug rejection. Container-nesting is punted to the smoke
  test (none of the bundled templates use columns yet).
- packages/website-blocks/src/schemas.test.ts (38): every block
  (11) + sanity-checks (defaults satisfy own schema, enum + length
  bounds, required fields). Pure Zod — no Svelte runtime needed.
- packages/website-blocks/src/themes/themes.test.ts (12): preset
  parity, resolveTheme overrides, themeCssVars output format +
  heading-font fallback.
- apps/api/src/modules/website/reserved-slugs.test.ts (10): mirror of
  the client tests for the server SSOT, plus new hostname validation
  cases (.mana.how reservation, length, malformed edges).

Refactor:

- apps/api/src/modules/website/reserved-slugs.ts now owns
  isValidHostname + RESERVED_HOSTNAMES. domains.ts imports them.
  Pure functions live next to the other pure validators; easier to
  test + share.

All 83 new tests green. Web-app svelte-check + apps/api type-check
both clean. Existing publish.test.ts / website-blocks tests still
pass (the monorepo-wide count is now well above 83 — these are
the new ones from this commit).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 21:07:40 +02:00

62 lines
2.2 KiB
TypeScript

/**
* Pure-Zod schema aggregation — no Svelte components imported here.
*
* This side-channel lets tests + server-side validators pull every
* block's Zod schema without triggering the .svelte imports that
* `registry.ts` needs (which drag in Svelte runtime and trip up
* plain node/bun vitest runners).
*
* Keep the two in lockstep: a new block goes into both.
*/
import { HeroSchema, HERO_DEFAULTS } from './hero/schema';
import { RichTextSchema, RICH_TEXT_DEFAULTS } from './richText/schema';
import { CtaSchema, CTA_DEFAULTS } from './cta/schema';
import { ImageSchema, IMAGE_DEFAULTS } from './image/schema';
import { GallerySchema, GALLERY_DEFAULTS } from './gallery/schema';
import { FaqSchema, FAQ_DEFAULTS } from './faq/schema';
import { FormSchema, FORM_DEFAULTS } from './form/schema';
import { ModuleEmbedSchema, MODULE_EMBED_DEFAULTS } from './moduleEmbed/schema';
import { AnalyticsSchema, ANALYTICS_DEFAULTS } from './analytics/schema';
import { ColumnsSchema, COLUMNS_DEFAULTS } from './columns/schema';
import { SpacerSchema, SPACER_DEFAULTS } from './spacer/schema';
import type { ZodTypeAny } from 'zod';
export const BLOCK_SCHEMAS: Record<string, ZodTypeAny> = {
hero: HeroSchema,
richText: RichTextSchema,
cta: CtaSchema,
image: ImageSchema,
gallery: GallerySchema,
faq: FaqSchema,
form: FormSchema,
moduleEmbed: ModuleEmbedSchema,
analytics: AnalyticsSchema,
columns: ColumnsSchema,
spacer: SpacerSchema,
};
export const BLOCK_DEFAULTS: Record<string, unknown> = {
hero: HERO_DEFAULTS,
richText: RICH_TEXT_DEFAULTS,
cta: CTA_DEFAULTS,
image: IMAGE_DEFAULTS,
gallery: GALLERY_DEFAULTS,
faq: FAQ_DEFAULTS,
form: FORM_DEFAULTS,
moduleEmbed: MODULE_EMBED_DEFAULTS,
analytics: ANALYTICS_DEFAULTS,
columns: COLUMNS_DEFAULTS,
spacer: SPACER_DEFAULTS,
};
export function safeValidateSchema(
type: string,
props: unknown
): { success: true; data: unknown } | { success: false; error: unknown } {
const schema = BLOCK_SCHEMAS[type];
if (!schema) return { success: false, error: new Error(`Unknown block type "${type}"`) };
const parsed = schema.safeParse(props);
if (parsed.success) return { success: true, data: parsed.data };
return { success: false, error: parsed.error };
}