Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug: Slug field invalid format #6

Closed
TatisLois opened this issue May 3, 2023 · 4 comments
Closed

Possible bug: Slug field invalid format #6

TatisLois opened this issue May 3, 2023 · 4 comments
Assignees

Comments

@TatisLois
Copy link

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
Copy link
Author

@joas8211
Copy link
Owner

joas8211 commented May 4, 2023

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
Copy link
Author

@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 joas8211 self-assigned this May 7, 2023
@joas8211
Copy link
Owner

joas8211 commented May 7, 2023

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

@joas8211 joas8211 closed this as completed May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants