feat(publish): validatePost prüft a-tag-format
This commit is contained in:
parent
4986eae592
commit
1b0872a93f
|
|
@ -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>")`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue