diff --git a/src/json-type-value/Value.ts b/src/json-type-value/Value.ts index 7e18ae229d..3a66d3e93e 100644 --- a/src/json-type-value/Value.ts +++ b/src/json-type-value/Value.ts @@ -16,3 +16,24 @@ export class Value { else type.encoder(codec.format)(value, encoder); } } + +if (process.env.NODE_ENV !== 'production') { + const encode = Value.prototype.encode; + Value.prototype.encode = function (codec: JsonValueCodec): void { + try { + encode.call(this, codec); + } catch (error) { + try { + // tslint:disable-next-line no-console + console.error(error); + const type = this.type; + if (type) { + const err = type.validator('object')(this.data); + // tslint:disable-next-line no-console + console.error(err); + } + } catch {} + throw error; + } + }; +} diff --git a/src/json-type/system/TypeSystem.ts b/src/json-type/system/TypeSystem.ts index bf3b9ae239..8e26786a02 100644 --- a/src/json-type/system/TypeSystem.ts +++ b/src/json-type/system/TypeSystem.ts @@ -23,14 +23,6 @@ export class TypeSystem implements Printable { return alias; }; - public importTypes( - types: A, - ): {readonly [K in keyof A]: TypeAlias} { - const result = {} as any; - for (const id in types) result[id] = this.alias(id, types[id]); - return result; - } - public readonly unalias = (id: K): TypeAlias => { const alias = this.aliases.get(id); if (!alias) throw new Error(`Alias [id = ${id}] not found.`); @@ -66,6 +58,19 @@ export class TypeSystem implements Printable { return result; } + public importTypes( + types: A, + ): { + readonly [K in keyof A]: TypeAlias< + K extends string ? K : never, + /** @todo Replace `any` by inferred type here. */ any + >; + } { + const result = {} as any; + for (const id in types) result[id] = this.alias(id, this.t.import(types[id])); + return result; + } + public toString(tab: string = '') { const nl = () => ''; return ( diff --git a/src/json-type/type/TypeBuilder.ts b/src/json-type/type/TypeBuilder.ts index c8857f6bbc..74885ae4e7 100644 --- a/src/json-type/type/TypeBuilder.ts +++ b/src/json-type/type/TypeBuilder.ts @@ -178,6 +178,8 @@ export class TypeBuilder { return this.Binary(this.import(node.type), node); case 'arr': return this.Array(this.import(node.type), node); + case 'tup': + return this.Tuple(...node.types.map((t: schema.Schema) => this.import(t))).options(node); case 'obj': { return this.Object( ...node.fields.map((f: any) => @@ -195,6 +197,14 @@ export class TypeBuilder { return this.Or(...node.types.map((t) => this.import(t as schema.Schema))).options(node); case 'ref': return this.Ref(node.ref).options(node); + case 'fn': + return this.Function(this.import(node.req as schema.Schema), this.import(node.res as schema.Schema)).options( + node, + ); + case 'fn$': + return this.Function$(this.import(node.req as schema.Schema), this.import(node.res as schema.Schema)).options( + node, + ); } throw new Error(`UNKNOWN_NODE [${node.kind}]`); } diff --git a/src/json-type/type/types.ts b/src/json-type/type/types.ts index 77a71f364d..d92c32a70f 100644 --- a/src/json-type/type/types.ts +++ b/src/json-type/type/types.ts @@ -35,7 +35,7 @@ export type SchemaOfObjectFieldType = export type SchemaOfObjectFields = {[K in keyof F]: SchemaOfObjectFieldType}; -export type TypeMap = {[name: string]: Type}; +export type TypeMap = {[name: string]: schema.Schema}; export type FilterFunctions = { [K in keyof T as T[K] extends classes.FunctionType