-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathstructure.ts
More file actions
1137 lines (980 loc) · 30.7 KB
/
Copy pathstructure.ts
File metadata and controls
1137 lines (980 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
append,
conflatenate,
flatMorph,
printable,
spliterate,
throwInternalError,
throwParseError,
type array,
type describe,
type dict,
type Key,
type listable
} from "@ark/util"
import {
BaseConstraint,
constraintKeyParser,
flattenConstraints,
intersectConstraints
} from "../constraint.ts"
import { intrinsic } from "../intrinsic.ts"
import type { nodeOfKind } from "../kinds.ts"
import type { GettableKeyOrNode, KeyOrKeyNode } from "../node.ts"
import type { Morph } from "../roots/morph.ts"
import { typeOrTermExtends, type BaseRoot } from "../roots/root.ts"
import type { BaseScope } from "../scope.ts"
import { compileSerializedValue, type NodeCompiler } from "../shared/compile.ts"
import type {
attachmentsOf,
BaseNormalizedSchema,
declareNode
} from "../shared/declare.ts"
import { Disjoint } from "../shared/disjoint.ts"
import {
implementNode,
type nodeImplementationOf,
type StructuralKind
} from "../shared/implement.ts"
import { intersectNodesRoot } from "../shared/intersections.ts"
import type { JsonSchema } from "../shared/jsonSchema.ts"
import {
$ark,
registeredReference,
type RegisteredReference
} from "../shared/registry.ts"
import { ToJsonSchema } from "../shared/toJsonSchema.ts"
import {
traverseKey,
type InternalTraversal,
type TraversalKind,
type TraverseAllows,
type TraverseApply
} from "../shared/traversal.ts"
import {
hasArkKind,
isNode,
makeRootAndArrayPropertiesMutable
} from "../shared/utils.ts"
import type { Index } from "./index.ts"
import { Optional, type OptionalNode } from "./optional.ts"
import type { Prop } from "./prop.ts"
import type { Required, RequiredNode } from "./required.ts"
import type { Sequence } from "./sequence.ts"
/**
* - `"ignore"` (default) - allow and preserve extra properties
* - `"reject"` - disallow extra properties
* - `"delete"` - clone and remove extra properties from output
*/
export type UndeclaredKeyBehavior = "ignore" | UndeclaredKeyHandling
export type UndeclaredKeyHandling = "reject" | "delete"
export declare namespace Structure {
export interface Schema extends BaseNormalizedSchema {
readonly optional?: readonly Optional.Schema[]
readonly required?: readonly Required.Schema[]
readonly index?: readonly Index.Schema[]
readonly sequence?: Sequence.Schema
readonly undeclared?: UndeclaredKeyBehavior
}
export interface Inner {
readonly optional?: readonly Optional.Node[]
readonly required?: readonly Required.Node[]
readonly index?: readonly Index.Node[]
readonly sequence?: Sequence.Node
readonly undeclared?: UndeclaredKeyHandling
}
export namespace Inner {
export type mutable = makeRootAndArrayPropertiesMutable<Inner>
}
export interface Declaration
extends declareNode<{
kind: "structure"
schema: Schema
normalizedSchema: Schema
inner: Inner
prerequisite: object
childKind: StructuralKind
}> {}
export type Node = StructureNode
}
const createStructuralWriter =
(childStringProp: "expression" | "description") => (node: StructureNode) => {
if (node.props.length || node.index) {
const parts = node.index?.map(index => index[childStringProp]) ?? []
for (const prop of node.props) parts.push(prop[childStringProp])
if (node.undeclared) parts.push(`+ (undeclared): ${node.undeclared}`)
const objectLiteralDescription = `{ ${parts.join(", ")} }`
return node.sequence ?
`${objectLiteralDescription} & ${node.sequence.description}`
: objectLiteralDescription
}
return node.sequence?.description ?? "{}"
}
const structuralDescription = createStructuralWriter("description")
const structuralExpression = createStructuralWriter("expression")
const intersectPropsAndIndex = <
l extends nodeOfKind<"required"> | nodeOfKind<"optional">
>(
l: l,
r: nodeOfKind<"index">,
$: BaseScope
): l | Disjoint | null => {
const kind = l.required ? "required" : "optional"
if (!r.signature.allows(l.key)) return null
const value = intersectNodesRoot(l.value, r.value, $)
if (value instanceof Disjoint) {
return kind === "optional" ?
($.node("optional", {
key: l.key,
value: $ark.intrinsic.never.internal
}) as l)
: value.withPrefixKey(l.key, l.kind)
}
return null
}
const implementation: nodeImplementationOf<Structure.Declaration> =
implementNode<Structure.Declaration>({
kind: "structure",
hasAssociatedError: false,
normalize: schema => schema,
applyConfig: (schema, config) => {
if (!schema.undeclared && config.onUndeclaredKey !== "ignore") {
return {
...schema,
undeclared: config.onUndeclaredKey
}
}
return schema
},
keys: {
required: {
child: true,
parse: constraintKeyParser("required"),
reduceIo: (ioKind, inner, nodes) => {
// ensure we don't overwrite nodes added by optional
inner.required = append(
inner.required,
nodes!.map(
node =>
(ioKind === "in" ? node.rawIn : node.rawOut) as RequiredNode
)
)
return
}
},
optional: {
child: true,
parse: constraintKeyParser("optional"),
reduceIo: (ioKind, inner, nodes) => {
if (ioKind === "in") {
inner.optional = nodes!.map(node => node.rawIn as OptionalNode)
return
}
for (const node of nodes!) {
inner[node.outProp.kind] = append(
inner[node.outProp.kind],
node.outProp.rawOut as Prop.Node
) as never
}
}
},
index: {
child: true,
parse: constraintKeyParser("index")
},
sequence: {
child: true,
parse: constraintKeyParser("sequence")
},
undeclared: {
parse: behavior => (behavior === "ignore" ? undefined : behavior),
reduceIo: (ioKind, inner, value) => {
if (value === "reject") {
inner.undeclared = "reject"
return
}
// if base is "delete", undeclared keys are "ignore" (i.e. unconstrained)
// on input and "reject" on output
if (ioKind === "in") delete inner.undeclared
else inner.undeclared = "reject"
}
}
},
defaults: {
description: structuralDescription
},
intersections: {
structure: (l, r, ctx) => {
const lInner = { ...l.inner }
const rInner = { ...r.inner }
const disjointResult = new Disjoint()
if (l.undeclared) {
const lKey = l.keyof()
for (const k of r.requiredKeys) {
if (!lKey.allows(k)) {
disjointResult.add(
"presence",
$ark.intrinsic.never.internal,
r.propsByKey[k]!.value,
{
path: [k]
}
)
}
}
if (rInner.optional)
rInner.optional = rInner.optional.filter(n => lKey.allows(n.key))
if (rInner.index) {
rInner.index = rInner.index.flatMap(n => {
if (n.signature.extends(lKey)) return n
const indexOverlap = intersectNodesRoot(lKey, n.signature, ctx.$)
if (indexOverlap instanceof Disjoint) return []
const normalized = normalizeIndex(indexOverlap, n.value, ctx.$)
if (normalized.required) {
rInner.required = conflatenate(
rInner.required,
normalized.required
)
}
if (normalized.optional) {
rInner.optional = conflatenate(
rInner.optional,
normalized.optional
)
}
return normalized.index ?? []
})
}
}
if (r.undeclared) {
const rKey = r.keyof()
for (const k of l.requiredKeys) {
if (!rKey.allows(k)) {
disjointResult.add(
"presence",
l.propsByKey[k]!.value,
$ark.intrinsic.never.internal,
{
path: [k]
}
)
}
}
if (lInner.optional)
lInner.optional = lInner.optional.filter(n => rKey.allows(n.key))
if (lInner.index) {
lInner.index = lInner.index.flatMap(n => {
if (n.signature.extends(rKey)) return n
const indexOverlap = intersectNodesRoot(rKey, n.signature, ctx.$)
if (indexOverlap instanceof Disjoint) return []
const normalized = normalizeIndex(indexOverlap, n.value, ctx.$)
if (normalized.required) {
lInner.required = conflatenate(
lInner.required,
normalized.required
)
}
if (normalized.optional) {
lInner.optional = conflatenate(
lInner.optional,
normalized.optional
)
}
return normalized.index ?? []
})
}
}
const baseInner: Structure.Inner.mutable = {}
if (l.undeclared || r.undeclared) {
baseInner.undeclared =
l.undeclared === "reject" || r.undeclared === "reject" ?
"reject"
: "delete"
}
const childIntersectionResult = intersectConstraints({
kind: "structure",
baseInner,
l: flattenConstraints(lInner),
r: flattenConstraints(rInner),
roots: [],
ctx
})
if (childIntersectionResult instanceof Disjoint)
disjointResult.push(...childIntersectionResult)
if (disjointResult.length) return disjointResult
return childIntersectionResult
}
},
reduce: (inner, $) => {
if (!inner.required && !inner.optional) return
const seen: Record<Key, true | undefined> = {}
let updated = false
const newOptionalProps: OptionalNode[] =
inner.optional ? [...inner.optional] : []
// check required keys for duplicates and handle index intersections
if (inner.required) {
for (let i = 0; i < inner.required.length; i++) {
const requiredProp = inner.required[i]
if (requiredProp.key in seen)
throwParseError(writeDuplicateKeyMessage(requiredProp.key))
seen[requiredProp.key] = true
if (inner.index) {
for (const index of inner.index) {
const intersection = intersectPropsAndIndex(
requiredProp,
index,
$
)
if (intersection instanceof Disjoint) return intersection
}
}
}
}
// check optional keys for duplicates and handle index intersections
if (inner.optional) {
for (let i = 0; i < inner.optional.length; i++) {
const optionalProp = inner.optional[i]
if (optionalProp.key in seen)
throwParseError(writeDuplicateKeyMessage(optionalProp.key))
seen[optionalProp.key] = true
if (inner.index) {
for (const index of inner.index) {
const intersection = intersectPropsAndIndex(
optionalProp,
index,
$
)
if (intersection instanceof Disjoint) return intersection
if (intersection !== null) {
newOptionalProps[i] = intersection
updated = true
}
}
}
}
}
if (updated) {
return $.node(
"structure",
{ ...inner, optional: newOptionalProps },
{ prereduced: true }
)
}
}
})
export class StructureNode extends BaseConstraint<Structure.Declaration> {
impliedBasis: BaseRoot = $ark.intrinsic.object.internal
impliedSiblings = this.children.flatMap(
n => (n.impliedSiblings as BaseConstraint[]) ?? []
)
props: array<Prop.Node> = conflatenate<Prop.Node>(
this.required,
this.optional
)
propsByKey: Record<Key, Prop.Node | undefined> = flatMorph(
this.props,
(i, node) => [node.key, node] as const
)
propsByKeyReference: RegisteredReference = registeredReference(
this.propsByKey
)
expression: string = structuralExpression(this)
requiredKeys: Key[] = this.required?.map(node => node.key) ?? []
optionalKeys: Key[] = this.optional?.map(node => node.key) ?? []
literalKeys: Key[] = [...this.requiredKeys, ...this.optionalKeys]
_keyof: BaseRoot | undefined
keyof(): BaseRoot {
if (this._keyof) return this._keyof
let branches = this.$.units(this.literalKeys).branches
if (this.index) {
for (const { signature } of this.index)
branches = branches.concat(signature.branches)
}
return (this._keyof = this.$.node("union", branches))
}
map(flatMapProp: PropFlatMapper): StructureNode {
return this.$.node(
"structure",
this.props
.flatMap(flatMapProp)
.reduce((structureInner: Structure.Inner.mutable, 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] as any,
mapped
)
return structureInner
}
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: Prop.Inner = flatMorph(
mapped as BaseMappedPropInner,
(k, v) => (k in Optional.implementation.keys ? [k, v] : [])
) as never
structureInner[mappedKind] = append(
structureInner[mappedKind] as any,
this.$.node(mappedKind, mappedPropInner)
)
return structureInner
}, {})
)
}
assertHasKeys(keys: array<KeyOrKeyNode>): void {
const invalidKeys = keys.filter(k => !typeOrTermExtends(k, this.keyof()))
if (invalidKeys.length) {
return throwParseError(
writeInvalidKeysMessage(this.expression, invalidKeys)
)
}
}
get(indexer: GettableKeyOrNode, ...path: array<GettableKeyOrNode>): BaseRoot {
let value: BaseRoot | undefined
let required = false
const key = indexerToKey(indexer)
if (
(typeof key === "string" || typeof key === "symbol") &&
this.propsByKey[key]
) {
value = this.propsByKey[key]!.value
required = this.propsByKey[key]!.required
}
if (this.index) {
for (const n of this.index) {
if (typeOrTermExtends(key, n.signature))
value = value?.and(n.value) ?? n.value
}
}
if (
this.sequence &&
typeOrTermExtends(key, $ark.intrinsic.nonNegativeIntegerString)
) {
if (hasArkKind(key, "root")) {
if (this.sequence.variadic)
// if there is a variadic element and we're accessing an index, return a union
// of all possible elements. If there is no variadic expression, we're in a tuple
// so this access wouldn't be safe based on the array indices
value = value?.and(this.sequence.element) ?? this.sequence.element
} else {
const index = Number.parseInt(key as string)
if (index < this.sequence.prevariadic.length) {
const fixedElement = this.sequence.prevariadic[index].node
value = value?.and(fixedElement) ?? fixedElement
required ||= index < this.sequence.prefixLength
} else if (this.sequence.variadic) {
// ideally we could return something more specific for postfix
// but there is no way to represent it using an index alone
const nonFixedElement = this.$.node(
"union",
this.sequence.variadicOrPostfix
)
value = value?.and(nonFixedElement) ?? nonFixedElement
}
}
}
if (!value) {
if (
this.sequence?.variadic &&
hasArkKind(key, "root") &&
key.extends($ark.intrinsic.number)
) {
return throwParseError(
writeNumberIndexMessage(key.expression, this.sequence.expression)
)
}
return throwParseError(writeInvalidKeysMessage(this.expression, [key]))
}
const result = value.get(...path)
return required ? result : result.or($ark.intrinsic.undefined)
}
pick(...keys: KeyOrKeyNode[]): StructureNode {
this.assertHasKeys(keys)
return this.$.node("structure", this.filterKeys("pick", keys))
}
omit(...keys: KeyOrKeyNode[]): StructureNode {
this.assertHasKeys(keys)
return this.$.node("structure", this.filterKeys("omit", keys))
}
optionalize(): StructureNode {
const { required: _, sequence, ...inner } = this.inner
return this.$.node("structure", {
...inner,
...(sequence ? { sequence: sequence.optionalize() } : {}),
optional: this.props.map(prop =>
prop.hasKind("required") ? this.$.node("optional", prop.inner) : prop
)
})
}
require(): StructureNode {
const { optional: _, sequence, ...inner } = this.inner
return this.$.node("structure", {
...inner,
...(sequence ? { sequence: sequence.require() } : {}),
required: this.props.map(prop =>
prop.hasKind("optional") ?
{
key: prop.key,
value: prop.value
}
: prop
)
})
}
merge(r: StructureNode): StructureNode {
const inner = this.filterKeys("omit", [r.keyof()])
if (r.required) inner.required = append(inner.required, r.required)
if (r.optional) inner.optional = append(inner.optional, r.optional)
if (r.index) inner.index = append(inner.index, r.index)
if (r.sequence) inner.sequence = r.sequence
if (r.undeclared) inner.undeclared = r.undeclared
else delete inner.undeclared
return this.$.node("structure", inner)
}
private filterKeys(
operation: "pick" | "omit",
keys: array<BaseRoot | Key>
): Structure.Inner.mutable {
const result = makeRootAndArrayPropertiesMutable(this.inner)
const shouldKeep = (key: KeyOrKeyNode) => {
const matchesKey = keys.some(k => typeOrTermExtends(key, k))
return operation === "pick" ? matchesKey : !matchesKey
}
if (result.required)
result.required = result.required.filter(prop => shouldKeep(prop.key))
if (result.optional)
result.optional = result.optional.filter(prop => shouldKeep(prop.key))
if (result.index)
result.index = result.index.filter(index => shouldKeep(index.signature))
return result
}
traverseAllows: TraverseAllows<object> = (data, ctx) =>
this._traverse("Allows", data, ctx)
traverseApply: TraverseApply<object> = (data, ctx) =>
this._traverse("Apply", data, ctx)
protected _traverse = (
traversalKind: TraversalKind,
data: object,
ctx: InternalTraversal
): boolean => {
const errorCount = ctx?.currentErrorCount ?? 0
for (let i = 0; i < this.props.length; i++) {
if (traversalKind === "Allows") {
if (!this.props[i].traverseAllows(data, ctx)) return false
} else {
this.props[i].traverseApply(data as never, ctx)
if (ctx.failFast && ctx.currentErrorCount > errorCount) return false
}
}
if (this.sequence) {
if (traversalKind === "Allows") {
if (!this.sequence.traverseAllows(data as never, ctx)) return false
} else {
this.sequence.traverseApply(data as never, ctx)
if (ctx.failFast && ctx.currentErrorCount > errorCount) return false
}
}
if (this.index || this.undeclared === "reject") {
const keys: Key[] = Object.keys(data)
keys.push(...Object.getOwnPropertySymbols(data))
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
if (this.index) {
for (const node of this.index) {
if (node.signature.traverseAllows(k, ctx)) {
if (traversalKind === "Allows") {
const result = traverseKey(
k,
() => node.value.traverseAllows(data[k as never], ctx),
ctx
)
if (!result) return false
} else {
traverseKey(
k,
() => node.value.traverseApply(data[k as never], ctx),
ctx
)
if (ctx.failFast && ctx.currentErrorCount > errorCount)
return false
}
}
}
}
if (this.undeclared === "reject" && !this.declaresKey(k)) {
if (traversalKind === "Allows") return false
// this should have its own error code:
// https://github.com/arktypeio/arktype/issues/1403
ctx.errorFromNodeContext({
code: "predicate",
expected: "removed",
actual: "",
relativePath: [k],
meta: this.meta
})
if (ctx.failFast) return false
}
}
}
// added additional ctx check here to address
// https://github.com/arktypeio/arktype/issues/1346
if (this.structuralMorph && ctx && !ctx.hasError())
ctx.queueMorphs([this.structuralMorph])
return true
}
get defaultable(): Optional.Node.withDefault[] {
return this.cacheGetter(
"defaultable",
this.optional?.filter(o => o.hasDefault()) ?? []
)
}
declaresKey = (k: Key): boolean =>
k in this.propsByKey ||
this.index?.some(n => n.signature.allows(k)) ||
(this.sequence !== undefined &&
$ark.intrinsic.nonNegativeIntegerString.allows(k))
_compileDeclaresKey(js: NodeCompiler): string {
const parts: string[] = []
if (this.props.length) parts.push(`k in ${this.propsByKeyReference}`)
if (this.index) {
for (const index of this.index)
parts.push(js.invoke(index.signature, { kind: "Allows", arg: "k" }))
}
if (this.sequence)
parts.push("$ark.intrinsic.nonNegativeIntegerString.allows(k)")
// if parts is empty, this is a structure like { "+": "reject" }
// that declares no keys, so return false
return parts.join(" || ") || "false"
}
get structuralMorph(): Morph | undefined {
return this.cacheGetter("structuralMorph", getPossibleMorph(this))
}
structuralMorphRef: RegisteredReference | undefined =
this.structuralMorph && registeredReference(this.structuralMorph)
compile(js: NodeCompiler): unknown {
if (js.traversalKind === "Apply") js.initializeErrorCount()
for (const prop of this.props) {
js.check(prop)
if (js.traversalKind === "Apply") js.returnIfFailFast()
}
if (this.sequence) {
js.check(this.sequence)
if (js.traversalKind === "Apply") js.returnIfFailFast()
}
if (this.index || this.undeclared === "reject") {
js.const("keys", "Object.keys(data)")
js.line("keys.push(...Object.getOwnPropertySymbols(data))")
js.for("i < keys.length", () => this.compileExhaustiveEntry(js))
}
if (js.traversalKind === "Allows") return js.return(true)
// always queue deleteUndeclared on valid traversal for "delete"
if (this.structuralMorphRef) {
// added additional ctx check here to address
// https://github.com/arktypeio/arktype/issues/1346
js.if("ctx && !ctx.hasError()", () => {
js.line(`ctx.queueMorphs([`)
precompileMorphs(js, this)
return js.line("])")
})
}
}
protected compileExhaustiveEntry(js: NodeCompiler): NodeCompiler {
js.const("k", "keys[i]")
if (this.index) {
for (const node of this.index) {
js.if(
`${js.invoke(node.signature, { arg: "k", kind: "Allows" })}`,
() => js.traverseKey("k", "data[k]", node.value)
)
}
}
if (this.undeclared === "reject") {
js.if(`!(${this._compileDeclaresKey(js)})`, () => {
if (js.traversalKind === "Allows") return js.return(false)
return js
.line(
`ctx.errorFromNodeContext({ code: "predicate", expected: "removed", actual: "", relativePath: [k], meta: ${this.compiledMeta} })`
)
.if("ctx.failFast", () => js.return())
})
}
return js
}
reduceJsonSchema(
schema: JsonSchema.Structure,
ctx: ToJsonSchema.Context
): JsonSchema.Structure {
switch (schema.type) {
case "object":
return this.reduceObjectJsonSchema(schema, ctx)
case "array":
const arraySchema =
this.sequence?.reduceJsonSchema(schema, ctx) ?? schema
if (this.props.length || this.index) {
return ctx.fallback.arrayObject({
code: "arrayObject",
base: arraySchema,
object: this.reduceObjectJsonSchema({ type: "object" }, ctx)
})
}
return arraySchema
default:
return ToJsonSchema.throwInternalOperandError("structure", schema)
}
}
reduceObjectJsonSchema(
schema: JsonSchema.Object,
ctx: ToJsonSchema.Context
): JsonSchema.Object {
if (this.props.length) {
schema.properties = {}
for (const prop of this.props) {
const valueSchema = prop.value.toJsonSchemaRecurse(ctx)
if (typeof prop.key === "symbol") {
ctx.fallback.symbolKey({
code: "symbolKey",
base: schema,
key: prop.key,
value: valueSchema,
optional: prop.optional
})
continue
}
if (prop.hasDefault()) {
const value =
typeof prop.default === "function" ? prop.default() : prop.default
valueSchema.default =
$ark.intrinsic.jsonData.allows(value) ?
value
: ctx.fallback.defaultValue({
code: "defaultValue",
base: valueSchema,
value
})
}
schema.properties![prop.key] = valueSchema
}
if (this.requiredKeys.length && schema.properties) {
schema.required = this.requiredKeys.filter(
(k): k is string => typeof k === "string" && k in schema.properties!
)
}
}
if (this.index) {
for (const index of this.index) {
const valueJsonSchema = index.value.toJsonSchemaRecurse(ctx)
if (index.signature.equals($ark.intrinsic.string)) {
schema.additionalProperties = valueJsonSchema
continue
}
for (const keyBranch of index.signature.branches) {
if (!keyBranch.extends($ark.intrinsic.string)) {
schema = ctx.fallback.symbolKey({
code: "symbolKey",
base: schema,
key: null,
value: valueJsonSchema,
optional: false
})
continue
}
let keySchema: JsonSchema.String = { type: "string" }
if (keyBranch.hasKind("morph")) {
keySchema = ctx.fallback.morph({
code: "morph",
base: keyBranch.rawIn.toJsonSchemaRecurse(ctx),
out: keyBranch.rawOut.toJsonSchemaRecurse(ctx)
}) as never
}
if (!keyBranch.hasKind("intersection")) {
return throwInternalError(
`Unexpected index branch kind ${keyBranch.kind}.`
)
}
const { pattern } = keyBranch.inner
if (pattern) {
const keySchemaWithPattern = Object.assign(keySchema, {
pattern: pattern[0].rule
})
for (let i = 1; i < pattern.length; i++) {
keySchema = ctx.fallback.patternIntersection({
code: "patternIntersection",
base: keySchemaWithPattern,
pattern: pattern[i].rule
})
}
schema.patternProperties ??= {}
schema.patternProperties[keySchemaWithPattern.pattern] =
valueJsonSchema
}
}
}
}
if (this.undeclared && !schema.additionalProperties)
schema.additionalProperties = false
return schema
}
}
const defaultableMorphsCache: Record<string, Morph | undefined> = {}
type PartiallyInitializedStructure = attachmentsOf<Structure.Declaration> &
Pick<Structure.Node, "defaultable" | "declaresKey">
const constructStructuralMorphCacheKey = (
node: PartiallyInitializedStructure
): string => {
let cacheKey = ""
for (let i = 0; i < node.defaultable.length; i++)
cacheKey += node.defaultable[i].defaultValueMorphRef
if (node.sequence?.defaultValueMorphsReference)
cacheKey += node.sequence?.defaultValueMorphsReference
if (node.undeclared === "delete") {
cacheKey += "delete !("
if (node.required)
for (const n of node.required) cacheKey += n.compiledKey + " | "
if (node.optional)
for (const n of node.optional) cacheKey += n.compiledKey + " | "
if (node.index)
for (const index of node.index) cacheKey += index.signature.id + " | "
if (node.sequence) {
if (node.sequence.maxLength === null)
cacheKey += intrinsic.nonNegativeIntegerString.id
else {
for (let i = 0; i < node.sequence.tuple.length; i++)
cacheKey += i + " | "
}
}
cacheKey += ")"
}
return cacheKey
}
const getPossibleMorph = (
node: PartiallyInitializedStructure
): Morph | undefined => {
const cacheKey = constructStructuralMorphCacheKey(node)
if (!cacheKey) return undefined
if (defaultableMorphsCache[cacheKey]) return defaultableMorphsCache[cacheKey]
const $arkStructuralMorph: Morph<any> = (data, ctx) => {
for (let i = 0; i < node.defaultable.length; i++) {
if (!(node.defaultable[i].key in data))
node.defaultable[i].defaultValueMorph(data as never, ctx)
}
if (node.sequence?.defaultables) {
for (
let i = data.length - node.sequence.prefixLength;
i < node.sequence.defaultables.length;
i++
)