Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-dots-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@scaleway/regex': minor
---

Add `pathSegment` and `absolutePath` regex
28 changes: 28 additions & 0 deletions packages/regex/src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from '@jest/globals'
import {
absoluteLinuxPath,
absolutePath,
accessKeyRegex,
advancedDomainName,
alpha,
Expand Down Expand Up @@ -36,6 +37,7 @@ import {
ipv6,
ipv6Cidr,
macAddress,
pathSegment,
phone,
reverseDNS,
s3BucketName,
Expand Down Expand Up @@ -958,4 +960,30 @@ describe('@regex', () => {
expect(accessKeyRegex.test(string)).toBe(expected)
})
})

describe.only('pathSegment', () => {
test.each([
['/hello', false],
['hello', true],
['hello nop', false],
['hello?', false],
['hello-world', true],
['hello/world', false],
])('should match regex %s to be %s', (string, expected) => {
expect(pathSegment.test(string)).toBe(expected)
})
})

describe.only('absolutePath', () => {
test.each([
['/hello', true],
['hello', false],
['/hello nop', false],
['/hello?', false],
['/hello-world', true],
['/hello/world', true],
])('should match regex %s to be %s', (string, expected) => {
expect(absolutePath.test(string)).toBe(expected)
})
})
})
4 changes: 4 additions & 0 deletions packages/regex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ export const ipv6Cidr = new RegExp(`^${cidrv6}$`)
export const reverseDNS = /^[a-z0-9-]+(\.[a-z0-9-]{1,63})+(\.)$/
export const dashedIpv4 =
/(\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}/

export const pathSegment = /^[_a-zA-Z0-9][-_.a-zA-Z0-9]*[_a-zA-Z0-9]$/
export const absolutePath =
/^\/([_a-zA-Z0-9][-_.a-zA-Z0-9]*[_a-zA-Z0-9]\/?){1,10}$/