Comparing version 1.5.1 to 1.5.2
@@ -334,3 +334,3 @@ declare const typeErrSym: unique symbol; | ||
private readonly right; | ||
private readonly _parse; | ||
private _schema; | ||
constructor(left: T, right: K); | ||
@@ -337,0 +337,0 @@ parse(value: unknown, opts?: PathOptions & ObjectOptions<any>): Eval<Infer<T> & Infer<K>>; |
@@ -1104,2 +1104,11 @@ "use strict"; | ||
exports.UnionType = UnionType; | ||
function asUnionType(schema) { | ||
if (schema instanceof UnionType) { | ||
return schema; | ||
} | ||
if (schema instanceof IntersectionType && schema._schema instanceof UnionType) { | ||
return schema._schema; | ||
} | ||
return null; | ||
} | ||
class IntersectionType extends Type { | ||
@@ -1117,25 +1126,18 @@ constructor(left, right) { | ||
} | ||
this._parse = (() => { | ||
// TODO Investigate why I unwrap partials in a new intersection again | ||
if (this.left instanceof UnionType) { | ||
const _schema = this.left.and(this.right); | ||
return (value) => _schema.parse(value); | ||
this._schema = (() => { | ||
const leftUnion = asUnionType(this.left); | ||
if (leftUnion) { | ||
return leftUnion.and(this.right); | ||
} | ||
if (this.right instanceof UnionType) { | ||
const _schema = this.right.and(this.left); | ||
return (value) => _schema.parse(value); | ||
const rightUnion = asUnionType(this.right); | ||
if (rightUnion) { | ||
return rightUnion.and(this.left); | ||
} | ||
if (this.left instanceof PartialType) { | ||
const _schema = new IntersectionType(this.left.schema, this.right); | ||
return (value) => _schema.parse(value); | ||
return new IntersectionType(this.left.schema, this.right); | ||
} | ||
if (this.right instanceof PartialType) { | ||
const _schema = new IntersectionType(this.left, this.right.schema); | ||
return (value) => _schema.parse(value); | ||
return new IntersectionType(this.left, this.right.schema); | ||
} | ||
return (value) => { | ||
this.left.parse(value); | ||
this.right.parse(value); | ||
return value; | ||
}; | ||
return null; | ||
})(); | ||
@@ -1152,3 +1154,9 @@ } | ||
} | ||
return this._parse(value, opts); | ||
if (this._schema) { | ||
// @ts-ignore | ||
return this._schema.parse(value, opts); | ||
} | ||
this.left.parse(value); | ||
this.right.parse(value); | ||
return value; | ||
} | ||
@@ -1155,0 +1163,0 @@ and(schema) { |
{ | ||
"name": "myzod", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "", | ||
@@ -30,5 +30,5 @@ "main": "./libs/index.js", | ||
"@types/mocha": "^8.2.1", | ||
"@types/node": "^14.14.31", | ||
"@types/node": "^14.14.33", | ||
"benchmonkey": "^0.0.8", | ||
"mocha": "^8.3.0", | ||
"mocha": "^8.3.1", | ||
"nyc": "^15.1.0", | ||
@@ -35,0 +35,0 @@ "prettier": "^2.2.1", |
1807
104062