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

Commit 53de988

Browse files
feat: ianaTimezoneSchema
1 parent ea2d9ac commit 53de988

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
numberEnumValueSchema,
2121
numberEnumKeySchema,
2222
dateIntervalStringSchema,
23+
ianaTimezoneSchema,
2324
} from './joi.shared.schemas'
2425
import { isValid, validate } from './joi.validation.util'
2526

@@ -163,3 +164,15 @@ test('dateIntervalSchema', () => {
163164
validate('2022-01-01/2022-01-02/2022-01-03', schema),
164165
).toThrowErrorMatchingInlineSnapshot(`"must be a DateInterval string"`)
165166
})
167+
168+
test('ianaTimezoneSchema', () => {
169+
const schema = ianaTimezoneSchema
170+
171+
validate('Europe/London', schema)
172+
expect(() => validate(undefined, schema)).toThrowErrorMatchingInlineSnapshot(
173+
`""value" is required"`,
174+
)
175+
expect(() => validate('London', schema)).toThrowErrorMatchingInlineSnapshot(
176+
`"must be a valid IANA timezone string"`,
177+
)
178+
})

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ export const utcOffsetSchema = numberSchema
156156
.max(14 * 60)
157157
.dividable(15)
158158

159+
const supportedTimezones = new Set(Intl.supportedValuesOf('timeZone'))
160+
161+
export const ianaTimezoneSchema = stringSchema.valid(...supportedTimezones).messages({
162+
'any.only': `must be a valid IANA timezone string`,
163+
})
164+
159165
export const ipAddressSchema = stringSchema.ip()
160166

161167
export const baseDBEntitySchema: ObjectSchema<BaseDBEntity> = objectSchema<BaseDBEntity>({

0 commit comments

Comments
 (0)