@arktype/schema
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -160,3 +160,7 @@ import type { conform } from "@arktype/util"; | ||
} | ||
export type constrain<t, kind extends PrimitiveConstraintKind, schema extends NodeSchema<kind>> = schemaToConstraint<kind, schema> extends infer constraint ? t extends of<infer base, infer constraints> ? [ | ||
export type constrain<t, kind extends PrimitiveConstraintKind, schema extends NodeSchema<kind>> = _constrain<t, kind, schema> extends infer constrained ? [ | ||
t, | ||
constrained | ||
] extends [constrained, t] ? t : constrained : never; | ||
type _constrain<t, kind extends PrimitiveConstraintKind, schema extends NodeSchema<kind>> = schemaToConstraint<kind, schema> extends infer constraint ? t extends of<infer base, infer constraints> ? [ | ||
number, | ||
@@ -163,0 +167,0 @@ base |
@@ -11,2 +11,3 @@ import { Callable, type Guardable, type Json, type Key, type conform } from "@arktype/util"; | ||
import { type TraverseAllows, type TraverseApply } from "./shared/traversal.js"; | ||
import type { arkKind } from "./shared/utils.js"; | ||
export type UnknownNode = BaseNode | Root; | ||
@@ -33,5 +34,9 @@ export declare abstract class BaseNode< | ||
private _in?; | ||
get in(): BaseNode; | ||
get in(): this extends { | ||
[arkKind]: "root"; | ||
} ? BaseRoot : BaseNode; | ||
private _out?; | ||
get out(): BaseNode; | ||
get out(): this extends { | ||
[arkKind]: "root"; | ||
} ? BaseRoot : BaseNode; | ||
private _description?; | ||
@@ -43,2 +48,3 @@ get description(): string; | ||
equals(other: UnknownNode): boolean; | ||
assertHasKind<kind extends NodeKind>(kind: kind): Node<kind>; | ||
hasKind<kind extends NodeKind>(kind: kind): this is Node<kind>; | ||
@@ -45,0 +51,0 @@ isBasis(): this is Node<BasisKind>; |
@@ -97,2 +97,7 @@ import { Callable, flatMorph, includes, isArray, isEmptyObject, shallowClone, throwError } from "@arktype/util"; | ||
} | ||
assertHasKind(kind) { | ||
if (!this.kind === kind) | ||
throwError(`${this.kind} node was not of asserted kind ${kind}`); | ||
return this; | ||
} | ||
hasKind(kind) { | ||
@@ -99,0 +104,0 @@ return this.kind === kind; |
@@ -38,2 +38,2 @@ import { type RegisteredReference } from "@arktype/util"; | ||
export type PredicateCast<input = never, narrowed extends input = input> = (input: input, ctx: TraversalContext) => input is narrowed; | ||
export type inferNarrow<In, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? In extends of<unknown, infer constraints> ? constrain<of<narrowed, constraints>, "predicate", any> : constrain<narrowed, "predicate", any> : constrain<In, "predicate", any>; | ||
export type inferNarrow<t, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? t extends of<unknown, infer constraints> ? constrain<of<narrowed, constraints>, "predicate", any> : constrain<narrowed, "predicate", any> : constrain<t, "predicate", any>; |
@@ -224,3 +224,3 @@ import { flatMorph, hasDomain, isEmptyObject, isKeyOf, omit, pick, throwParseError } from "@arktype/util"; | ||
expected: source => ` • ${source.errors.map(e => e.expected).join("\n • ")}`, | ||
problem: ctx => `must be...\n${ctx.expected}` | ||
problem: ctx => `${ctx.actual} must be...\n${ctx.expected}` | ||
}, | ||
@@ -227,0 +227,0 @@ intersections: { |
@@ -45,3 +45,4 @@ import { type BuiltinObjectKind, type BuiltinObjects, type Primitive, type anyOrNever, type array, type listable, type show } from "@arktype/util"; | ||
get in(): BaseRoot; | ||
get validatedOut(): BaseRoot | undefined; | ||
lastMorph: Morph<any, unknown> | Root<unknown, any> | undefined; | ||
validatedOut: BaseRoot | undefined; | ||
get out(): BaseRoot; | ||
@@ -52,7 +53,7 @@ rawKeyOf(): BaseRoot; | ||
export type inferMorphOut<morph extends Morph> = Exclude<ReturnType<morph>, ArkError | ArkErrors>; | ||
export type distillIn<t> = includesMorphs<t> extends true ? _distill<t, "in", "base"> : t; | ||
export type distillOut<t> = includesMorphs<t> extends true ? _distill<t, "out", "base"> : t; | ||
export type distillConstrainableIn<t> = includesMorphs<t> extends true ? _distill<t, "in", "constrainable"> : t; | ||
export type distillConstrainableOut<t> = includesMorphs<t> extends true ? _distill<t, "out", "constrainable"> : t; | ||
export type includesMorphs<t> = [ | ||
export type distillIn<t> = includesMorphsOrConstraints<t> extends true ? _distill<t, "in", "base"> : t; | ||
export type distillOut<t> = includesMorphsOrConstraints<t> extends true ? _distill<t, "out", "base"> : t; | ||
export type distillConstrainableIn<t> = includesMorphsOrConstraints<t> extends true ? _distill<t, "in", "constrainable"> : t; | ||
export type distillConstrainableOut<t> = includesMorphsOrConstraints<t> extends true ? _distill<t, "out", "constrainable"> : t; | ||
export type includesMorphsOrConstraints<t> = [ | ||
t, | ||
@@ -68,2 +69,9 @@ _distill<t, "in", "base">, | ||
]) ? false : true; | ||
export type includesMorphs<t> = [ | ||
_distill<t, "in", "constrainable">, | ||
_distill<t, "out", "constrainable"> | ||
] extends ([ | ||
_distill<t, "out", "constrainable">, | ||
_distill<t, "in", "constrainable"> | ||
]) ? false : true; | ||
type _distill<t, io extends "in" | "out", distilledKind extends "base" | "constrainable"> = t extends TerminallyInferredObjectKind | Primitive ? t : unknown extends t ? unknown : t extends MorphAst<infer i, infer o> ? io extends "in" ? _distill<i, io, distilledKind> : _distill<o, io, distilledKind> : t extends DefaultableAst<infer t> ? _distill<t, io, distilledKind> : t extends of<infer base, any> ? distilledKind extends "base" ? _distill<base, io, distilledKind> : t : t extends array ? distillArray<t, io, distilledKind, []> : t extends Function ? t : { | ||
@@ -70,0 +78,0 @@ [k in keyof t]: t[k]; |
@@ -66,4 +66,4 @@ import { arrayFrom, registeredReference, throwParseError } from "@arktype/util"; | ||
traverseApply = (data, ctx) => { | ||
this.in.traverseApply(data, ctx); | ||
ctx.queueMorphs(this.morphs); | ||
this.in.traverseApply(data, ctx); | ||
}; | ||
@@ -76,4 +76,4 @@ expression = `(In: ${this.in.expression}) => Out<${this.out?.expression ?? "unknown"}>`; | ||
} | ||
js.line(js.invoke(this.in)); | ||
js.line(`ctx.queueMorphs(${this.compiledMorphs})`); | ||
js.line(js.invoke(this.in)); | ||
} | ||
@@ -83,8 +83,7 @@ get in() { | ||
} | ||
get validatedOut() { | ||
const lastMorph = this.inner.morphs.at(-1); | ||
return hasArkKind(lastMorph, "root") ? | ||
lastMorph?.out | ||
: undefined; | ||
} | ||
lastMorph = this.inner.morphs.at(-1); | ||
validatedOut = hasArkKind(this.lastMorph, "root") ? | ||
Object.assign(this.referencesById, this.lastMorph.out.referencesById) && | ||
this.lastMorph.out | ||
: undefined; | ||
get out() { | ||
@@ -91,0 +90,0 @@ return this.validatedOut ?? this.$.keywords.unknown.raw; |
@@ -44,3 +44,3 @@ import { type Callable, type Json, type conform } from "@arktype/util"; | ||
describe(description: string): this; | ||
create(input: unknown): unknown; | ||
from(input: unknown): unknown; | ||
pipe(...morphs: Morph[]): BaseRoot; | ||
@@ -50,2 +50,4 @@ private pipeOnce; | ||
constrain<kind extends PrimitiveConstraintKind>(kind: kind, schema: NodeSchema<kind>): BaseRoot; | ||
constrainOut<kind extends PrimitiveConstraintKind>(kind: kind, schema: NodeSchema<kind>): BaseRoot; | ||
private _constrain; | ||
onUndeclaredKey(undeclared: UndeclaredKeyBehavior): BaseRoot; | ||
@@ -84,3 +86,3 @@ } | ||
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this; | ||
create(literal: this["inferIn"]): this["infer"]; | ||
from(literal: this["inferIn"]): this["infer"]; | ||
} | ||
@@ -87,0 +89,0 @@ declare class _Root<t = unknown, $ = any> extends InnerRoot<t, $> { |
@@ -74,3 +74,3 @@ import { includes, omit, throwParseError } from "@arktype/util"; | ||
} | ||
create(input) { | ||
from(input) { | ||
// ideally we wouldn't validate here but for now we need to do determine | ||
@@ -84,4 +84,8 @@ // which morphs to apply | ||
pipeOnce(morph) { | ||
if (hasArkKind(morph, "root")) | ||
return pipeNodesRoot(this, morph, this.$); | ||
if (hasArkKind(morph, "root")) { | ||
const result = pipeNodesRoot(this, morph, this.$); | ||
if (result instanceof Disjoint) | ||
return result.throw(); | ||
return result; | ||
} | ||
if (this.hasKind("union")) { | ||
@@ -103,14 +107,24 @@ const branches = this.branches.map(node => node.pipe(morph)); | ||
narrow(predicate) { | ||
return this.constrain("predicate", predicate); | ||
return this.constrainOut("predicate", predicate); | ||
} | ||
constrain(kind, schema) { | ||
return this._constrain("in", kind, schema); | ||
} | ||
constrainOut(kind, schema) { | ||
return this._constrain("out", kind, schema); | ||
} | ||
_constrain(io, kind, schema) { | ||
const constraint = this.$.node(kind, schema); | ||
if (constraint.impliedBasis && !this.extends(constraint.impliedBasis)) { | ||
if (constraint.impliedBasis && !this[io].extends(constraint.impliedBasis)) { | ||
return throwInvalidOperandError(kind, constraint.impliedBasis, this); | ||
} | ||
return this.and( | ||
// TODO: not an intersection | ||
this.$.node("intersection", { | ||
const partialIntersection = this.$.node("intersection", { | ||
[kind]: constraint | ||
})); | ||
}); | ||
const result = io === "in" ? | ||
intersectNodesRoot(this, partialIntersection, this.$) | ||
: pipeNodesRoot(this, partialIntersection, this.$); | ||
if (result instanceof Disjoint) | ||
result.throw(); | ||
return result; | ||
} | ||
@@ -117,0 +131,0 @@ onUndeclaredKey(undeclared) { |
@@ -238,3 +238,3 @@ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { | ||
const r = this.branches[rIndex]; | ||
const result = intersectNodesRoot(l, r, l.$); | ||
const result = intersectNodesRoot(l.in, r.in, l.$); | ||
if (!(result instanceof Disjoint)) | ||
@@ -421,12 +421,10 @@ continue; | ||
} | ||
const intersection = intersectNodesRoot(branches[i], branches[j], branches[0].$); | ||
const intersection = intersectNodesRoot(branches[i].in, branches[j].in, branches[0].$); | ||
if (intersection instanceof Disjoint) | ||
continue; | ||
if (intersection.equals(branches[i])) { | ||
if (!ordered) { | ||
// preserve ordered branches that are a subtype of a subsequent branch | ||
uniquenessByIndex[i] = false; | ||
} | ||
if (intersection.equals(branches[i].in)) { | ||
// preserve ordered branches that are a subtype of a subsequent branch | ||
uniquenessByIndex[i] = !!ordered; | ||
} | ||
else if (intersection.equals(branches[j])) | ||
else if (intersection.equals(branches[j].in)) | ||
uniquenessByIndex[j] = false; | ||
@@ -433,0 +431,0 @@ } |
@@ -28,3 +28,2 @@ import { ArkError, ArkErrors } from "./errors.js"; | ||
return this.errors; | ||
let out = this.root; | ||
if (this.queuedMorphs.length) { | ||
@@ -37,3 +36,3 @@ for (let i = 0; i < this.queuedMorphs.length; i++) { | ||
// find the object on which the key to be morphed exists | ||
parent = out; | ||
parent = this.root; | ||
for (let pathIndex = 0; pathIndex < path.length - 1; pathIndex++) | ||
@@ -44,3 +43,3 @@ parent = parent[path[pathIndex]]; | ||
for (const morph of morphs) { | ||
const result = morph(parent === undefined ? out : parent[key], this); | ||
const result = morph(parent === undefined ? this.root : parent[key], this); | ||
if (result instanceof ArkErrors) | ||
@@ -59,3 +58,3 @@ return result; | ||
if (parent === undefined) | ||
out = result; | ||
this.root = result; | ||
else | ||
@@ -66,3 +65,3 @@ parent[key] = result; | ||
} | ||
return out; | ||
return this.root; | ||
} | ||
@@ -69,0 +68,0 @@ get currentErrorCount() { |
{ | ||
"name": "@arktype/schema", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "author": { | ||
"dependencies": { | ||
"@arktype/util": "0.0.44" | ||
"@arktype/util": "0.0.45" | ||
}, | ||
@@ -29,0 +29,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310544
6992
+ Added@arktype/util@0.0.45(transitive)
- Removed@arktype/util@0.0.44(transitive)
Updated@arktype/util@0.0.45