refactor: extract escapeRegex and validateTagSlugs server utils

Deduplicate tag validation and regex escaping into shared auto-imported
utils. Add tag validation to wiki patch/batch-tag routes. Remove
duplicate tags field from event schema.
This commit is contained in:
Jennie Robinson Faber 2026-04-09 23:51:56 +01:00
parent f585fabf21
commit a516f172fb
11 changed files with 33 additions and 31 deletions

View file

@ -0,0 +1,3 @@
export function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

View file

@ -0,0 +1,14 @@
import Tag from '../models/tag.js'
export async function validateTagSlugs(slugs) {
if (!slugs?.length) return
const foundTags = await Tag.find({ slug: { $in: slugs } })
const foundSlugs = new Set(foundTags.map(t => t.slug))
const invalid = slugs.filter(s => !foundSlugs.has(s))
if (invalid.length > 0) {
throw createError({
statusCode: 400,
statusMessage: `Unknown tag slugs: ${invalid.join(', ')}`
})
}
}