diff --git a/.changeset/light-dots-tell.md b/.changeset/light-dots-tell.md new file mode 100644 index 000000000..3512b7182 --- /dev/null +++ b/.changeset/light-dots-tell.md @@ -0,0 +1,5 @@ +--- +'@scaleway/regex': minor +--- + +Add `pathSegment` and `absolutePath` regex diff --git a/packages/regex/src/__tests__/index.ts b/packages/regex/src/__tests__/index.ts index 2814f3e98..54908e5c3 100644 --- a/packages/regex/src/__tests__/index.ts +++ b/packages/regex/src/__tests__/index.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from '@jest/globals' import { absoluteLinuxPath, + absolutePath, accessKeyRegex, advancedDomainName, alpha, @@ -36,6 +37,7 @@ import { ipv6, ipv6Cidr, macAddress, + pathSegment, phone, reverseDNS, s3BucketName, @@ -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) + }) + }) }) diff --git a/packages/regex/src/index.ts b/packages/regex/src/index.ts index 047b40588..d2356ac3e 100644 --- a/packages/regex/src/index.ts +++ b/packages/regex/src/index.ts @@ -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}$/