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:
parent
f585fabf21
commit
a516f172fb
11 changed files with 33 additions and 31 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import Event from '../../../models/event.js'
|
||||
import Tag from '../../../models/tag.js'
|
||||
import { connectDB } from '../../../utils/mongoose.js'
|
||||
import { requireAdmin } from '../../../utils/auth.js'
|
||||
|
||||
|
|
@ -12,18 +11,7 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
await connectDB()
|
||||
|
||||
// Validate tag slugs against Tag collection
|
||||
if (body.tags && body.tags.length > 0) {
|
||||
const foundTags = await Tag.find({ slug: { $in: body.tags } })
|
||||
const foundSlugs = new Set(foundTags.map(t => t.slug))
|
||||
const invalid = body.tags.filter(s => !foundSlugs.has(s))
|
||||
if (invalid.length > 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: `Unknown tag slugs: ${invalid.join(', ')}`
|
||||
})
|
||||
}
|
||||
}
|
||||
await validateTagSlugs(body.tags)
|
||||
|
||||
const updateData = {
|
||||
...body,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue