Skip to content

Commit

Permalink
wip removal of circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
o-alexandrov committed Nov 19, 2020
1 parent def3054 commit 87238de
Show file tree
Hide file tree
Showing 42 changed files with 585 additions and 546 deletions.
4 changes: 1 addition & 3 deletions src/PseudoPromise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { INVALID } from './helpers/util';
import { ZodError } from './ZodError';
// import { INVALID } from './util';
import { INVALID, ZodError } from './internal';

type Func = (arg: any, ctx: { async: boolean }) => any;
type FuncItem = { type: 'function'; function: Func };
Expand Down
56 changes: 56 additions & 0 deletions src/ZodDef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ZodStringDef } from './types/string';
import { ZodNumberDef } from './types/number';
import { ZodBigIntDef } from './types/bigint';
import { ZodBooleanDef } from './types/boolean';
import { ZodDateDef } from './types/date';
import { ZodUndefinedDef } from './types/undefined';
import { ZodNullDef } from './types/null';
import { ZodAnyDef } from './types/any';
import { ZodUnknownDef } from './types/unknown';
import { ZodNeverDef } from './types/never';
import { ZodVoidDef } from './types/void';
import { ZodArrayDef } from './types/array';
import { ZodObjectDef } from './types/object';
import { ZodUnionDef } from './types/union';
import { ZodIntersectionDef } from './types/intersection';
import { ZodTupleDef } from './types/tuple';
import { ZodRecordDef } from './types/record';
import { ZodMapDef } from './types/map';
import { ZodFunctionDef } from './types/function';
import { ZodLazyDef } from './types/lazy';
import { ZodLiteralDef } from './types/literal';
import { ZodEnumDef } from './types/enum';
import { ZodNativeEnumDef } from './types/nativeEnum';
import { ZodPromiseDef } from './types/promise';
import { ZodTransformerDef } from './types/transformer';
import { ZodOptionalDef } from './types/optional';
import { ZodNullableDef } from './types/nullable';

export type ZodDef =
| ZodStringDef
| ZodNumberDef
| ZodBigIntDef
| ZodBooleanDef
| ZodDateDef
| ZodUndefinedDef
| ZodNullDef
| ZodAnyDef
| ZodUnknownDef
| ZodNeverDef
| ZodVoidDef
| ZodArrayDef
| ZodObjectDef
| ZodUnionDef
| ZodIntersectionDef
| ZodTupleDef
| ZodRecordDef
| ZodMapDef
| ZodFunctionDef
| ZodLazyDef
| ZodLiteralDef
| ZodEnumDef
| ZodTransformerDef
| ZodNativeEnumDef
| ZodOptionalDef
| ZodNullableDef
| ZodPromiseDef;
3 changes: 1 addition & 2 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { util } from './helpers/util';
import { ZodParsedType } from './parser';
import { util, ZodParsedType } from './internal';

