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";
|
||||
import { validateBody } from "../../utils/validateBody.js";
|
||||
|
|
@ -13,18 +12,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 eventData = {
|
||||
...body,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
await connectDB()
|
||||
|
||||
await validateTagSlugs(body.tags)
|
||||
|
||||
const article = await WikiArticle.findByIdAndUpdate(
|
||||
id,
|
||||
{ tags: body.tags },
|
||||
|
|
|
|||
|
|
@ -30,6 +30,8 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
await connectDB()
|
||||
|
||||
await validateTagSlugs([...(body.addTags || []), ...(body.removeTags || [])])
|
||||
|
||||
const filter = body.articleIds
|
||||
? { _id: { $in: body.articleIds } }
|
||||
: { collection: body.collection }
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
|
|||
filter.collection = collection
|
||||
}
|
||||
if (search) {
|
||||
filter.title = { $regex: search, $options: 'i' }
|
||||
filter.title = { $regex: escapeRegex(search), $options: 'i' }
|
||||
}
|
||||
|
||||
const articles = await WikiArticle.find(filter)
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ export default defineEventHandler(async (event) => {
|
|||
}
|
||||
|
||||
if (search) {
|
||||
// Escape regex metacharacters to prevent ReDoS
|
||||
const escaped = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const escaped = escapeRegex(search);
|
||||
andConditions.push({
|
||||
$or: [
|
||||
{ name: { $regex: escaped, $options: "i" } },
|
||||
|
|
|
|||
|
|
@ -183,7 +183,6 @@ const eventSchema = new mongoose.Schema({
|
|||
refundAmount: Number,
|
||||
},
|
||||
],
|
||||
tags: [{ type: String }],
|
||||
createdBy: { type: String, required: true },
|
||||
createdAt: { type: Date, default: Date.now },
|
||||
updatedAt: { type: Date, default: Date.now },
|
||||
|
|
|
|||
3
server/utils/escapeRegex.js
Normal file
3
server/utils/escapeRegex.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function escapeRegex(str) {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
}
|
||||
14
server/utils/validateTagSlugs.js
Normal file
14
server/utils/validateTagSlugs.js
Normal 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(', ')}`
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue