|
| 1 | +import { Value } from 'typebox/value' |
| 2 | +import { Type } from 'typebox' |
| 3 | +import { Assert } from 'test' |
| 4 | +import { GlobalsGuard } from 'typebox/guard' |
| 5 | +const Test = Assert.Context('Value.Repair.Base') |
| 6 | + |
| 7 | +// ------------------------------------------------------------------ |
| 8 | +// https://github.com/sinclairzx81/typebox/issues/1388 |
| 9 | +// ------------------------------------------------------------------ |
| 10 | +Test('Should Repair 1', () => { |
| 11 | + class DateType extends Type.Base<Date> { |
| 12 | + public override Check(value: unknown): value is Date { |
| 13 | + return value instanceof Date |
| 14 | + } |
| 15 | + public override Errors(value: unknown): object[] { |
| 16 | + return !this.Check(value) ? [{ message: 'not a Date' }] : [] |
| 17 | + } |
| 18 | + } |
| 19 | + const T = Type.Object({ |
| 20 | + createdDate: new DateType(), |
| 21 | + updatedDate: new DateType() |
| 22 | + }) |
| 23 | + // Create override is required |
| 24 | + Assert.Throws(() => Value.Repair(T, { createdDate: new Date(0) })) |
| 25 | +}) |
| 26 | +Test('Should Repair 2', () => { |
| 27 | + class DateType extends Type.Base<Date> { |
| 28 | + public override Create(): Date { |
| 29 | + return new Date() |
| 30 | + } |
| 31 | + public override Check(value: unknown): value is Date { |
| 32 | + return value instanceof Date |
| 33 | + } |
| 34 | + public override Errors(value: unknown): object[] { |
| 35 | + return !this.Check(value) ? [{ message: 'not a Date' }] : [] |
| 36 | + } |
| 37 | + } |
| 38 | + const T = Type.Object({ |
| 39 | + createdDate: new DateType(), |
| 40 | + updatedDate: new DateType() |
| 41 | + }) |
| 42 | + const R = Value.Repair(T, { createdDate: new Date(0) }) |
| 43 | + Assert.HasPropertyKey(R, 'createdDate') |
| 44 | + Assert.HasPropertyKey(R, 'updatedDate') |
| 45 | + Assert.IsTrue(GlobalsGuard.IsDate(R.createdDate)) |
| 46 | + Assert.IsTrue(GlobalsGuard.IsDate(R.updatedDate)) |
| 47 | + Assert.IsTrue(R.createdDate.getTime() === 0) |
| 48 | + Assert.IsTrue(R.updatedDate.getTime() !== 0) |
| 49 | +}) |
0 commit comments