Skip to content

Commit d5c23e7

Browse files
committed
feat(regex): add new isJson regex
1 parent 6e7f188 commit d5c23e7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

.changeset/green-mangos-shake.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+
regex: add new isJson 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+
isJson,
4142
kafkaUsernameRegex,
4243
macAddress,
4344
nineDigitsCode,
@@ -1154,6 +1155,19 @@ describe('@regex', () => {
11541155
})
11551156
})
11561157

1158+
describe('isJson', () => {
1159+
test.each([
1160+
['{"key": "value"}', true],
1161+
['{"key": {"nested": "value"}}', true],
1162+
['{"key": ["array", "value"]}', true],
1163+
['{"key": "value"', false],
1164+
['{"key": {"nested": "value"', false],
1165+
['{"key": ["array", "value"', false],
1166+
])('should match regex %s to be %s', (string, expected) => {
1167+
expect(isJson.test(string)).toBe(expected)
1168+
})
1169+
})
1170+
11571171
describe('kafkaUsernameRegex', () => {
11581172
test.each([
11591173
['username', true],

packages/regex/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ export const password = /^(?!@)[^`]*$/
102102
// 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"
103103
export const kafkaUsernameRegex =
104104
/^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/
105+
106+
export const isJson = /({(?:[^{}]|{(?:[^{}]|{[^{}]*})*})*})/s

0 commit comments

Comments
 (0)