-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
suggestion: Slug generation does not handle special characters or duplicate names.
The current logic does not sanitize special characters or ensure slug uniqueness, which may result in invalid or duplicate slugs. Please use a more comprehensive slugification method and add a uniqueness check.
Suggested implementation:
slug: await generateUniqueSlug(name || 'Untitled', org.id), // Helper function to slugify and ensure uniqueness
async function generateUniqueSlug(name: string, orgId: string): Promise<string> {
// Basic slugification: lowercase, remove special chars, replace spaces/hyphens, trim
let baseSlug = name
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '') // remove special chars
.replace(/\s+/g, '-') // replace spaces with hyphens
.replace(/-+/g, '-') // collapse multiple hyphens
|| 'untitled';
let slug = baseSlug;
let suffix = 1;
// Check for uniqueness within the org
while (
await prisma.template.findFirst({
where: { slug, orgId }
})
) {
slug = `${baseSlug}-${suffix++}`;
}
return slug;
}
const newTemplate = await prisma.template.create({
data: {
name: name || 'Untitled',
orgId: org.id,
slug: await generateUniqueSlug(name || 'Untitled', org.id),
status: 'draft',
versions: {
create: {
design_json: design,
number: 1,
createdById: user.id,
status: 'DRAFT',
},
},
},Originally posted by @sourcery-ai[bot] in #48 (comment)
Metadata
Metadata
Assignees
Labels
No labels