joas8211 / payload-tenancy

Multi-tenancy plugin for Payload CMS
MIT License
122 stars 6 forks source link

Possible bug: Slug field invalid format #6

Closed TatisLois closed 1 year ago

TatisLois commented 1 year ago

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?

TatisLois commented 1 year ago

I think we could add the beforeValidate to this file -> https://github.com/joas8211/payload-tenancy/blob/f687c556ab5518be52a3e732590136237943ed4d/src/fields/tenantSlug.ts

joas8211 commented 1 year ago

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?

TatisLois commented 1 year ago

@joas8211 that would be perfect, currently I was trying to modify it in a beforeOperation or beforeRead hook on the collection but if you extended the config then that would make it much more powerful!

joas8211 commented 1 year ago

Following issues are split from this issue: #11, #8. Closing this issue in favor or those split issues.