Skip to content

Commit

Permalink
fix(joi): enum types as singular
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jul 28, 2023
1 parent b30b1e7 commit 0b51dda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/validation/joi/joi.shared.schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
stringSchemaTyped,
stringSchema,
urlSchema,
stringEnumValuesSchema,
stringEnumKeysSchema,
stringEnumValueSchema,
stringEnumKeySchema,
numberSchemaTyped,
numberEnumValuesSchema,
numberEnumKeysSchema,
numberEnumValueSchema,
numberEnumKeySchema,
} from './joi.shared.schemas'
import { isValid, validate } from './joi.validation.util'

Expand Down Expand Up @@ -123,18 +123,18 @@ test('enum schemas', () => {
let os = validate(OS.IOS, stringSchemaTyped<OS>())
expectTypeOf(os).toEqualTypeOf<OS>()

os = validate(OS.IOS, stringEnumValuesSchema(OS))
os = validate(OS.IOS, stringEnumValueSchema(OS))
expectTypeOf(os).toEqualTypeOf<OS>()

const osKey = validate(OS.IOS, stringEnumKeysSchema(OS))
const osKey = validate(OS.IOS, stringEnumKeySchema(OS))
expectTypeOf(osKey).toEqualTypeOf<string>()

let appId = validate(AppId.APP1, numberSchemaTyped<AppId>())
expectTypeOf(appId).toEqualTypeOf<AppId>()

appId = validate(AppId.APP1, numberEnumValuesSchema(AppId))
appId = validate(AppId.APP1, numberEnumValueSchema(AppId))
expectTypeOf(appId).toEqualTypeOf<AppId>()

const appIdKey = validate(AppId[AppId.APP1], numberEnumKeysSchema(AppId))
const appIdKey = validate(AppId[AppId.APP1], numberEnumKeySchema(AppId))
expectTypeOf(appIdKey).toEqualTypeOf<string>()
})
8 changes: 4 additions & 4 deletions src/validation/joi/joi.shared.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ export const dateObjectSchema = Joi.object().instance(Date)
/**
* Allows all values of a String Enum.
*/
export const stringEnumValuesSchema = <ENUM extends StringEnum>(
export const stringEnumValueSchema = <ENUM extends StringEnum>(
en: ENUM,
): StringSchema<ENUM[keyof ENUM]> => Joi.string<ENUM[keyof ENUM]>().allow(..._stringEnumValues(en))

/**
* Allows all keys of a String Enum.
*/
export const stringEnumKeysSchema = <ENUM extends StringEnum>(en: ENUM): StringSchema =>
export const stringEnumKeySchema = <ENUM extends StringEnum>(en: ENUM): StringSchema =>
Joi.string().allow(..._stringEnumKeys(en))

/**
* Allows all values of a String Enum.
*/
export const numberEnumValuesSchema = <ENUM extends NumberEnum>(
export const numberEnumValueSchema = <ENUM extends NumberEnum>(
en: ENUM,
): NumberSchema<ENUM[keyof ENUM]> => Joi.number<ENUM[keyof ENUM]>().allow(..._numberEnumValues(en))

/**
* Allows all keys of a Number Enum.
*/
export const numberEnumKeysSchema = <ENUM extends NumberEnum>(en: ENUM): StringSchema =>
export const numberEnumKeySchema = <ENUM extends NumberEnum>(en: ENUM): StringSchema =>
Joi.string().allow(..._numberEnumKeys(en))

export const urlSchema = (scheme: string | string[] = 'https'): StringSchema =>
Expand Down

0 comments on commit 0b51dda

Please sign in to comment.