|
| 1 | +import { Value } from '@sinclair/typebox/value' |
| 2 | +import { TypeRegistry, Type, Kind, TSchema } from '@sinclair/typebox' |
| 3 | +import { Assert } from '../../assert' |
| 4 | + |
| 5 | +describe('value/check/Kind', () => { |
| 6 | + // ------------------------------------------------------------ |
| 7 | + // Fixtures |
| 8 | + // ------------------------------------------------------------ |
| 9 | + beforeEach(() => TypeRegistry.Set('PI', (_, value) => value === Math.PI)) |
| 10 | + afterEach(() => TypeRegistry.Delete('PI')) |
| 11 | + // ------------------------------------------------------------ |
| 12 | + // Tests |
| 13 | + // ------------------------------------------------------------ |
| 14 | + it('Should validate', () => { |
| 15 | + const T = Type.Unsafe({ [Kind]: 'PI' }) |
| 16 | + Assert.IsTrue(Value.Check(T, Math.PI)) |
| 17 | + }) |
| 18 | + it('Should not validate', () => { |
| 19 | + const T = Type.Unsafe({ [Kind]: 'PI' }) |
| 20 | + Assert.IsFalse(Value.Check(T, Math.PI * 2)) |
| 21 | + }) |
| 22 | + it('Should validate in object', () => { |
| 23 | + const T = Type.Object({ |
| 24 | + x: Type.Unsafe({ [Kind]: 'PI' }), |
| 25 | + }) |
| 26 | + Assert.IsTrue(Value.Check(T, { x: Math.PI })) |
| 27 | + }) |
| 28 | + it('Should not validate in object', () => { |
| 29 | + const T = Type.Object({ |
| 30 | + x: Type.Unsafe({ [Kind]: 'PI' }), |
| 31 | + }) |
| 32 | + Assert.IsFalse(Value.Check(T, { x: Math.PI * 2 })) |
| 33 | + }) |
| 34 | + it('Should validate in array', () => { |
| 35 | + const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) |
| 36 | + Assert.IsTrue(Value.Check(T, [Math.PI])) |
| 37 | + }) |
| 38 | + it('Should not validate in array', () => { |
| 39 | + const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) |
| 40 | + Assert.IsFalse(Value.Check(T, [Math.PI * 2])) |
| 41 | + }) |
| 42 | + it('Should validate in tuple', () => { |
| 43 | + const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) |
| 44 | + Assert.IsTrue(Value.Check(T, [Math.PI])) |
| 45 | + }) |
| 46 | + it('Should not validate in tuple', () => { |
| 47 | + const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) |
| 48 | + Assert.IsFalse(Value.Check(T, [Math.PI * 2])) |
| 49 | + }) |
| 50 | + // ------------------------------------------------------------ |
| 51 | + // Instances |
| 52 | + // ------------------------------------------------------------ |
| 53 | + it('Should receive kind instance on registry callback', () => { |
| 54 | + const stack: string[] = [] |
| 55 | + TypeRegistry.Set('Kind', (schema: unknown) => { |
| 56 | + // prettier-ignore |
| 57 | + return (typeof schema === 'object' && schema !== null && Kind in schema && schema[Kind] === 'Kind' && '$id' in schema && typeof schema.$id === 'string') |
| 58 | + ? (() => { stack.push(schema.$id); return true })() |
| 59 | + : false |
| 60 | + }) |
| 61 | + const A = { [Kind]: 'Kind', $id: 'A' } as TSchema |
| 62 | + const B = { [Kind]: 'Kind', $id: 'B' } as TSchema |
| 63 | + const T = Type.Object({ a: A, b: B }) |
| 64 | + const R = Value.Check(T, { a: null, b: null }) |
| 65 | + Assert.IsTrue(R) |
| 66 | + Assert.IsEqual(stack[0], 'A') |
| 67 | + Assert.IsEqual(stack[1], 'B') |
| 68 | + TypeRegistry.Delete('Kind') |
| 69 | + }) |
| 70 | +}) |
0 commit comments