Skip to content

Commit

Permalink
Merge pull request #23 from NaturalCycles/DEV-19343-nc-backend-3-endp…
Browse files Browse the repository at this point in the history
…oint-for-samsung-night-input-data

feat(data): Add dateTimeStringSchema
  • Loading branch information
Depyden authored Oct 2, 2024
2 parents c4e8f4c + 61f9b31 commit 46f7806
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/validation/joi/joi.shared.schemas.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expectTypeOf } from '@naturalcycles/dev-lib/dist/testing'
import { BaseDBEntity } from '@naturalcycles/js-lib'
import { BaseDBEntity, localTime } from '@naturalcycles/js-lib'
import { testValidation } from '../../test/validation.test.util'
import {
baseDBEntitySchema,
binarySchema,
dateIntervalStringSchema,
dateObjectSchema,
dateTimeStringSchema,
emailSchema,
ianaTimezoneSchema,
idSchema,
Expand Down Expand Up @@ -218,3 +219,40 @@ describe('uuidSchema', () => {
expect(() => validate('123g4567-e89b-12d3-a456-426614174000', uuidSchema)).toThrow()
})
})

describe('dateTimeStringSchema', () => {
const validDateTimes = [
'2024-07-25',
'2024-09-30T00:55:12',
'2024-09-30T00:55:12+02:00',
'2024-09-30T00:55:12Z',
]

test.each(validDateTimes)('valid dateTime: %s', s => {
expect(isValid(s, dateTimeStringSchema)).toBe(true)
expect(localTime.isValid(s)).toBe(true)
})

const invalidDateTimes = [
undefined, // Non-string
null, // Non-string
'', // Empty string
'T00:55:12Z', // Missing date
'2024-09-30T00:55', // Missing timezone
'2024-09-30T00:55Z', // Missing seconds
'2024-09-30T00:55+02:00', // Missing seconds
'2024-07-25T0055Z', // Missing colon in time
'2024-07-25 00:55', // Missing "T" between date and time
'2024/07/25T00:55', // Invalid date separator ("/" instead of "-")
'2024-07-25T00-55', // Invalid time separator ("-" instead of ":")
'2024-07-25T00:55Zextra', // Extra characters after a valid datetime
'extra2024-07-25T00:55Z', // Extra characters before a valid datetime
'Some random string', // Random string
'2024 was a good year', // Year with some text
]

test.each(invalidDateTimes)('invalid dateTime: %s', s => {
expect(isValid(s, dateTimeStringSchema)).toBe(false)
expect(localTime.isValid(s)).toBe(false)
})
})
7 changes: 7 additions & 0 deletions src/validation/joi/joi.shared.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export const dateIntervalStringSchema = stringSchema.regex(DATE_INTERVAL_REGEX).
'string.pattern.base': `must be a DateInterval string`,
})

export const DATE_TIME_STRING_REGEX =
/^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})?)?$/

export const dateTimeStringSchema = stringSchema.regex(DATE_TIME_STRING_REGEX).messages({
'string.pattern.base': `must be a DateTime string`,
})

/**
* Allows all values of a String Enum.
*/
Expand Down

0 comments on commit 46f7806

Please sign in to comment.