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>
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { createTemplateSchema } from '@/lib/validators/document-templates';
|
|
|
|
const baseInput = {
|
|
name: 'Tmpl',
|
|
templateType: 'custom' as const,
|
|
bodyHtml: '<p>x</p>',
|
|
};
|
|
|
|
describe('createTemplateSchema — mergeFields allow-list', () => {
|
|
it('accepts valid tokens from the catalog', () => {
|
|
const parsed = createTemplateSchema.parse({
|
|
...baseInput,
|
|
mergeFields: ['{{client.fullName}}', '{{yacht.name}}', '{{berth.mooringNumber}}'],
|
|
});
|
|
expect(parsed.mergeFields).toEqual([
|
|
'{{client.fullName}}',
|
|
'{{yacht.name}}',
|
|
'{{berth.mooringNumber}}',
|
|
]);
|
|
});
|
|
|
|
it('rejects deprecated tokens that lived on `clients` before the refactor', () => {
|
|
const result = createTemplateSchema.safeParse({
|
|
...baseInput,
|
|
mergeFields: ['{{client.yachtName}}'],
|
|
});
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0]?.message).toMatch(/Unknown merge tokens.*client\.yachtName/);
|
|
}
|
|
});
|
|
|
|
it('rejects unknown tokens with a helpful message listing them', () => {
|
|
const result = createTemplateSchema.safeParse({
|
|
...baseInput,
|
|
mergeFields: ['{{client.fullName}}', '{{not.a.token}}', '{{also.bogus}}'],
|
|
});
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0]?.message).toContain('not.a.token');
|
|
expect(result.error.issues[0]?.message).toContain('also.bogus');
|
|
}
|
|
});
|
|
|
|
it('defaults mergeFields to an empty array when omitted', () => {
|
|
const parsed = createTemplateSchema.parse(baseInput);
|
|
expect(parsed.mergeFields).toEqual([]);
|
|
});
|
|
|
|
it('accepts an empty mergeFields array', () => {
|
|
const parsed = createTemplateSchema.parse({ ...baseInput, mergeFields: [] });
|
|
expect(parsed.mergeFields).toEqual([]);
|
|
});
|
|
|
|
it('allows the new `eoi` templateType', () => {
|
|
const parsed = createTemplateSchema.parse({ ...baseInput, templateType: 'eoi' });
|
|
expect(parsed.templateType).toBe('eoi');
|
|
});
|
|
});
|