diff --git a/.changeset/many-breads-cover.md b/.changeset/many-breads-cover.md new file mode 100644 index 000000000..a122d8a07 --- /dev/null +++ b/.changeset/many-breads-cover.md @@ -0,0 +1,5 @@ +--- +"@scaleway/regex": minor +--- + +feat(regex): add regex organizationAlias and uuid diff --git a/packages/regex/src/__tests__/index.test.ts b/packages/regex/src/__tests__/index.test.ts index 167f92557..34b5b9721 100644 --- a/packages/regex/src/__tests__/index.test.ts +++ b/packages/regex/src/__tests__/index.test.ts @@ -40,6 +40,7 @@ import { ipv6Cidr, macAddress, nineDigitsCode, + organizationAlias, password, pathSegment, phone, @@ -51,6 +52,7 @@ import { uppercaseBasicDomain, uppercaseBasicSubdomain, url, + uuid, } from '..' const alphanumDashDotsText = 'testwithdashdots-.' @@ -104,6 +106,7 @@ const linuxPaths = { ], GOOD: ['/var', '/var/test', '/var/test_', '/var_/test', '/'], } +const uuidTest = '550e8400-e29b-41d4-a716-446655440000' describe('@regex', () => { describe('alpha', () => { @@ -495,6 +498,24 @@ describe('@regex', () => { }) }) + describe('organizationAlias', () => { + test.each([ + [asciiLetters, false], + [asciiLowercase, true], + [asciiUppercase, false], + [digitsTest, true], + [emailTest, false], + [octdigits, true], + [hexdigits, false], + [printable, false], + [punctuation, false], + [whitespace, false], + [cronTest, false], + ])('should match regex %s to be %s', (string, expected) => { + expect(organizationAlias.test(string)).toBe(expected) + }) + }) + describe('ascii', () => { test.each([ [asciiLetters, true], @@ -1131,4 +1152,24 @@ describe('@regex', () => { expect(password.test(string)).toBe(expected) }) }) + + describe('uuid', () => { + test.each([ + [asciiLetters, false], + [asciiLowercase, false], + [asciiUppercase, false], + [digitsTest, false], + [emailTest, false], + [octdigits, false], + [hexdigits, false], + [printable, false], + [punctuation, false], + [whitespace, false], + [cronTest, false], + [macAddress1, false], + [uuidTest, true], + ])('should match regex %s to be %s', (string, expected) => { + expect(uuid.test(string)).toBe(expected) + }) + }) }) diff --git a/packages/regex/src/index.ts b/packages/regex/src/index.ts index f7ddbeb07..41444bb57 100644 --- a/packages/regex/src/index.ts +++ b/packages/regex/src/index.ts @@ -24,6 +24,7 @@ export const absoluteLinuxPath = /(^\/$|^(\/[a-zA-Z0-9_]+)*$)/ // eslint-disable-next-line no-control-regex export const ascii = /^[\x00-\x7F]+$/ +export const organizationAlias = /^[a-z0-9]{2,32}$/ export const backupKey = /^[A-Z0-9]{8}$|^[A-Z0-9]{32}$/ export const basicDomain = /^[a-z0-9-]+(\.[a-z0-9-]{1,63})+$/ export const uppercaseBasicDomain = @@ -51,6 +52,8 @@ export const url = /^http(s)?:\/\/?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/ export const hexadecimal = /^[0-9a-fA-F]+$/ export const s3BucketName = /^[a-z0-9][-.a-z0-9]{1,61}[a-z0-9]$/ +export const uuid = + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i // Pasted from `ip-regex` package (https://github.com/sindresorhus/ip-regex/blob/main/index.js) const v4 =