Skip to content

Commit 29dce4b

Browse files
authored
Version 1.0.40 (#1415)
* Handle Top Level Repair Error * ChangeLog * Version
1 parent 5cc1623 commit 29dce4b

4 files changed

Lines changed: 70 additions & 4 deletions

File tree

changelog/1.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.0.40](https://github.com/sinclairzx81/typebox/pull/1415)
7+
- Support Top Level Value Recovery on Repair
68
- [Revision 1.0.39](https://github.com/sinclairzx81/typebox/pull/1411)
79
- Add IsAnchor, IsDynamicAnchor and IsDynamicRef Guards
810
- [Revision 1.0.38](https://github.com/sinclairzx81/typebox/pull/1410)

src/value/repair/repair.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { Arguments } from '../../system/arguments/index.ts'
3232
import type { TProperties, TSchema, Static } from '../../type/index.ts'
3333
import { FromType } from './from-type.ts'
3434
import { Assert } from '../assert/index.ts'
35+
import { Check } from '../check/index.ts'
36+
import { Create } from '../create/index.ts'
3537

3638
/**
3739
* Repairs a value to match the provided type. This function is intended for data migration
@@ -67,7 +69,8 @@ export function Repair(...args: unknown[]): unknown {
6769
3: (context, type, value) => [context, type, value],
6870
2: (type, value) => [{}, type, value],
6971
})
70-
const result = FromType(context, type, value)
71-
Assert(context, type, result) // ensure correct
72-
return result
72+
const repaired = FromType(context, type, value)
73+
const checked = Check(context, type, repaired) ? repaired : Create(context, type)
74+
Assert(context, type, checked) // ensure correct
75+
return checked
7376
}

tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Range } from './task/range/index.ts'
88
import { Metrics } from './task/metrics/index.ts'
99
import { Task } from 'tasksmith'
1010

11-
const Version = '1.0.39'
11+
const Version = '1.0.40'
1212

1313
// ------------------------------------------------------------------
1414
// Build
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)