feat(publish): validatePost prüft a-tag-format

This commit is contained in:
Jörg Lohrer 2026-04-21 09:19:35 +02:00
parent 4986eae592
commit 1b0872a93f
2 changed files with 41 additions and 0 deletions

View File

@ -27,4 +27,15 @@ export function validatePost(fm: Frontmatter): void {
if (!(fm.date instanceof Date) || isNaN(fm.date.getTime())) {
throw new Error('missing/invalid date (expected YAML date or ISO-string)')
}
if (fm.a !== undefined) {
if (!Array.isArray(fm.a)) {
throw new Error('a must be a list of strings')
}
const coordRe = /^\d+:[0-9a-f]+:[a-z0-9][a-z0-9-]*$/
for (const coord of fm.a) {
if (typeof coord !== 'string' || !coordRe.test(coord)) {
throw new Error(`invalid a-tag: "${coord}" (expected "<kind>:<pubkey-hex>:<d-tag>")`)
}
}
}
}

View File

@ -56,3 +56,33 @@ Deno.test('validatePost: akzeptiert ISO-string-date', () => {
validatePost(fm)
assertEquals(fm.date instanceof Date, true)
})
Deno.test('validatePost: akzeptiert a-tag im korrekten format', () => {
const fm = {
title: 'T',
slug: 'abc',
date: new Date('2024-01-01'),
a: ['30023:abcdef0123456789:other-slug'],
} as Frontmatter
validatePost(fm) // wirft nicht
})
Deno.test('validatePost: lehnt a-tag mit falschem format ab', () => {
const fm = {
title: 'T',
slug: 'abc',
date: new Date('2024-01-01'),
a: ['nur-ein-string'],
} as Frontmatter
assertThrows(() => validatePost(fm), Error, 'invalid a-tag')
})
Deno.test('validatePost: lehnt a-tag mit fehlendem d-tag ab', () => {
const fm = {
title: 'T',
slug: 'abc',
date: new Date('2024-01-01'),
a: ['30023:abcdef:'],
} as Frontmatter
assertThrows(() => validatePost(fm), Error, 'invalid a-tag')
})