Skip to content

Commit aececf9

Browse files
MathildeJollymjolly
andauthored
feat(regex): add new kafka username regex (#2782)
Co-authored-by: mjolly <mjolly@scaleway.com>
1 parent 13a0e73 commit aececf9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

.changeset/calm-singers-whisper.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+
feat(regex): add new kafka username regex

packages/regex/src/__tests__/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
ipv4Cidr,
3939
ipv6,
4040
ipv6Cidr,
41+
kafkaUsernameRegex,
4142
macAddress,
4243
nineDigitsCode,
4344
organizationAlias,
@@ -1153,6 +1154,19 @@ describe('@regex', () => {
11531154
})
11541155
})
11551156

1157+
describe('kafkaUsernameRegex', () => {
1158+
test.each([
1159+
['username', true],
1160+
['user-name', true],
1161+
['my-group.user-name', true],
1162+
['-user-name', false],
1163+
['user-Name-', false],
1164+
['user..name', false],
1165+
])('should match regex %s to be %s', (string, expected) => {
1166+
expect(kafkaUsernameRegex.test(string)).toBe(expected)
1167+
})
1168+
})
1169+
11561170
describe('uuid', () => {
11571171
test.each([
11581172
[asciiLetters, false],

packages/regex/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ export const sgPortRange =
9898

9999
// Include all characters except backtick ` and @ as first character
100100
export const password = /^(?!@)[^`]*$/
101+
102+
// A kafka username contains lowercase letters and numbers, with each segment starting and ending with a letter or number. Hyphens are only allowed in the middle of segments. Example: "username", "user-name", "my-group.user-name"
103+
export const kafkaUsernameRegex =
104+
/^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/

0 commit comments

Comments
 (0)