Skip to content

Commit b1ee3a6

Browse files
authored
Version 1.0.80 (#1524)
* Retain Ref Options on Instantiate * ChangeLog * Version
1 parent 2d69ad2 commit b1ee3a6

5 files changed

Lines changed: 59 additions & 15 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.80](https://github.com/sinclairzx81/typebox/pull/1524)
7+
- Retain Ref Options on Instantiate
68
- [Revision 1.0.79](https://github.com/sinclairzx81/typebox/pull/1518)
79
- Fix IsMinLength String Check | 0-Length | No Enumeration
810
- [Revision 1.0.78](https://github.com/sinclairzx81/typebox/pull/1514)

src/type/engine/instantiate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export type TInstantiateType<Context extends TProperties, State extends TState,
344344
>,
345345
Type extends TSchema = Modifiers[0],
346346
Instantiated extends TSchema = (
347-
Type extends TRef<infer Ref extends string> ? TRefInstantiate<Context, State, Ref> :
347+
Type extends TRef<infer Ref extends string> ? TRefInstantiate<Context, State, Type, Ref> :
348348
Type extends TArray<infer Type extends TSchema> ? TArray<TInstantiateType<Context, State, Type>> :
349349
Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TInstantiateType<Context, State, Type>> :
350350
Type extends TCall<infer Target extends TSchema, infer Parameters extends TSchema[]> ? TCallInstantiate<Context, State, Target, Parameters> :
@@ -374,7 +374,7 @@ export function InstantiateType<Context extends TProperties, State extends TStat
374374
IsOptional(input) ? 'add' : 'none')
375375
const type = IsBase(modifiers[0]) ? modifiers[0].Clone() : modifiers[0]
376376
const instantiated = (
377-
IsRef(type) ? RefInstantiate(context, state, type.$ref) :
377+
IsRef(type) ? RefInstantiate(context, state, type, type.$ref) :
378378
IsArray(type) ? Array(InstantiateType(context, state, type.items), ArrayOptions(type)) :
379379
IsAsyncIterator(type) ? AsyncIterator(InstantiateType(context, state, type.iteratorItems), AsyncIteratorOptions(type)) :
380380
IsCall(type) ? CallInstantiate(context, state, type.target, type.arguments) :

src/type/engine/ref/instantiate.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030

3131
import { TProperties } from '../../types/properties.ts'
32-
import { type TRef, Ref } from '../../types/ref.ts'
3332
import { type TState, type TInstantiateType, InstantiateType } from '../instantiate.ts'
3433
import { type TCyclicCheck, CyclicCheck } from '../cyclic/check.ts'
34+
import { type TRef } from '../../types/ref.ts'
3535

3636
// ------------------------------------------------------------------
3737
// Instantiate
3838
// ------------------------------------------------------------------
39-
export type TRefInstantiate<Context extends TProperties, State extends TState, Ref extends string> = (
39+
export type TRefInstantiate<Context extends TProperties, State extends TState, Type extends TRef, Ref extends string> = (
4040
Ref extends keyof Context
4141
? TCyclicCheck<[Ref], Context, Context[Ref]> extends true
42-
? TRef<Ref>
42+
? Type
4343
: TInstantiateType<Context, State, Context[Ref]>
44-
: TRef<Ref>
44+
: Type
4545
)
46-
export function RefInstantiate<Context extends TProperties, State extends TState, Ref extends string>
47-
(context: Context, state: State, ref: Ref):
48-
TRefInstantiate<Context, State, Ref> {
46+
export function RefInstantiate<Context extends TProperties, State extends TState, Type extends TRef, Ref extends string>
47+
(context: Context, state: State, type: Type, ref: Ref):
48+
TRefInstantiate<Context, State, Type, Ref> {
4949
return (
5050
ref in context
5151
? CyclicCheck([ref], context, context[ref])
52-
? Ref(ref)
52+
? type
5353
: InstantiateType(context, state, context[ref])
54-
: Ref(ref)
54+
: type
5555
) as never
5656
}

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

1313
// ------------------------------------------------------------------
1414
// Build

test/typebox/runtime/type/engine/action/ref.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,66 @@ Test('Should Ref 1', () => {
99
const T: Type.TString = Type.Instantiate({ A }, R)
1010
Assert.IsTrue(Type.IsString(T))
1111
})
12-
Test('Should Ref 1', () => {
12+
Test('Should Ref 2', () => {
1313
const A = Type.String()
1414
const R = Type.Ref('B')
1515
const T: Type.TRef<'B'> = Type.Instantiate({ A }, R)
1616
Assert.IsTrue(Type.IsRef(T))
1717
Assert.IsEqual(T.$ref, 'B')
1818
})
19-
Test('Should Ref 1', () => {
19+
Test('Should Ref 3', () => {
2020
const A = Type.Ref('B')
2121
const B = Type.Ref('A')
2222
const T: Type.TRef<'A'> = Type.Instantiate({ A, B }, B)
2323
Assert.IsTrue(Type.IsRef(T))
2424
Assert.IsEqual(T.$ref, 'A')
2525
})
26-
Test('Should Ref 1', () => {
26+
Test('Should Ref 4', () => {
2727
const A = Type.Ref('B')
2828
const B = Type.Ref('A')
2929
const T: Type.TRef<'A'> = Type.Instantiate({ A, B }, B)
3030
Assert.IsTrue(Type.IsRef(T))
3131
Assert.IsEqual(T.$ref, 'A')
3232
})
33+
// ------------------------------------------------------------------
34+
// https://github.com/sinclairzx81/typebox/issues/1522
35+
//
36+
// Non-Instantiated Ref Must Retain Options
37+
// ------------------------------------------------------------------
38+
Test('Should Ref 5', () => {
39+
const T: Type.TRef<'A'> = Type.Ref('A', { foo: 'bar' })
40+
const S = Type.Instantiate({}, T) // no-target
41+
42+
Assert.IsTrue(Type.IsRef(S))
43+
Assert.IsEqual(S.$ref, 'A')
44+
Assert.HasPropertyKey(S, 'foo')
45+
Assert.IsEqual(S.foo, 'bar')
46+
})
47+
Test('Should Ref 6', () => {
48+
const T: Type.TRef<'A'> = Type.Ref('A', { foo: 'bar' })
49+
const S: Type.TString = Type.Instantiate({ A: Type.String({ foo: 'baz' }) }, T)
50+
51+
Assert.IsTrue(Type.IsString(S))
52+
Assert.HasPropertyKey(S, 'foo')
53+
Assert.IsEqual(S.foo, 'baz')
54+
})
55+
Test('Should Ref 7', () => {
56+
const R: Type.TRecord<'^.*$', Type.TRef<'A'>> = Type.Record(Type.String(), Type.Ref('A', { foo: 'bar' }))
57+
const S: Type.TRecord<'^.*$', Type.TRef<'A'>> = Type.Instantiate({}, R) // no-target
58+
const V: Type.TRef<'A'> = Type.RecordValue(S)
59+
60+
Assert.IsTrue(Type.IsRecord(S))
61+
Assert.IsEqual(V.$ref, 'A')
62+
Assert.HasPropertyKey(V, 'foo')
63+
Assert.IsEqual(V.foo, 'bar')
64+
})
65+
Test('Should Ref 8', () => {
66+
const R: Type.TRecord<'^.*$', Type.TRef<'A'>> = Type.Record(Type.String(), Type.Ref('A', { foo: 'bar' }))
67+
const S: Type.TRecord<'^.*$', Type.TString> = Type.Instantiate({ A: Type.String({ foo: 'baz' }) }, R)
68+
const V: Type.TString = Type.RecordValue(S)
69+
70+
Assert.IsTrue(Type.IsRecord(S))
71+
Assert.IsTrue(Type.IsString(V))
72+
Assert.HasPropertyKey(V, 'foo')
73+
Assert.IsEqual(V.foo, 'baz')
74+
})

0 commit comments

Comments
 (0)