Skip to content

Commit 7f4a667

Browse files
author
Tom Lienard
authored
feat(regex): add pathSegment and absolutePath regex (#1493)
1 parent f3b92e7 commit 7f4a667

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

.changeset/light-dots-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scaleway/regex': minor
3+
---
4+
5+
Add `pathSegment` and `absolutePath` regex

packages/regex/src/__tests__/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from '@jest/globals'
22
import {
33
absoluteLinuxPath,
4+
absolutePath,
45
accessKeyRegex,
56
advancedDomainName,
67
alpha,
@@ -36,6 +37,7 @@ import {
3637
ipv6,
3738
ipv6Cidr,
3839
macAddress,
40+
pathSegment,
3941
phone,
4042
reverseDNS,
4143
s3BucketName,
@@ -958,4 +960,30 @@ describe('@regex', () => {
958960
expect(accessKeyRegex.test(string)).toBe(expected)
959961
})
960962
})
963+
964+
describe.only('pathSegment', () => {
965+
test.each([
966+
['/hello', false],
967+
['hello', true],
968+
['hello nop', false],
969+
['hello?', false],
970+
['hello-world', true],
971+
['hello/world', false],
972+
])('should match regex %s to be %s', (string, expected) => {
973+
expect(pathSegment.test(string)).toBe(expected)
974+
})
975+
})
976+
977+
describe.only('absolutePath', () => {
978+
test.each([
979+
['/hello', true],
980+
['hello', false],
981+
['/hello nop', false],
982+
['/hello?', false],
983+
['/hello-world', true],
984+
['/hello/world', true],
985+
])('should match regex %s to be %s', (string, expected) => {
986+
expect(absolutePath.test(string)).toBe(expected)
987+
})
988+
})
961989
})

packages/regex/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ export const ipv6Cidr = new RegExp(`^${cidrv6}$`)
8181
export const reverseDNS = /^[a-z0-9-]+(\.[a-z0-9-]{1,63})+(\.)$/
8282
export const dashedIpv4 =
8383
/(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(-(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/
84+
85+
export const pathSegment = /^[_a-zA-Z0-9][-_.a-zA-Z0-9]*[_a-zA-Z0-9]$/
86+
export const absolutePath =
87+
/^\/([_a-zA-Z0-9][-_.a-zA-Z0-9]*[_a-zA-Z0-9]\/?){1,10}$/

0 commit comments

Comments
 (0)