Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/json-type-value/Value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ export class Value<T extends Type = Type> {
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;
}
};
}
21 changes: 13 additions & 8 deletions src/json-type/system/TypeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ export class TypeSystem implements Printable {
return alias;
};

public importTypes<A extends TypeMap>(
types: A,
): {readonly [K in keyof A]: TypeAlias<K extends string ? K : never, A[K]>} {
const result = {} as any;
for (const id in types) result[id] = this.alias(id, types[id]);
return result;
}

public readonly unalias = <K extends string>(id: K): TypeAlias<K, Type> => {
const alias = this.aliases.get(id);
if (!alias) throw new Error(`Alias [id = ${id}] not found.`);
Expand Down Expand Up @@ -66,6 +58,19 @@ export class TypeSystem implements Printable {
return result;
}

public importTypes<A extends TypeMap>(
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 (
Expand Down
10 changes: 10 additions & 0 deletions src/json-type/type/TypeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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}]`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type SchemaOfObjectFieldType<F> =

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

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

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