File tree 4 files changed +45
-9
lines changed
4 files changed +45
-9
lines changed Original file line number Diff line number Diff line change @@ -16,3 +16,24 @@ export class Value<T extends Type = Type> {
16
16
else type . encoder ( codec . format ) ( value , encoder ) ;
17
17
}
18
18
}
19
+
20
+ if ( process . env . NODE_ENV !== 'production' ) {
21
+ const encode = Value . prototype . encode ;
22
+ Value . prototype . encode = function ( codec : JsonValueCodec ) : void {
23
+ try {
24
+ encode . call ( this , codec ) ;
25
+ } catch ( error ) {
26
+ try {
27
+ // tslint:disable-next-line no-console
28
+ console . error ( error ) ;
29
+ const type = this . type ;
30
+ if ( type ) {
31
+ const err = type . validator ( 'object' ) ( this . data ) ;
32
+ // tslint:disable-next-line no-console
33
+ console . error ( err ) ;
34
+ }
35
+ } catch { }
36
+ throw error ;
37
+ }
38
+ } ;
39
+ }
Original file line number Diff line number Diff line change @@ -23,14 +23,6 @@ export class TypeSystem implements Printable {
23
23
return alias ;
24
24
} ;
25
25
26
- public importTypes < A extends TypeMap > (
27
- types : A ,
28
- ) : { readonly [ K in keyof A ] : TypeAlias < K extends string ? K : never , A [ K ] > } {
29
- const result = { } as any ;
30
- for ( const id in types ) result [ id ] = this . alias ( id , types [ id ] ) ;
31
- return result ;
32
- }
33
-
34
26
public readonly unalias = < K extends string > ( id : K ) : TypeAlias < K , Type > => {
35
27
const alias = this . aliases . get ( id ) ;
36
28
if ( ! alias ) throw new Error ( `Alias [id = ${ id } ] not found.` ) ;
@@ -66,6 +58,19 @@ export class TypeSystem implements Printable {
66
58
return result ;
67
59
}
68
60
61
+ public importTypes < A extends TypeMap > (
62
+ types : A ,
63
+ ) : {
64
+ readonly [ K in keyof A ] : TypeAlias <
65
+ K extends string ? K : never ,
66
+ /** @todo Replace `any` by inferred type here. */ any
67
+ > ;
68
+ } {
69
+ const result = { } as any ;
70
+ for ( const id in types ) result [ id ] = this . alias ( id , this . t . import ( types [ id ] ) ) ;
71
+ return result ;
72
+ }
73
+
69
74
public toString ( tab : string = '' ) {
70
75
const nl = ( ) => '' ;
71
76
return (
Original file line number Diff line number Diff line change @@ -178,6 +178,8 @@ export class TypeBuilder {
178
178
return this . Binary ( this . import ( node . type ) , node ) ;
179
179
case 'arr' :
180
180
return this . Array ( this . import ( node . type ) , node ) ;
181
+ case 'tup' :
182
+ return this . Tuple ( ...node . types . map ( ( t : schema . Schema ) => this . import ( t ) ) ) . options ( node ) ;
181
183
case 'obj' : {
182
184
return this . Object (
183
185
...node . fields . map ( ( f : any ) =>
@@ -195,6 +197,14 @@ export class TypeBuilder {
195
197
return this . Or ( ...node . types . map ( ( t ) => this . import ( t as schema . Schema ) ) ) . options ( node ) ;
196
198
case 'ref' :
197
199
return this . Ref ( node . ref ) . options ( node ) ;
200
+ case 'fn' :
201
+ return this . Function ( this . import ( node . req as schema . Schema ) , this . import ( node . res as schema . Schema ) ) . options (
202
+ node ,
203
+ ) ;
204
+ case 'fn$' :
205
+ return this . Function$ ( this . import ( node . req as schema . Schema ) , this . import ( node . res as schema . Schema ) ) . options (
206
+ node ,
207
+ ) ;
198
208
}
199
209
throw new Error ( `UNKNOWN_NODE [${ node . kind } ]` ) ;
200
210
}
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ export type SchemaOfObjectFieldType<F> =
35
35
36
36
export type SchemaOfObjectFields < F > = { [ K in keyof F ] : SchemaOfObjectFieldType < F [ K ] > } ;
37
37
38
- export type TypeMap = { [ name : string ] : Type } ;
38
+ export type TypeMap = { [ name : string ] : schema . Schema } ;
39
39
40
40
export type FilterFunctions < T > = {
41
41
[ K in keyof T as T [ K ] extends classes . FunctionType < any , any >
You can’t perform that action at this time.
0 commit comments