|
| 1 | +import { Value } from '@sinclair/typebox/value' |
| 2 | +import { Type } from '@sinclair/typebox' |
| 3 | +import { Assert } from '../../assert/index' |
| 4 | + |
| 5 | +describe('value/check/Constructor', () => { |
| 6 | + it('Should validate constructor 1', () => { |
| 7 | + const T = Type.Constructor([], Type.Object({})) |
| 8 | + Assert.IsTrue(Value.Check(T, class {})) |
| 9 | + }) |
| 10 | + it('Should validate constructor 2', () => { |
| 11 | + const T = Type.Constructor([Type.Number()], Type.Object({})) |
| 12 | + // note: constructor arguments are non-checkable |
| 13 | + Assert.IsTrue(Value.Check(T, class {})) |
| 14 | + }) |
| 15 | + it('Should validate constructor 3', () => { |
| 16 | + const T = Type.Constructor( |
| 17 | + [Type.Number()], |
| 18 | + Type.Object({ |
| 19 | + method: Type.Function([], Type.Void()), |
| 20 | + }), |
| 21 | + ) |
| 22 | + Assert.IsTrue( |
| 23 | + Value.Check( |
| 24 | + T, |
| 25 | + class { |
| 26 | + method() {} |
| 27 | + }, |
| 28 | + ), |
| 29 | + ) |
| 30 | + }) |
| 31 | + it('Should validate constructor 4', () => { |
| 32 | + const T = Type.Constructor( |
| 33 | + [Type.Number()], |
| 34 | + Type.Object({ |
| 35 | + x: Type.Number(), |
| 36 | + y: Type.Number(), |
| 37 | + z: Type.Number(), |
| 38 | + }), |
| 39 | + ) |
| 40 | + Assert.IsTrue( |
| 41 | + Value.Check( |
| 42 | + T, |
| 43 | + class { |
| 44 | + get x() { |
| 45 | + return 1 |
| 46 | + } |
| 47 | + get y() { |
| 48 | + return 1 |
| 49 | + } |
| 50 | + get z() { |
| 51 | + return 1 |
| 52 | + } |
| 53 | + }, |
| 54 | + ), |
| 55 | + ) |
| 56 | + }) |
| 57 | + it('Should not validate constructor 1', () => { |
| 58 | + const T = Type.Constructor([Type.Number()], Type.Object({})) |
| 59 | + Assert.IsFalse(Value.Check(T, 1)) |
| 60 | + }) |
| 61 | + it('Should not validate constructor 2', () => { |
| 62 | + const T = Type.Constructor( |
| 63 | + [Type.Number()], |
| 64 | + Type.Object({ |
| 65 | + x: Type.Number(), |
| 66 | + y: Type.Number(), |
| 67 | + z: Type.Number(), |
| 68 | + }), |
| 69 | + ) |
| 70 | + Assert.IsFalse( |
| 71 | + Value.Check( |
| 72 | + T, |
| 73 | + class { |
| 74 | + get x() { |
| 75 | + return null |
| 76 | + } |
| 77 | + get y() { |
| 78 | + return null |
| 79 | + } |
| 80 | + get z() { |
| 81 | + return null |
| 82 | + } |
| 83 | + }, |
| 84 | + ), |
| 85 | + ) |
| 86 | + }) |
| 87 | +}) |
0 commit comments