Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/type-system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import type {
JavaScriptTypeBuilder,
StringOptions,
TUnsafe,
Uint8ArrayOptions
Uint8ArrayOptions,
TEnum
} from '@sinclair/typebox'

import {
Expand All @@ -40,7 +41,8 @@ import {
TForm,
TUnionEnum,
ElysiaTransformDecodeBuilder,
TArrayBuffer
TArrayBuffer,
AssertNumericEnum
} from './types'

import { ELYSIA_FORM_DATA, form } from '../utils'
Expand Down Expand Up @@ -151,6 +153,23 @@ export const ElysiaType = {
.Encode((value) => value) as any as TNumber
},

NumericEnum<T extends AssertNumericEnum<T>>(item: T, property?: SchemaOptions) {
const schema = Type.Enum(item, property)
const compiler = compile(schema)

return t
.Transform(
t.Union([t.String({ format: 'numeric' }), t.Number()], property)
)
.Decode((value) => {
const number = +value
if (isNaN(number)) throw compiler.Error(number)
if (!compiler.Check(number)) throw compiler.Error(number)
return number
})
.Encode((value) => value) as any as TEnum<T>
},

Integer: (property?: IntegerOptions): TInteger => {
const schema = Type.Integer(property)
const compiler = compile(schema)
Expand Down Expand Up @@ -607,6 +626,7 @@ t.ObjectString = ElysiaType.ObjectString
t.ArrayString = ElysiaType.ArrayString
t.ArrayQuery = ElysiaType.ArrayQuery
t.Numeric = ElysiaType.Numeric
t.NumericEnum = ElysiaType.NumericEnum
t.Integer = ElysiaType.Integer

t.File = (arg) => {
Expand Down
10 changes: 10 additions & 0 deletions src/type-system/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,13 @@ export interface TTransform<
[TransformKind]: TransformOptions<I, O>
[key: string]: any
}

export type AssertNumericEnum<T extends Record<string, string | number>> = {
[K in keyof T]: K extends number
? string
: K extends `${number}`
? string
: K extends string
? number
: never
}
9 changes: 9 additions & 0 deletions test/aot/has-transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ describe('Has Transform', () => {
expect(hasTransform(schema)).toBe(true)
})

it('Found t.NumericEnum', () => {
const schema = t.Object({
gender: t.NumericEnum({ UNKNOWN: 0, MALE: 1, FEMALE: 2 }),
liyue: t.String()
})

expect(hasTransform(schema)).toBe(true)
})

it('Found t.ObjectString', () => {
const schema = t.Object({
id: t.String(),
Expand Down
24 changes: 24 additions & 0 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ describe('Query Validator', () => {
expect(res.status).toBe(200)
})

it('parse numeric enum', async () => {
enum Gender {
MALE = 1,
FEMALE = 2,
UNKNOWN = 3
}

const app = new Elysia().get('/', ({ query }) => query, {
query: t.Object({
name: t.String(),
gender: t.NumericEnum(Gender)
})
})
const res = await app.handle(
req(`/?name=sucrose&gender=${Gender.MALE}`)
)

expect(await res.json()).toEqual({
name: 'sucrose',
gender: Gender.MALE
})
expect(res.status).toBe(200)
})

it('parse single integer', async () => {
const app = new Elysia().get('/', ({ query: { limit } }) => limit, {
query: t.Object({
Expand Down