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

Commit e8fe43a

Browse files
feat: dateIntervalStringSchema
1 parent 6a78ffc commit e8fe43a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
numberSchemaTyped,
2020
numberEnumValueSchema,
2121
numberEnumKeySchema,
22+
dateIntervalStringSchema,
2223
} from './joi.shared.schemas'
2324
import { isValid, validate } from './joi.validation.util'
2425

@@ -141,3 +142,24 @@ test('enum schemas', () => {
141142
const appIdKey = validate(AppId[AppId.APP1], numberEnumKeySchema(AppId))
142143
expectTypeOf(appIdKey).toEqualTypeOf<string>()
143144
})
145+
146+
test('dateIntervalSchema', () => {
147+
const schema = dateIntervalStringSchema
148+
149+
validate('2022-01-01/2022-01-02', schema)
150+
expect(() => validate(undefined, schema)).toThrowErrorMatchingInlineSnapshot(
151+
`""value" is required"`,
152+
)
153+
expect(() => validate('2022-01-01', schema)).toThrowErrorMatchingInlineSnapshot(
154+
`"must be a DateInterval string"`,
155+
)
156+
expect(() => validate('2022-01-01/2022-01-0', schema)).toThrowErrorMatchingInlineSnapshot(
157+
`"must be a DateInterval string"`,
158+
)
159+
expect(() => validate('2022-01-01/2022-01-02/', schema)).toThrowErrorMatchingInlineSnapshot(
160+
`"must be a DateInterval string"`,
161+
)
162+
expect(() =>
163+
validate('2022-01-01/2022-01-02/2022-01-03', schema),
164+
).toThrowErrorMatchingInlineSnapshot(`"must be a DateInterval string"`)
165+
})

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export const dateStringSchema = stringSchema.dateString()
2424
export const binarySchema = Joi.binary()
2525
export const dateObjectSchema = Joi.object().instance(Date)
2626

27+
const DATE_INTERVAL_REGEX = /^\d{4}-\d{2}-\d{2}\/\d{4}-\d{2}-\d{2}$/
28+
export const dateIntervalStringSchema = stringSchema.regex(DATE_INTERVAL_REGEX).messages({
29+
'string.pattern.base': `must be a DateInterval string`,
30+
})
31+
2732
/**
2833
* Allows all values of a String Enum.
2934
*/

src/validation/joi/string.extensions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export function stringExtensions(joi: typeof Joi): Extension {
5151

5252
// Today allows +-14 hours gap to account for different timezones
5353
if (max === 'today') {
54-
max = localTimeNow().add(14, 'hour').toISODate()
54+
max = localTimeNow().plus(14, 'hour').toISODate()
5555
}
5656
if (min === 'today') {
57-
min = localTimeNow().subtract(14, 'hour').toISODate()
57+
min = localTimeNow().minus(14, 'hour').toISODate()
5858
}
5959
// console.log('min/max', min, max)
6060

0 commit comments

Comments
 (0)