Possible bug: Slug field invalid format
TatisLois opened this issue · 4 comments
I noticed that a slug can be saved in a invalid format for example my slug
, ideally this would convert to my-slug
for the user.
In other payload projects i've used this utils in a beforeValidate
hook, what are your thoughts of including it into the project or a similar solution?
function formatSlug({ value }) {
if (!value || typeof value !== "string") {
return "";
}
return value
.trim()
.toLowerCase()
.replace(/[^\w\s-]/g, "") // Remove non-word characters
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/--+/g, "-") // Replace multiple hyphens with a single hyphen
.replace(/^-+|-+$/g, ""); // Remove leading/trailing hyphens
}
Alternatively is it possible to extend your tenancy collection to pass this in myself for the field?
I think we could add the beforeValidate to this file -> https://github.com/joas8211/payload-tenancy/blob/f687c556ab5518be52a3e732590136237943ed4d/src/fields/tenantSlug.ts
I don't know if I consider this a bug. URL can contain almost, if not, any character but it just has to be encoded in the correct way. There actually is a bug in path mapping middleware though. It should decode the slug before trying to find tenant that matches that slug. Also I'm thinking that maybe we make it more easier to modify the fields, so that users of this plugin that want to limit the way slug can be formatted, can do that by adding their own validation or auto formatting.
@TatisLois, how does that sound to you? Would it be enough if I add the slug decoding and allow to modify the fields?