export const ZodIssueCode = util.arrayToEnum([
'invalid_type',
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/map.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as z from '../index';
import { util } from '../helpers/util';
import { ZodIssueCode } from '../index';
import { util, ZodIssueCode } from '../internal';

const stringMap = z.map(z.string(), z.string());
type stringMap = z.infer<typeof stringMap>;
Expand Down
76 changes: 37 additions & 39 deletions src/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// import * as z from './index';
import { ZodDef } from '.';
import { util } from './helpers/util';
import { ZodType, ZodTypes } from './types/base';
// import { ZodDef, util, ZodType, ZodTypes } from './internal';
import * as z from './internal';

type TypeResult = { schema: any; id: string; type: string };

const isOptional = (schema: ZodType<any, any>): boolean => {
const isOptional = (schema: z.ZodType<any, any>): boolean => {
// const def: z.ZodDef = schema._def;
// if (def.t === ZodTypes.undefined) return true;
// else if (def.t === ZodTypes.intersection) {
// if (def.t === z.ZodTypes.undefined) return true;
// else if (def.t === z.ZodTypes.intersection) {
// return isOptional(def.right) && isOptional(def.left);
// } else if (def.t === ZodTypes.union) {
// } else if (def.t === z.ZodTypes.union) {
// return def.options.map(isOptional).some(x => x === true);
// }
// return false;
Expand All @@ -26,7 +24,7 @@ export class ZodCodeGenerator {
return `IZod${this.serial++}`;
};

findBySchema = (schema: ZodType<any, any>) => {
findBySchema = (schema: z.ZodType<any, any>) => {
return this.seen.find(s => s.schema === schema);
};

Expand All @@ -52,11 +50,11 @@ ${this.seen
return found;
};

generate = (schema: ZodType<any, any>): TypeResult => {
generate = (schema: z.ZodType<any, any>): TypeResult => {
const found = this.findBySchema(schema);
if (found) return found;

const def: ZodDef = schema._def;
const def: z.ZodDef = schema._def;

const id = this.randomId();

Expand All @@ -69,35 +67,35 @@ ${this.seen
this.seen.push(ty);

switch (def.t) {
case ZodTypes.string:
case z.ZodTypes.string:
return this.setType(id, `string`);
case ZodTypes.number:
case z.ZodTypes.number:
return this.setType(id, `number`);
case ZodTypes.bigint:
case z.ZodTypes.bigint:
return this.setType(id, `bigint`);
case ZodTypes.boolean:
case z.ZodTypes.boolean:
return this.setType(id, `boolean`);
case ZodTypes.date:
case z.ZodTypes.date:
return this.setType(id, `Date`);
case ZodTypes.undefined:
case z.ZodTypes.undefined:
return this.setType(id, `undefined`);
case ZodTypes.null:
case z.ZodTypes.null:
return this.setType(id, `null`);
case ZodTypes.any:
case z.ZodTypes.any:
return this.setType(id, `any`);
case ZodTypes.unknown:
case z.ZodTypes.unknown:
return this.setType(id, `unknown`);
case ZodTypes.never:
case z.ZodTypes.never:
return this.setType(id, `never`);
case ZodTypes.void:
case z.ZodTypes.void:
return this.setType(id, `void`);
case ZodTypes.literal:
case z.ZodTypes.literal:
const val = def.value;
const literalType = typeof val === 'string' ? `"${val}"` : `${val}`;
return this.setType(id, literalType);
case ZodTypes.enum:
case z.ZodTypes.enum:
return this.setType(id, def.values.map(v => `"${v}"`).join(' | '));
case ZodTypes.object:
case z.ZodTypes.object:
const objectLines: string[] = [];
const shape = def.shape();

Expand All @@ -112,7 +110,7 @@ ${this.seen
.join('\n')}\n}`;
this.setType(id, `${baseStruct}`);
break;
case ZodTypes.tuple:
case z.ZodTypes.tuple:
const tupleLines: string[] = [];
for (const elSchema of def.items) {
const elType = this.generate(elSchema);
Expand All @@ -122,56 +120,56 @@ ${this.seen
.map(line => ` ${line},`)
.join('\n')}\n]`;
return this.setType(id, `${baseTuple}`);
case ZodTypes.array:
case z.ZodTypes.array:
return this.setType(id, `${this.generate(def.type).id}[]`);
case ZodTypes.function:
case z.ZodTypes.function:
const args = this.generate(def.args);
const returns = this.generate(def.returns);
return this.setType(id, `(...args: ${args.id})=>${returns.id}`);
case ZodTypes.promise:
case z.ZodTypes.promise:
const promValue = this.generate(def.type);
return this.setType(id, `Promise<${promValue.id}>`);
case ZodTypes.union:
case z.ZodTypes.union:
const unionLines: string[] = [];
for (const elSchema of def.options) {
const elType = this.generate(elSchema);
unionLines.push(elType.id);
}
return this.setType(id, unionLines.join(` | `));
case ZodTypes.intersection:
case z.ZodTypes.intersection:
return this.setType(
id,
`${this.generate(def.left).id} & ${this.generate(def.right).id}`,
);
case ZodTypes.record:
case z.ZodTypes.record:
return this.setType(
id,
`{[k:string]: ${this.generate(def.valueType).id}}`,
);
case ZodTypes.transformer:
case z.ZodTypes.transformer:
return this.setType(id, this.generate(def.output).id);
case ZodTypes.map:
case z.ZodTypes.map:
return this.setType(
id,
`Map<${this.generate(def.keyType).id}, ${
this.generate(def.valueType).id
}>`,
);
case ZodTypes.lazy:
case z.ZodTypes.lazy:
const lazyType = def.getter();
return this.setType(id, this.generate(lazyType).id);
case ZodTypes.nativeEnum:
case z.ZodTypes.nativeEnum:
// const lazyType = def.getter();
return this.setType(id, 'asdf');
case ZodTypes.optional:
case z.ZodTypes.optional:
return this.setType(
id,
`${this.generate(def.innerType).id} | undefined`,
);
case ZodTypes.nullable:
case z.ZodTypes.nullable:
return this.setType(id, `${this.generate(def.innerType).id} | null`);
default:
util.assertNever(def);
z.util.assertNever(def);
}
return this.findById(id);
};
Expand Down
3 changes: 1 addition & 2 deletions src/defaultErrorMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ZodIssueCode, ZodIssueOptionalMessage } from './ZodError';
import { util } from './helpers/util';
import { ZodIssueCode, ZodIssueOptionalMessage, util } from './internal';

type ErrorMapCtx = {
// path: (string | number)[];
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/maskUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Primitive } from './primitive';
import { Primitive } from '../internal';

type AnyObject = { [k: string]: any };
type IsAny<T> = any extends T ? (T extends any ? true : false) : false;
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/objectUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ZodRawShape, ZodTypes } from '../types/base';
import { ZodIntersection } from '../types/intersection';
import { ZodObject, AnyZodObject } from '../types/object';
import {
ZodRawShape,
ZodTypes,
ZodIntersection,
ZodObject,
AnyZodObject,
} from '../internal';

export namespace objectUtil {
// export interface ZodObjectParams {
Expand Down
31 changes: 15 additions & 16 deletions src/helpers/partialUtil.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import * as z from '../index';
import { AnyZodObject } from '../types/object';
import { AnyZodObject, ZodTypeAny, ZodObject, ZodOptional } from '../internal';

export namespace partialUtil {
export type RootDeepPartial<T extends z.ZodTypeAny> = {
// optional: T extends z.ZodOptional<z.ZodTypeAny> ? T : z.ZodOptional<T>;
// array: T extends z.ZodArray<infer Type> ? z.ZodArray<DeepPartial<Type>> : never;
export type RootDeepPartial<T extends ZodTypeAny> = {
// optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
object: T extends AnyZodObject
? z.ZodObject<
? ZodObject<
{ [k in keyof T['_shape']]: DeepPartial<T['_shape'][k]> },
T['_unknownKeys'],
T['_catchall']
>
: never;
rest: ReturnType<T['optional']>; // z.ZodOptional<T>;
rest: ReturnType<T['optional']>; // ZodOptional<T>;
}[T extends AnyZodObject
? 'object' // T extends z.ZodOptional<any> // ? 'optional' // :
? 'object' // T extends ZodOptional<any> // ? 'optional' // :
: 'rest'];

export type DeepPartial<T extends z.ZodTypeAny> = {
// optional: T extends z.ZodOptional<z.ZodTypeAny> ? T : z.ZodOptional<T>;
// array: T extends z.ZodArray<infer Type> ? z.ZodArray<DeepPartial<Type>> : never;
object: T extends z.ZodObject<infer Shape, infer Params, infer Catchall>
? z.ZodOptional<
z.ZodObject<
export type DeepPartial<T extends ZodTypeAny> = {
// optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
object: T extends ZodObject<infer Shape, infer Params, infer Catchall>
? ZodOptional<
ZodObject<
{ [k in keyof Shape]: DeepPartial<Shape[k]> },
Params,
Catchall
>
>
: never;
rest: ReturnType<T['optional']>;
}[T extends z.ZodObject<any>
? 'object' // T extends z.ZodOptional<any> // ? 'optional' // :
}[T extends ZodObject<any>
? 'object' // T extends ZodOptional<any> // ? 'optional' // :
: 'rest'];
}
Loading

0 comments on commit 87238de

Please sign in to comment.