Skip to content

Commit 941db7f

Browse files
authored
fix(isSlug): restrict allowed characters to valid slug charset (#2693)
1 parent 2758f70 commit 941db7f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/lib/isSlug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assertString from './util/assertString';
22

3-
let charsetRegex = /^[^\s-_](?!.*?[-_]{2,})[a-z0-9-\\][^\s]*[^-_\s]$/;
3+
const charsetRegex = /^[a-z0-9](?!.*[-_]{2,})(?:[a-z0-9_-]*[a-z0-9])?$/;
44

55
export default function isSlug(str) {
66
assertString(str);

test/validators.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14211,13 +14211,15 @@ describe('Validators', () => {
1421114211
test({
1421214212
validator: 'isSlug',
1421314213
valid: [
14214+
'f',
14215+
'fo',
1421414216
'foo',
1421514217
'foo-bar',
1421614218
'foo_bar',
1421714219
'foo-bar-foo',
1421814220
'foo-bar_foo',
14219-
'foo-bar_foo*75-b4r-**_foo',
14220-
'foo-bar_foo*75-b4r-**_foo-&&',
14221+
'foo-75-b4r-foo',
14222+
'a1-b2_c3',
1422114223
],
1422214224
invalid: [
1422314225
'not-----------slug',
@@ -14227,6 +14229,12 @@ describe('Validators', () => {
1422714229
'_not-slug',
1422814230
'not-slug_',
1422914231
'not slug',
14232+
'i.am.not.a.slug',
14233+
'slug.is.cool',
14234+
'foo-bar_foo*75-b4r-**_foo',
14235+
'foo-bar_foo*75-b4r-**_foo-&&',
14236+
'Foo-Bar',
14237+
'a:b',
1423014238
],
1423114239
});
1423214240
});

0 commit comments

Comments
 (0)