-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉: feat: add
UnionEnum
with JSON schema enum usage (#512)
- Loading branch information
1 parent
bfdd80a
commit d832742
Showing
2 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Elysia, { t } from '../../src' | ||
import { describe, expect, it } from 'bun:test' | ||
import { Value } from '@sinclair/typebox/value' | ||
import { post } from '../utils' | ||
|
||
describe('TypeSystem - UnionEnum', () => { | ||
it('Create', () => { | ||
expect(Value.Create(t.UnionEnum(['some', 'data']))).toEqual('some') | ||
}) | ||
|
||
it('Check', () => { | ||
const schema = t.UnionEnum(['some', 'data', null]) | ||
|
||
expect(Value.Check(schema, 'some')).toBe(true) | ||
expect(Value.Check(schema, 'data')).toBe(true) | ||
expect(Value.Check(schema, null)).toBe(true) | ||
|
||
expect(Value.Check(schema, { deep: 2 })).toBe(false) | ||
expect(Value.Check(schema, 'yay')).toBe(false) | ||
expect(Value.Check(schema, 42)).toBe(false) | ||
expect(Value.Check(schema, {})).toBe(false) | ||
expect(Value.Check(schema, undefined)).toBe(false) | ||
}) | ||
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) | ||
|
||
const res2 = await app.handle(post('/', { value: null })) | ||
expect(res2.status).toBe(200) | ||
|
||
const res3 = await app.handle(post('/', { value: 'some' })) | ||
expect(res3.status).toBe(200) | ||
|
||
const res4 = await app.handle(post('/', { value: 'data' })) | ||
expect(res4.status).toBe(422) | ||
}) | ||
}) |