|
| 1 | +import { t } from 'elysia' |
| 2 | +import { type TAnySchema } from '@sinclair/typebox' |
| 3 | +import { createAccelerator } from '../src' |
| 4 | + |
| 5 | +import { describe, expect, it } from 'bun:test' |
| 6 | + |
| 7 | +const isEqual = (shape: TAnySchema, value: unknown, expected = value) => |
| 8 | + expect(JSON.parse(createAccelerator(shape)(value))).toEqual(expected) |
| 9 | + |
| 10 | +describe('Array', () => { |
| 11 | + it('handle string array at root', () => { |
| 12 | + const shape = t.Array(t.String()) |
| 13 | + |
| 14 | + isEqual(shape, ['a', 'b']) |
| 15 | + }) |
| 16 | + |
| 17 | + it('handle number array at root', () => { |
| 18 | + const shape = t.Array(t.Number()) |
| 19 | + |
| 20 | + isEqual(shape, [1, 2]) |
| 21 | + }) |
| 22 | + |
| 23 | + it('handle boolean array at root', () => { |
| 24 | + const shape = t.Array(t.Number()) |
| 25 | + |
| 26 | + isEqual(shape, [true, false]) |
| 27 | + }) |
| 28 | + |
| 29 | + it('handle big int array at root', () => { |
| 30 | + const shape = t.Array(t.Number()) |
| 31 | + |
| 32 | + isEqual(shape, [1n, 2n], [1, 2]) |
| 33 | + }) |
| 34 | + |
| 35 | + it('handle array union at root', () => { |
| 36 | + const shape = t.Array(t.Union([t.String(), t.Number()])) |
| 37 | + |
| 38 | + isEqual(shape, ['a', 'b', 1, 2, 'c']) |
| 39 | + }) |
| 40 | + |
| 41 | + it('handle array object', () => { |
| 42 | + const shape = t.Array( |
| 43 | + t.Object({ |
| 44 | + a: t.String(), |
| 45 | + b: t.String() |
| 46 | + }) |
| 47 | + ) |
| 48 | + |
| 49 | + isEqual( |
| 50 | + shape, |
| 51 | + [ |
| 52 | + { |
| 53 | + a: 'a', |
| 54 | + b: 'b' |
| 55 | + }, |
| 56 | + { |
| 57 | + a: 'a', |
| 58 | + b: 'b', |
| 59 | + c: 'c' |
| 60 | + } |
| 61 | + ], |
| 62 | + [ |
| 63 | + { |
| 64 | + a: 'a', |
| 65 | + b: 'b' |
| 66 | + }, |
| 67 | + { |
| 68 | + a: 'a', |
| 69 | + b: 'b' |
| 70 | + } |
| 71 | + ] |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it('handle array object with optional', () => { |
| 76 | + const shape = t.Array( |
| 77 | + t.Object({ |
| 78 | + a: t.String(), |
| 79 | + b: t.Optional(t.String()) |
| 80 | + }) |
| 81 | + ) |
| 82 | + |
| 83 | + isEqual( |
| 84 | + shape, |
| 85 | + [ |
| 86 | + { |
| 87 | + a: 'a' |
| 88 | + }, |
| 89 | + { |
| 90 | + a: 'a', |
| 91 | + b: 'b' |
| 92 | + }, |
| 93 | + { |
| 94 | + a: 'a', |
| 95 | + b: 'b', |
| 96 | + c: 'c' |
| 97 | + } |
| 98 | + ], |
| 99 | + [ |
| 100 | + { a: 'a' }, |
| 101 | + { |
| 102 | + a: 'a', |
| 103 | + b: 'b' |
| 104 | + }, |
| 105 | + { |
| 106 | + a: 'a', |
| 107 | + b: 'b' |
| 108 | + } |
| 109 | + ] |
| 110 | + ) |
| 111 | + }) |
| 112 | +}) |
0 commit comments