Skip to content
Closed
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/green-mangos-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/regex": minor
---

regex: add new json regex
14 changes: 14 additions & 0 deletions packages/regex/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
ipv4Cidr,
ipv6,
ipv6Cidr,
json,
kafkaUsernameRegex,
macAddress,
nineDigitsCode,
Expand Down Expand Up @@ -1154,6 +1155,19 @@ describe('@regex', () => {
})
})

describe('json', () => {
test.each([
['{"key": "value"}', true],
['{"key": {"nested": "value"}}', true],
['{"key": ["array", "value"]}', true],
['{"key": "value"', false],
['{"key": {"nested": "value"', false],
['{"key": ["array", "value"', false],
])('should match regex %s to be %s', (string, expected) => {
expect(json.test(string)).toBe(expected)
})
})

describe('kafkaUsernameRegex', () => {
test.each([
['username', true],
Expand Down
2 changes: 2 additions & 0 deletions packages/regex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ export const password = /^(?!@)[^`]*$/
// 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"
export const kafkaUsernameRegex =
/^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/

export const json = /({(?:[^{}]|{(?:[^{}]|{[^{}]*})*})*})/s
Loading