validatorjs / validator.js

String validation
MIT License
23.12k stars 2.31k forks source link

Slug regex allows special characters #1497

Open cdekkers opened 4 years ago

cdekkers commented 4 years ago

https://github.com/validatorjs/validator.js/blob/bab507ad4a33156ece15582268b2fe0ab3bbca1d/src/lib/isSlug.js#L3

This regex does not invalidate slugs that start with a special character. For example, €test is allowed as a slug. Perhaps this one is a better solution:

/^[a-z0-9]+(?:(?:-|_)+[a-z0-9]+)*$/

More info and tests: https://regexr.com/5eqe6

profnandaa commented 4 years ago

PR welcome.

cdekkers commented 4 years ago

On a related note, the current expression does not allow for slugs with 1 to 2 characters. The proposed one does.

AubreyHewes commented 3 years ago

That this library even wants to validate a slug; it is overreaching it's initial domain; validation of known quantities.

A slug can be implemented differently per cms/site and one may not match another. The case in hand €test is a valid slug, and should always be a valid slug. It may not be a compatible with your cms slug; but it is valid.

@cdekkers Create a separate issue for related note problem.