Skip to content

Commit

Permalink
fix: stringSchema.dateString output type
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Nov 9, 2024
1 parent 5990a9a commit a60c1e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/validation/joi/joi.shared.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
_stringEnumKeys,
_stringEnumValues,
BaseDBEntity,
IsoDate,
IsoDateTime,
NumberEnum,
StringEnum,
Expand All @@ -24,7 +23,7 @@ export const numberSchema = Joi.number()
export const numberSchemaTyped = <T>(): NumberSchema<T> => Joi.number<T>()
export const integerSchema = Joi.number().integer()
export const percentageSchema = Joi.number().integer().min(0).max(100)
export const dateStringSchema = stringSchema.dateString() as StringSchema<IsoDate>
export const dateStringSchema = stringSchema.dateString()
export const binarySchema = Joi.binary()
export const dateObjectSchema = Joi.object().instance(Date)

Expand Down
8 changes: 8 additions & 0 deletions src/validation/joi/string.extensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { expectTypeOf } from '@naturalcycles/dev-lib/dist/testing'
import { IsoDate, localTime } from '@naturalcycles/js-lib'
import { testValidation } from '../../test/validation.test.util'
import { stringSchema } from './joi.shared.schemas'
import { validate } from './joi.validation.util'
import { StringSchema } from './string.extensions'

test('dateString', () => {
const schema = stringSchema.dateString()
Expand All @@ -15,6 +18,11 @@ test('dateString', () => {
test('dateString min/max', async () => {
const schema = stringSchema.dateString('2017-06-21' as IsoDate, '2017-06-23' as IsoDate)

expectTypeOf(schema).toEqualTypeOf<StringSchema<IsoDate>>()

const value = validate('2017-06-22' as IsoDate, schema)
expectTypeOf(value).toEqualTypeOf<IsoDate>()

testValidation(
schema,
['2017-06-21', '2017-06-22', '2017-06-23'],
Expand Down
2 changes: 1 addition & 1 deletion src/validation/joi/string.extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type Joi from 'joi'
import { Extension, StringSchema as JoiStringSchema } from 'joi'

export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
dateString: (min?: IsoDate | 'today', max?: IsoDate | 'today') => this
dateString: (min?: IsoDate | 'today', max?: IsoDate | 'today') => StringSchema<IsoDate>
}

export interface JoiDateStringOptions {
Expand Down

0 comments on commit a60c1e2

Please sign in to comment.