|
| 1 | +import { Value } from 'typebox/value' |
| 2 | +import { Type } from 'typebox' |
| 3 | +import { Assert } from 'test' |
| 4 | + |
| 5 | +const Test = Assert.Context('Value.Repair.Refine') |
| 6 | + |
| 7 | +// ------------------------------------------------------------------ |
| 8 | +// https://github.com/sinclairzx81/typebox/issues/1414 |
| 9 | +// ------------------------------------------------------------------ |
| 10 | +Test('Should Refine 1', () => { |
| 11 | + const T = Type.Refine( |
| 12 | + Type.Object({ |
| 13 | + greater: Type.Integer({ default: 10 }), |
| 14 | + smaller: Type.Integer({ default: 5 }) |
| 15 | + }), |
| 16 | + (data) => data.greater > data.smaller |
| 17 | + ) |
| 18 | + const R = Value.Repair(T, { greater: 3, smaller: 7 }) |
| 19 | + Assert.IsEqual(R, { |
| 20 | + greater: 10, |
| 21 | + smaller: 5 |
| 22 | + }) |
| 23 | +}) |
| 24 | +Test('Should Refine 2', () => { |
| 25 | + const T = Type.Refine( |
| 26 | + Type.Object({ |
| 27 | + greater: Type.Integer(), |
| 28 | + smaller: Type.Integer() |
| 29 | + }, { |
| 30 | + default: { |
| 31 | + greater: 10, |
| 32 | + smaller: 5 |
| 33 | + } |
| 34 | + }), |
| 35 | + (data) => data.greater > data.smaller |
| 36 | + ) |
| 37 | + const R = Value.Repair(T, { greater: 3, smaller: 7 }) |
| 38 | + Assert.IsEqual(R, { |
| 39 | + greater: 10, |
| 40 | + smaller: 5 |
| 41 | + }) |
| 42 | +}) |
| 43 | +Test('Should Refine 3', () => { |
| 44 | + const T = Type.Refine( |
| 45 | + Type.Object({ |
| 46 | + greater: Type.Integer({ default: 10 }), |
| 47 | + smaller: Type.Integer({ default: 5 }) |
| 48 | + }, { |
| 49 | + default: { // top-level overrides subschema |
| 50 | + greater: 100, |
| 51 | + smaller: 50 |
| 52 | + } |
| 53 | + }), |
| 54 | + (data) => data.greater > data.smaller |
| 55 | + ) |
| 56 | + const R = Value.Repair(T, { greater: 3, smaller: 7 }) |
| 57 | + Assert.IsEqual(R, { |
| 58 | + greater: 100, |
| 59 | + smaller: 50 |
| 60 | + }) |
| 61 | +}) |
0 commit comments