@arktype/schema
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -7,6 +7,7 @@ import { type array, type describe, type listable, type satisfy } from "@ark/util"; | ||
import type { BaseRoot } from "./roots/root.ts"; | ||
import type { BaseScope } from "./scope.ts"; | ||
import type { NodeCompiler } from "./shared/compile.ts"; | ||
import type { BaseNodeDeclaration } from "./shared/declare.ts"; | ||
import { Disjoint } from "./shared/disjoint.ts"; | ||
import { type ConstraintKind, type IntersectionContext, type NodeKind, type RootKind, type StructuralKind, type kindLeftOf } from "./shared/implement.ts"; | ||
import { type ConstraintKind, type IntersectionContext, type NodeKind, type RootKind, type StructuralKind, type UnknownAttachments, type kindLeftOf } from "./shared/implement.ts"; | ||
import type { JsonSchema } from "./shared/jsonSchema.ts"; | ||
@@ -31,3 +32,4 @@ import type { TraverseAllows, TraverseApply } from "./shared/traversal.ts"; | ||
out d extends Constraint.Declaration = Constraint.Declaration> extends BaseNode<d> { | ||
readonly [arkKind] = "constraint"; | ||
readonly [arkKind]: "constraint"; | ||
constructor(attachments: UnknownAttachments, $: BaseScope); | ||
abstract readonly impliedBasis: BaseRoot | null; | ||
@@ -34,0 +36,0 @@ readonly impliedSiblings?: array<BaseConstraint>; |
@@ -9,3 +9,10 @@ import { append, appendUnique, capitalize, isArray, throwInternalError, throwParseError } from "@ark/util"; | ||
export class BaseConstraint extends BaseNode { | ||
[arkKind] = "constraint"; | ||
constructor(attachments, $) { | ||
super(attachments, $); | ||
// define as a getter to avoid it being enumerable/spreadable | ||
Object.defineProperty(this, arkKind, { | ||
value: "constraint", | ||
enumerable: false | ||
}); | ||
} | ||
impliedSiblings; | ||
@@ -12,0 +19,0 @@ intersect(r) { |
@@ -10,8 +10,10 @@ import { inferred, type array } from "@ark/util"; | ||
import type { ExclusiveDateRangeSchema, ExclusiveNumericRangeSchema, InclusiveDateRangeSchema, InclusiveNumericRangeSchema, LimitSchemaValue, UnknownRangeSchema } from "../refinements/range.ts"; | ||
import type { BaseScope } from "../scope.ts"; | ||
import type { BaseNodeDeclaration, MetaSchema } from "../shared/declare.ts"; | ||
import { Disjoint } from "../shared/disjoint.ts"; | ||
import { type NodeKind, type RootKind, type kindRightOf } from "../shared/implement.ts"; | ||
import { type NodeKind, type RootKind, type UnknownAttachments, type kindRightOf } from "../shared/implement.ts"; | ||
import type { JsonSchema } from "../shared/jsonSchema.ts"; | ||
import { arkKind } from "../shared/utils.ts"; | ||
import type { NodeEntryFlatMapper, UndeclaredKeyBehavior } from "../structure/structure.ts"; | ||
import type { Prop } from "../structure/prop.ts"; | ||
import type { PropFlatMapper, UndeclaredKeyBehavior } from "../structure/structure.ts"; | ||
import type { Morph } from "./morph.ts"; | ||
@@ -25,4 +27,5 @@ import type { Union } from "./union.ts"; | ||
out d extends InternalRootDeclaration = InternalRootDeclaration> extends BaseNode<d> { | ||
readonly [arkKind] = "root"; | ||
readonly [arkKind]: "root"; | ||
readonly [inferred]: unknown; | ||
constructor(attachments: UnknownAttachments, $: BaseScope); | ||
get internal(): this; | ||
@@ -44,3 +47,3 @@ as(): this; | ||
assert(data: unknown): unknown; | ||
map(flatMapEntry: NodeEntryFlatMapper): BaseRoot; | ||
map(flatMapEntry: PropFlatMapper): BaseRoot; | ||
pick(...keys: KeyOrKeyNode[]): BaseRoot; | ||
@@ -52,2 +55,3 @@ omit(...keys: KeyOrKeyNode[]): BaseRoot; | ||
keyof(): BaseRoot; | ||
get props(): Prop.Node[]; | ||
merge(r: unknown): BaseRoot; | ||
@@ -118,5 +122,17 @@ private applyStructuralOperation; | ||
export type schemaKindOrRightOf<kind extends RootKind> = kind | schemaKindRightOf<kind>; | ||
export type StructuralOperationName = "keyof" | "pick" | "omit" | "get" | "map" | "required" | "partial" | "merge"; | ||
export type StructuralOperationBranchResultByName = { | ||
keyof: Union.ChildNode; | ||
pick: Union.ChildNode; | ||
omit: Union.ChildNode; | ||
get: Union.ChildNode; | ||
map: Union.ChildNode; | ||
required: Union.ChildNode; | ||
partial: Union.ChildNode; | ||
merge: Union.ChildNode; | ||
props: array<Prop.Node>; | ||
}; | ||
export type StructuralOperationName = keyof StructuralOperationBranchResultByName; | ||
export declare const writeLiteralUnionEntriesMessage: (expression: string) => string; | ||
export declare const writeNonStructuralOperandMessage: <operation extends StructuralOperationName, operand extends string>(operation: operation, operand: operand) => writeNonStructuralOperandMessage<operation, operand>; | ||
export type writeNonStructuralOperandMessage<operation extends StructuralOperationName, operand extends string> = `${operation} operand must be an object (was ${operand})`; | ||
export {}; |
@@ -11,3 +11,7 @@ import { includes, inferred, omit, throwInternalError, throwParseError } from "@ark/util"; | ||
export class BaseRoot extends BaseNode { | ||
[arkKind] = "root"; | ||
constructor(attachments, $) { | ||
super(attachments, $); | ||
// define as a getter to avoid it being enumerable/spreadable | ||
Object.defineProperty(this, arkKind, { value: "root", enumerable: false }); | ||
} | ||
get internal() { | ||
@@ -89,2 +93,7 @@ return this; | ||
} | ||
get props() { | ||
if (this.branches.length !== 1) | ||
return throwParseError(writeLiteralUnionEntriesMessage(this.expression)); | ||
return [...this.applyStructuralOperation("props", [])[0]]; | ||
} | ||
merge(r) { | ||
@@ -114,2 +123,4 @@ const rNode = this.$.parseDefinition(r); | ||
return structure.get(...args); | ||
if (operation === "props") | ||
return structure.props; | ||
const structuralMethodName = operation === "required" ? "require" | ||
@@ -347,2 +358,4 @@ : operation === "partial" ? "optionalize" | ||
}; | ||
export const writeLiteralUnionEntriesMessage = (expression) => `Props cannot be extracted from a union. Use .distribute to extract props from each branch instead. Received: | ||
${expression}`; | ||
export const writeNonStructuralOperandMessage = (operation, operand) => `${operation} operand must be an object (was ${operand})`; |
@@ -1,2 +0,2 @@ | ||
import { throwParseError } from "@ark/util"; | ||
import { printable, throwParseError } from "@ark/util"; | ||
import { ArkErrors } from "../shared/errors.js"; | ||
@@ -37,3 +37,3 @@ import { implementNode } from "../shared/implement.js"; | ||
} | ||
expression = `${this.compiledKey}?: ${this.value.expression}`; | ||
expression = `${this.compiledKey}?: ${this.value.expression}${"default" in this.inner ? ` = ${printable(this.inner.default)}` : ""}`; | ||
} | ||
@@ -40,0 +40,0 @@ export const Optional = { |
@@ -20,3 +20,4 @@ import { type Key } from "@ark/util"; | ||
} | ||
interface Inner extends Schema { | ||
interface Inner { | ||
readonly key: Key; | ||
readonly value: BaseRoot; | ||
@@ -23,0 +24,0 @@ } |
@@ -14,3 +14,3 @@ import { type array, type Key, type listable } from "@ark/util"; | ||
import type { Index } from "./index.ts"; | ||
import type { Optional } from "./optional.ts"; | ||
import { Optional } from "./optional.ts"; | ||
import type { Prop } from "./prop.ts"; | ||
@@ -57,9 +57,8 @@ import type { Required } from "./required.ts"; | ||
expression: string; | ||
requiredLiteralKeys: Key[]; | ||
optionalLiteralKeys: Key[]; | ||
requiredKeys: Key[]; | ||
optionalKeys: Key[]; | ||
literalKeys: Key[]; | ||
entries: array<NodeEntry>; | ||
_keyof: BaseRoot | undefined; | ||
keyof(): BaseRoot; | ||
map(flatMapEntry: NodeEntryFlatMapper): StructureNode; | ||
map(flatMapProp: PropFlatMapper): StructureNode; | ||
assertHasKeys(keys: array<KeyOrKeyNode>): void; | ||
@@ -82,13 +81,10 @@ get(indexer: GettableKeyOrNode, ...path: array<GettableKeyOrNode>): BaseRoot; | ||
} | ||
export type NodeEntryFlatMapper = (entry: NodeEntry) => listable<MappedNodeEntry>; | ||
export type NodeEntry = readonly [ | ||
key: Key, | ||
value: BaseRoot, | ||
kind: "required" | "optional" | ||
]; | ||
export type MappedNodeEntry = readonly [ | ||
key: Key, | ||
value: BaseRoot, | ||
kind?: "required" | "optional" | ||
]; | ||
export type PropFlatMapper = (entry: Prop.Node) => listable<MappedPropInner>; | ||
export type MappedPropInner = BaseMappedPropInner | OptionalMappedPropInner; | ||
export interface BaseMappedPropInner extends Required.Schema { | ||
kind?: "required" | "optional"; | ||
} | ||
export interface OptionalMappedPropInner extends Optional.Schema { | ||
kind: "optional"; | ||
} | ||
export declare const Structure: { | ||
@@ -95,0 +91,0 @@ implementation: nodeImplementationOf<Structure.Declaration>; |
@@ -9,3 +9,4 @@ import { append, conflatenate, flatMorph, printable, spliterate, throwParseError } from "@ark/util"; | ||
import { $ark, registeredReference } from "../shared/registry.js"; | ||
import { hasArkKind, makeRootAndArrayPropertiesMutable } from "../shared/utils.js"; | ||
import { hasArkKind, isNode, makeRootAndArrayPropertiesMutable } from "../shared/utils.js"; | ||
import { Optional } from "./optional.js"; | ||
import { arrayIndexMatcherReference } from "./shared.js"; | ||
@@ -69,3 +70,3 @@ const createStructuralWriter = (childStringProp) => (node) => { | ||
const lKey = l.keyof(); | ||
const disjointRKeys = r.requiredLiteralKeys.filter(k => !lKey.allows(k)); | ||
const disjointRKeys = r.requiredKeys.filter(k => !lKey.allows(k)); | ||
if (disjointRKeys.length) { | ||
@@ -103,3 +104,3 @@ return new Disjoint(...disjointRKeys.map(k => ({ | ||
const rKey = r.keyof(); | ||
const disjointLKeys = l.requiredLiteralKeys.filter(k => !rKey.allows(k)); | ||
const disjointLKeys = l.requiredKeys.filter(k => !rKey.allows(k)); | ||
if (disjointLKeys.length) { | ||
@@ -160,9 +161,5 @@ return new Disjoint(...disjointLKeys.map(k => ({ | ||
expression = structuralExpression(this); | ||
requiredLiteralKeys = this.required?.map(node => node.key) ?? []; | ||
optionalLiteralKeys = this.optional?.map(node => node.key) ?? []; | ||
literalKeys = [ | ||
...this.requiredLiteralKeys, | ||
...this.optionalLiteralKeys | ||
]; | ||
entries = this.props.map(entry => [entry.key, entry.value, entry.kind]); | ||
requiredKeys = this.required?.map(node => node.key) ?? []; | ||
optionalKeys = this.optional?.map(node => node.key) ?? []; | ||
literalKeys = [...this.requiredKeys, ...this.optionalKeys]; | ||
_keyof; | ||
@@ -178,13 +175,21 @@ keyof() { | ||
} | ||
map(flatMapEntry) { | ||
const inner = {}; | ||
this.entries.forEach(entry => { | ||
const result = flatMapEntry(entry); | ||
// based on flatMorph from @ark/util | ||
if (Array.isArray(result[0]) || result.length === 0) { | ||
return result.forEach(entry => reduceMappedEntry(this, inner, entry)); | ||
map(flatMapProp) { | ||
return this.$.node("structure", this.props | ||
.flatMap(flatMapProp) | ||
.reduce((structureInner, mapped) => { | ||
const originalProp = this.propsByKey[mapped.key]; | ||
if (isNode(mapped)) { | ||
if (mapped.kind !== "required" && mapped.kind !== "optional") { | ||
return throwParseError(`Map result must have kind "required" or "optional" (was ${mapped.kind})`); | ||
} | ||
structureInner[mapped.kind] = append(structureInner[mapped.kind], mapped); | ||
return structureInner; | ||
} | ||
reduceMappedEntry(this, inner, result); | ||
}); | ||
return this.$.node("structure", inner); | ||
const mappedKind = mapped.kind ?? originalProp?.kind ?? "required"; | ||
// extract the inner keys from the map result in case a node was spread, | ||
// which would otherwise lead to invalid keys | ||
const mappedPropInner = flatMorph(mapped, (k, v) => (k in Optional.implementation.keys ? [k, v] : [])); | ||
structureInner[mappedKind] = append(structureInner[mappedKind], this.$.node(mappedKind, mappedPropInner)); | ||
return structureInner; | ||
}, {})); | ||
} | ||
@@ -455,4 +460,4 @@ assertHasKeys(keys) { | ||
}); | ||
if (this.requiredLiteralKeys.length) | ||
schema.required = this.requiredLiteralKeys; | ||
if (this.requiredKeys.length) | ||
schema.required = this.requiredKeys; | ||
} | ||
@@ -480,14 +485,2 @@ this.index?.forEach(index => { | ||
} | ||
const reduceMappedEntry = (original, inner, [k, v, kind]) => { | ||
const originalProp = original.propsByKey[k]; | ||
const mappedKind = kind ?? originalProp?.kind ?? "required"; | ||
return (inner[mappedKind] = append(inner[mappedKind], (originalProp && | ||
mappedKind === originalProp.kind && | ||
v === originalProp.value) ? | ||
originalProp | ||
: original.$.node(mappedKind, { | ||
key: k, | ||
value: v | ||
}))); | ||
}; | ||
export const Structure = { | ||
@@ -494,0 +487,0 @@ implementation, |
{ | ||
"name": "@arktype/schema", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"license": "MIT", | ||
@@ -32,3 +32,3 @@ "author": { | ||
"dependencies": { | ||
"@ark/util": "0.10.0" | ||
"@ark/util": "0.11.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "publishConfig": { |
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
340074
7966
+ Added@ark/util@0.11.0(transitive)
- Removed@ark/util@0.10.0(transitive)
Updated@ark/util@0.11.0