Skip to content

Commit 82cb893

Browse files
authored
Merge pull request #688 from streamich/fix-value-encoding
Improved debugging for `Value` encoding
2 parents 0618874 + 60aa948 commit 82cb893

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/json-type-value/Value.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,24 @@ export class Value<T extends Type = Type> {
1616
else type.encoder(codec.format)(value, encoder);
1717
}
1818
}
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+
}

src/json-type/system/TypeSystem.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ export class TypeSystem implements Printable {
2323
return alias;
2424
};
2525

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-
3426
public readonly unalias = <K extends string>(id: K): TypeAlias<K, Type> => {
3527
const alias = this.aliases.get(id);
3628
if (!alias) throw new Error(`Alias [id = ${id}] not found.`);
@@ -66,6 +58,19 @@ export class TypeSystem implements Printable {
6658
return result;
6759
}
6860

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+
6974
public toString(tab: string = '') {
7075
const nl = () => '';
7176
return (

src/json-type/type/TypeBuilder.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ export class TypeBuilder {
178178
return this.Binary(this.import(node.type), node);
179179
case 'arr':
180180
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);
181183
case 'obj': {
182184
return this.Object(
183185
...node.fields.map((f: any) =>
@@ -195,6 +197,14 @@ export class TypeBuilder {
195197
return this.Or(...node.types.map((t) => this.import(t as schema.Schema))).options(node);
196198
case 'ref':
197199
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+
);
198208
}
199209
throw new Error(`UNKNOWN_NODE [${node.kind}]`);
200210
}

src/json-type/type/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type SchemaOfObjectFieldType<F> =
3535

3636
export type SchemaOfObjectFields<F> = {[K in keyof F]: SchemaOfObjectFieldType<F[K]>};
3737

38-
export type TypeMap = {[name: string]: Type};
38+
export type TypeMap = {[name: string]: schema.Schema};
3939

4040
export type FilterFunctions<T> = {
4141
[K in keyof T as T[K] extends classes.FunctionType<any, any>

0 commit comments

Comments
 (0)