Skip to content

Commit 97fd896

Browse files
authored
Version 1.0.31 (#1390)
* Add Repair Path for Base * ChangeLog * Version
1 parent 27fe9ce commit 97fd896

5 files changed

Lines changed: 95 additions & 1 deletion

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.31](https://github.com/sinclairzx81/typebox/pull/1390)
7+
- Add Repair Path for Base Types
68
- [Revision 1.0.30](https://github.com/sinclairzx81/typebox/pull/1386)
79
- Finalize Base Schema | Translations
810
- [Revision 1.0.29](https://github.com/sinclairzx81/typebox/pull/1385)

src/value/repair/from-base.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*--------------------------------------------------------------------------
2+
3+
TypeBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2017-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
// deno-fmt-ignore-file
30+
31+
import type { TProperties, Base } from '../../type/index.ts'
32+
import { FromUnknown } from './from-unknown.ts'
33+
34+
// ------------------------------------------------------------------
35+
// FromBase
36+
// ------------------------------------------------------------------
37+
export function FromBase(context: TProperties, type: Base, value: unknown): unknown {
38+
return FromUnknown(context, type, value)
39+
}

src/value/repair/from-type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { Guard, GlobalsGuard } from '../../guard/index.ts'
3232
import * as T from '../../type/index.ts'
3333

3434
import { FromArray } from './from-array.ts'
35+
import { FromBase } from './from-base.ts'
3536
import { FromEnum } from './from-enum.ts'
3637
import { FromIntersect } from './from-intersect.ts'
3738
import { FromObject } from './from-object.ts'
@@ -75,6 +76,9 @@ function AssertRepairableType(context: T.TProperties, type: T.TSchema, value: un
7576
// FromType
7677
// ------------------------------------------------------------------
7778
export function FromType(context: T.TProperties, type: T.TSchema, value: unknown): unknown {
79+
// Intercept Base
80+
if (T.IsBase(type)) return FromBase(context, type, value)
81+
// Standard Repair
7882
AssertRepairableValue(context, type, value)
7983
AssertRepairableType(context, type, value)
8084
return (

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.30'
11+
const Version = '1.0.31'
1212

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

Comments
 (0)