From c8d67ff81edd0f49eb418147747c64d67ca8c0bc Mon Sep 17 00:00:00 2001 From: Kravets <57632712+kravetsone@users.noreply.github.com> Date: Sun, 1 Sep 2024 19:01:06 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8chore:=20add=20JSON=20schema=20tests?= =?UTF-8?q?=20and=20some=20improves?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/type-system.ts | 8 +++++--- test/type-system/union-enum.test.ts | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/type-system.ts b/src/type-system.ts index 268d968f..a319daa3 100644 --- a/src/type-system.ts +++ b/src/type-system.ts @@ -316,9 +316,11 @@ TypeRegistry.Set('UnionEnum', (schema, value) => { type NonEmptyArray = [T, ...T[]] -export type TEnumValue = number | string | null; +export type TEnumValue = number | string | null -export interface TUnionEnum = [TEnumValue]> extends TSchema { +export interface TUnionEnum = [TEnumValue]> + extends TSchema { + type?: 'number' | 'string' | 'null' [Kind]: 'UnionEnum' static: T[number] enum: T @@ -599,7 +601,7 @@ export const ElysiaType = { ? { type: 'string' } : values.every((value) => typeof value === 'number') ? { type: 'number' } - : values.every((value) => value === 'null') + : values.every((value) => value === null) ? { type: 'null' } : {} diff --git a/test/type-system/union-enum.test.ts b/test/type-system/union-enum.test.ts index 1f61c5f9..fb210cd5 100644 --- a/test/type-system/union-enum.test.ts +++ b/test/type-system/union-enum.test.ts @@ -21,13 +21,27 @@ describe('TypeSystem - UnionEnum', () => { expect(Value.Check(schema, {})).toBe(false) expect(Value.Check(schema, undefined)).toBe(false) }) + it('JSON schema', () => { + expect(t.UnionEnum(['some', 'data'])).toMatchObject({ + type: 'string', + enum: ['some', 'data'] + }) + expect(t.UnionEnum(['some', 1]).type).toBeUndefined() + expect(t.UnionEnum([2, 1])).toMatchObject({ + type: 'number', + enum: [2, 1] + }) + expect(t.UnionEnum([null])).toMatchObject({ + type: 'null', + enum: [null] + }) + }) it('Integrate', async () => { const app = new Elysia().post('/', ({ body }) => body, { body: t.Object({ value: t.UnionEnum(['some', 1, null]) }) }) - const res1 = await app.handle(post('/', { value: 1 })) expect(res1.status).toBe(200)