Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 4c65213

Browse files
committed
fix(data): Add tests + pr review fixes
1 parent 1b78eb5 commit 4c65213

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/validation/joi/joi.shared.schemas.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
binarySchema,
77
dateIntervalStringSchema,
88
dateObjectSchema,
9+
dateTimeStringSchema,
910
emailSchema,
1011
ianaTimezoneSchema,
1112
idSchema,
@@ -218,3 +219,35 @@ describe('uuidSchema', () => {
218219
expect(() => validate('123g4567-e89b-12d3-a456-426614174000', uuidSchema)).toThrow()
219220
})
220221
})
222+
223+
describe('dateTimeStringSchema', () => {
224+
const validDateTimes = [
225+
'2024-09-30T00:55',
226+
'2024-09-30T00:55+02:00',
227+
'2024-09-30T00:55Z',
228+
'2024-09-30T00:55:12',
229+
'2024-09-30T00:55:12+02:00',
230+
'2024-09-30T00:55:12Z',
231+
]
232+
233+
test.each(validDateTimes)('valid dateTime: %s', s => {
234+
expect(validate(s, dateTimeStringSchema)).toBe(s)
235+
})
236+
237+
const invalidDateTimes = [
238+
undefined, // Non-string
239+
null, // Non-string
240+
'', // Empty string
241+
'2024-07-25', // Missing time part
242+
'T00:55:12Z', // Missing date
243+
'2024-07-25T0055Z', // Missing colon in time
244+
'2024-07-25 00:55', // Missing "T" between date and time
245+
'2024/07/25T00:55', // Invalid date separator ("/" instead of "-")
246+
'2024-07-25T00-55', // Invalid time separator ("-" instead of ":")
247+
'2024-07-25T00:55Zextra', // Extra characters after a valid datetime
248+
]
249+
250+
test.each(invalidDateTimes)('invalid dateTime: %s', s => {
251+
expect(() => validate(s, dateTimeStringSchema)).toThrow()
252+
})
253+
})

src/validation/joi/joi.shared.schemas.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,7 @@ export const macAddressSchema = stringSchema.regex(/^([0-9A-Fa-f]{2}[:-]){5}([0-
175175

176176
export const uuidSchema = stringSchema.uuid()
177177

178-
export const DATE_TIME_STRING_REGEX = /d{4}-\d{2}-\d{2}T\d{2}:\d{2}([+-]\d{2}:\d{2})?/
178+
export const DATE_TIME_STRING_REGEX =
179+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:\d{2})?$/
180+
179181
export const dateTimeStringSchema = stringSchema.regex(DATE_TIME_STRING_REGEX)

0 commit comments

Comments
 (0)