From 4bf45735c8450228854b70cf68bae1e3e943547a Mon Sep 17 00:00:00 2001 From: David Blass Date: Wed, 16 Oct 2024 11:06:24 -0400 Subject: [PATCH 1/3] some cleanup --- ark/repo/scratch.ts | 15 +++------------ ark/schema/shared/intersections.ts | 22 ++++++++-------------- ark/type/__tests__/realWorld.test.ts | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/ark/repo/scratch.ts b/ark/repo/scratch.ts index ea4e6fee4b..66c9793c77 100644 --- a/ark/repo/scratch.ts +++ b/ark/repo/scratch.ts @@ -1,14 +1,5 @@ import { type } from "arktype" -console.log( - type({ - foo: type("string").pipe(() => 123) - }) - .pipe(c => c) - .to({ - foo: "123" - })({ - foo: "bar" - }) + "" -) -// foo must be 123 (was "bar") +type({ + foo: "string" +}) diff --git a/ark/schema/shared/intersections.ts b/ark/schema/shared/intersections.ts index a75975c8d4..afd915bab0 100644 --- a/ark/schema/shared/intersections.ts +++ b/ark/schema/shared/intersections.ts @@ -73,24 +73,18 @@ export const intersectOrPipeNodes: InternalNodeIntersection if (isPureIntersection && l.equals(r)) return l - let result: BaseNode | Disjoint | null - - if (isPureIntersection) { - if (l.equals(r)) return l - result = _intersectNodes(l, r, ctx) - } else { - result = - l.hasKindIn(...rootKinds) ? - // if l is a RootNode, r will be as well - _pipeNodes(l, r as never, ctx) - : _intersectNodes(l, r, ctx) - } + let result = + isPureIntersection ? _intersectNodes(l, r, ctx) + : l.hasKindIn(...rootKinds) ? + // if l is a RootNode, r will be as well + _pipeNodes(l, r as never, ctx) + : _intersectNodes(l, r, ctx) if (isNode(result)) { // if the result equals one of the operands, preserve its metadata by // returning the original reference - if (l.equals(result)) result = l as never - else if (r.equals(result)) result = r as never + if (l.equals(result)) result = l + else if (r.equals(result)) result = r } intersectionCache[lrCacheKey] = result diff --git a/ark/type/__tests__/realWorld.test.ts b/ark/type/__tests__/realWorld.test.ts index 22b8c88863..aa889fd6c3 100644 --- a/ark/type/__tests__/realWorld.test.ts +++ b/ark/type/__tests__/realWorld.test.ts @@ -1009,4 +1009,19 @@ nospace must be matched by ^\\S*$ (was "One space")`) attest(EitherInput(["str"])).snap(["str"]) }) + + it("intersecting unknown with piped type preserves identity", () => { + const base = type({ + foo: type("string").pipe(() => 123) + }) + .pipe(c => c) + .to({ + foo: "123" + }) + + const identity = base.and("unknown") + + attest(base.json).equals(identity.json) + attest(base.internal.id).equals(identity.internal.id) + }) }) From 8a60477a2a5616b9528871022159c8b959b4c4b9 Mon Sep 17 00:00:00 2001 From: David Blass Date: Wed, 16 Oct 2024 12:36:40 -0400 Subject: [PATCH 2/3] fix inTersection morph --- ark/repo/scratch.ts | 32 ++++++++++++++++++++++++++++++++ ark/schema/roots/morph.ts | 20 ++++++++++---------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/ark/repo/scratch.ts b/ark/repo/scratch.ts index 66c9793c77..32070d1ca2 100644 --- a/ark/repo/scratch.ts +++ b/ark/repo/scratch.ts @@ -3,3 +3,35 @@ import { type } from "arktype" type({ foo: "string" }) + +export type StandardValidate = (input: unknown, ...rest: object[]) => unknown + +// a required prop here should be an error but isn't +const implmementation: StandardValidate = ( + input: unknown, + required: object +) => {} + +/** + * The validate function interface. + */ +export type StandardValidate = ( + input: unknown, + // we can type these as `undefined` to ensure implementations at least handle that case- works similarly to `never` but a bit more intuitive + optionalArg1?: undefined, + optionalArg2?: undefined, + optionalArg3?: undefined +) => unknown + +// this is okay +const implmementation: StandardValidate = ( + input: unknown, + optional?: { someKey: string }, + optional2?: string +) => {} + +// now fails because of the required arg +const badImplmementation: StandardValidate = ( + input: unknown, + required: object +) => {} diff --git a/ark/schema/roots/morph.ts b/ark/schema/roots/morph.ts index c730af2eb5..3dc9f9106b 100644 --- a/ark/schema/roots/morph.ts +++ b/ark/schema/roots/morph.ts @@ -129,16 +129,16 @@ const implementation: nodeImplementationOf = ) }, ...defineRightwardIntersections("morph", (l, r, ctx) => { - const inTersection = intersectOrPipeNodes(l.in, r, ctx) - return inTersection instanceof Disjoint ? inTersection : ( - inTersection.distribute( - branch => ({ - ...l.inner, - in: branch - }), - ctx.$.parseSchema - ) - ) + const inTersection = + l.inner.in ? intersectOrPipeNodes(l.inner.in, r, ctx) : r + return ( + inTersection instanceof Disjoint ? inTersection + : inTersection.equals(l.inner.in) ? l + : ctx.$.node("morph", { + ...l.inner, + in: inTersection + }) + ) }) } }) From a64d5648f6f34d108238b7f6f9800e4f6e9a12f8 Mon Sep 17 00:00:00 2001 From: David Blass Date: Wed, 16 Oct 2024 12:39:11 -0400 Subject: [PATCH 3/3] bump versions --- ark/schema/package.json | 2 +- ark/type/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ark/schema/package.json b/ark/schema/package.json index 885e2328b3..3fcce76c9f 100644 --- a/ark/schema/package.json +++ b/ark/schema/package.json @@ -1,6 +1,6 @@ { "name": "@ark/schema", - "version": "0.18.0", + "version": "0.19.0", "license": "MIT", "author": { "name": "David Blass", diff --git a/ark/type/package.json b/ark/type/package.json index e8cc670e14..d00c4e229a 100644 --- a/ark/type/package.json +++ b/ark/type/package.json @@ -1,7 +1,7 @@ { "name": "arktype", "description": "TypeScript's 1:1 validator, optimized from editor to runtime", - "version": "2.0.0-rc.16", + "version": "2.0.0-rc.17", "license": "MIT", "author": { "name": "David Blass",