|
6 | 6 | binarySchema, |
7 | 7 | dateIntervalStringSchema, |
8 | 8 | dateObjectSchema, |
| 9 | + dateTimeStringSchema, |
9 | 10 | emailSchema, |
10 | 11 | ianaTimezoneSchema, |
11 | 12 | idSchema, |
@@ -218,3 +219,35 @@ describe('uuidSchema', () => { |
218 | 219 | expect(() => validate('123g4567-e89b-12d3-a456-426614174000', uuidSchema)).toThrow() |
219 | 220 | }) |
220 | 221 | }) |
| 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 | +}) |
0 commit comments