refactor(templates): merge-field allow-list rejects unknown tokens

Extracts the MERGE_FIELDS catalog out of the document-templates service
into src/lib/templates/merge-fields.ts so the Zod validator can import
it without circular deps. createTemplateSchema now refines mergeFields
against VALID_MERGE_TOKENS — unknown tokens (including the deprecated
`{{client.yachtName}}` / `{{client.companyName}}` family) are rejected
at template creation time with a message naming the offenders.

Adds the missing `eoi` value to templateType enum so seeded EOI rows
round-trip through the validator. Drops the historical "Removed (PR 11):"
comment from the catalog (per project convention against `// removed`
markers).

6 new validator unit tests; 652/652 green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Ciaccio
2026-04-26 13:48:06 +02:00
parent f4ec51002c
commit 456d399ee2
4 changed files with 158 additions and 70 deletions

View File

@@ -1,11 +1,25 @@
import { z } from 'zod';
import { baseListQuerySchema } from '@/lib/api/route-helpers';
import { VALID_MERGE_TOKENS } from '@/lib/templates/merge-fields';
const mergeFieldsSchema = z
.array(z.string())
.optional()
.default([])
.refine(
(tokens) => tokens.every((t) => VALID_MERGE_TOKENS.has(t)),
(tokens) => {
const unknown = tokens?.filter((t) => !VALID_MERGE_TOKENS.has(t)) ?? [];
return { message: `Unknown merge tokens: ${unknown.join(', ')}` };
},
);
export const createTemplateSchema = z.object({
name: z.string().min(1).max(200),
description: z.string().max(500).optional(),
templateType: z.enum([
'eoi',
'welcome_letter',
'handover_checklist',
'acknowledgment',
@@ -13,7 +27,7 @@ export const createTemplateSchema = z.object({
'custom',
]),
bodyHtml: z.string().min(1),
mergeFields: z.array(z.string()).optional().default([]),
mergeFields: mergeFieldsSchema,
isActive: z.boolean().default(true),
});