@telorun/templating 0.128.0 → 0.129.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cel/analyze.d.ts +22 -0
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +191 -26
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/src/cel/analyze.ts +191 -25
- package/src/index.ts +1 -0
package/dist/cel/analyze.d.ts
CHANGED
|
@@ -87,6 +87,17 @@ export declare function findNullableAccessIssues(node: CelNode, contextSchema: R
|
|
|
87
87
|
* reach inside a `live` value type — one whose consumption has effects, so its
|
|
88
88
|
* contents exist only for a consumer that drains it.
|
|
89
89
|
* Returns null when the chain is valid or the schema is too open to judge.
|
|
90
|
+
*
|
|
91
|
+
* A node is read with its `allOf` conjuncts ({@link schemaConjuncts}), and a
|
|
92
|
+
* document-local `$ref` (`#…`) is followed against the nearest node on the walk
|
|
93
|
+
* that holds its target — the innermost document a contract embedded whole
|
|
94
|
+
* carries its reached types in. A union (`anyOf` / `oneOf`) is read in every
|
|
95
|
+
* alternative: one that cannot hold an object (a null or scalar type, a value
|
|
96
|
+
* type with no members) or a closed object not declaring the member contributes
|
|
97
|
+
* nothing, and the chain continues through every alternative declaring it. A
|
|
98
|
+
* member is unknown only when every alternative is read and none declares it
|
|
99
|
+
* nor is open; an alternative, conjunct or reference that cannot be read leaves
|
|
100
|
+
* the chain unjudged.
|
|
90
101
|
*/
|
|
91
102
|
export declare function validateChainAgainstSchema(chain: string[], schema: Record<string, any>): string | null;
|
|
92
103
|
/**
|
|
@@ -101,5 +112,16 @@ export declare function chainFailureAgainstSchema(chain: string[], schema: Recor
|
|
|
101
112
|
path: string;
|
|
102
113
|
message: string;
|
|
103
114
|
} | null;
|
|
115
|
+
/**
|
|
116
|
+
* A schema node and each of its `allOf` members, recursively — or undefined when
|
|
117
|
+
* a member cannot be read, which leaves the node unjudged. `schemaOf` reads the
|
|
118
|
+
* schema off whatever the caller carries per node (a node alone, or a node with
|
|
119
|
+
* the document its references resolve against), and `member` reads one written
|
|
120
|
+
* member in its parent's context, following its references.
|
|
121
|
+
*
|
|
122
|
+
* Shared by every walk reading a node as the conjunction it is — the CEL chain
|
|
123
|
+
* check and the analyzer's plain-literal decoding.
|
|
124
|
+
*/
|
|
125
|
+
export declare function schemaConjuncts<N>(node: N, schemaOf: (node: N) => Record<string, any>, member: (parent: N, raw: unknown) => N | undefined): N[] | undefined;
|
|
104
126
|
export {};
|
|
105
127
|
//# sourceMappingURL=analyze.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyze.d.ts","sourceRoot":"","sources":["../../src/cel/analyze.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,OAAO,EACb,MAAM,cAAc,CAAC;AAItB;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,EAAE,CAI7D;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAS5F;AA8FD;;+DAE+D;AAC/D,eAAO,MAAM,aAAa,QAAQ,CAAC;AA6BnC;qDACqD;AACrD,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC;;mDAEmD;AACnD,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAEpC;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,EAAE,CAI3D;AA4ED;+DAC+D;AAC/D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;CACrC;AAED;;;;GA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B,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjC,MAAM,GAAG,IAAI,CAIf;AA0DD;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,OAAO,EACb,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjC,aAAa,EAAE,CAIjB;AA2ED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,MAAM,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM,GAAG,IAAI,CAEf;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC/C,GAAG,CAAC,MAAM,CAAC,CAab;AAED,iFAAiF;AACjF,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,MAAM,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAiF1C;AAmDD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAC/B,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1C,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,OAAO,KAAK,CAAC,GAAG,SAAS,GACjD,CAAC,EAAE,GAAG,SAAS,CAYjB"}
|
package/dist/cel/analyze.js
CHANGED
|
@@ -295,7 +295,11 @@ function schemaIsNullable(schema) {
|
|
|
295
295
|
if (!schema || typeof schema !== "object")
|
|
296
296
|
return false;
|
|
297
297
|
const t = schema.type;
|
|
298
|
-
|
|
298
|
+
if (t === "null" || (Array.isArray(t) && t.includes("null")))
|
|
299
|
+
return true;
|
|
300
|
+
// A union admits null when one of its branches does.
|
|
301
|
+
const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : [];
|
|
302
|
+
return branches.some((branch) => schemaIsNullable(branch));
|
|
299
303
|
}
|
|
300
304
|
/** Navigate `schema` following a member chain, descending through `properties`.
|
|
301
305
|
* Returns the schema node at that path, or undefined when it can't be resolved. */
|
|
@@ -452,6 +456,17 @@ function walkNullable(node, nonNull, boundVars, issues, schema) {
|
|
|
452
456
|
* reach inside a `live` value type — one whose consumption has effects, so its
|
|
453
457
|
* contents exist only for a consumer that drains it.
|
|
454
458
|
* Returns null when the chain is valid or the schema is too open to judge.
|
|
459
|
+
*
|
|
460
|
+
* A node is read with its `allOf` conjuncts ({@link schemaConjuncts}), and a
|
|
461
|
+
* document-local `$ref` (`#…`) is followed against the nearest node on the walk
|
|
462
|
+
* that holds its target — the innermost document a contract embedded whole
|
|
463
|
+
* carries its reached types in. A union (`anyOf` / `oneOf`) is read in every
|
|
464
|
+
* alternative: one that cannot hold an object (a null or scalar type, a value
|
|
465
|
+
* type with no members) or a closed object not declaring the member contributes
|
|
466
|
+
* nothing, and the chain continues through every alternative declaring it. A
|
|
467
|
+
* member is unknown only when every alternative is read and none declares it
|
|
468
|
+
* nor is open; an alternative, conjunct or reference that cannot be read leaves
|
|
469
|
+
* the chain unjudged.
|
|
455
470
|
*/
|
|
456
471
|
export function validateChainAgainstSchema(chain, schema) {
|
|
457
472
|
return chainFailureAgainstSchema(chain, schema)?.message ?? null;
|
|
@@ -481,52 +496,202 @@ export function chainsRefusedAt(node, ranges) {
|
|
|
481
496
|
}
|
|
482
497
|
/** {@link validateChainAgainstSchema}, with the path of the read that failed. */
|
|
483
498
|
export function chainFailureAgainstSchema(chain, schema) {
|
|
484
|
-
|
|
499
|
+
const walked = [];
|
|
500
|
+
let current = [schema];
|
|
485
501
|
for (let i = 0; i < chain.length; i++) {
|
|
486
502
|
const key = chain[i];
|
|
487
|
-
|
|
503
|
+
const alternatives = alternativesOf(current, walked, key === INDEX_SEGMENT ? holdsCollection : holdsObject);
|
|
504
|
+
if (!alternatives || alternatives.length === 0)
|
|
488
505
|
return null;
|
|
506
|
+
walked.push(...alternatives);
|
|
489
507
|
// An index reads an ELEMENT: an array's `items`, or a map's value schema. What
|
|
490
508
|
// is below it is checked like any member access, which is what reaches a
|
|
491
509
|
// typo inside a list of records (`items[0].amont`). An object that also
|
|
492
510
|
// declares `properties` may be indexed by one of those names, which the
|
|
493
511
|
// chain does not record, so nothing below it is judged.
|
|
494
512
|
if (key === INDEX_SEGMENT) {
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
513
|
+
const elements = [];
|
|
514
|
+
for (const alternative of alternatives) {
|
|
515
|
+
const element = alternative.items && typeof alternative.items === "object" && !Array.isArray(alternative.items)
|
|
516
|
+
? alternative.items
|
|
517
|
+
: !alternative.properties &&
|
|
518
|
+
alternative.additionalProperties &&
|
|
519
|
+
typeof alternative.additionalProperties === "object"
|
|
520
|
+
? alternative.additionalProperties
|
|
521
|
+
: undefined;
|
|
522
|
+
if (!element)
|
|
523
|
+
return null;
|
|
524
|
+
elements.push(element);
|
|
525
|
+
}
|
|
526
|
+
current = elements;
|
|
505
527
|
continue;
|
|
506
528
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
529
|
+
const found = [];
|
|
530
|
+
let declares = false;
|
|
531
|
+
const available = [];
|
|
532
|
+
for (const alternative of alternatives) {
|
|
533
|
+
const conjuncts = schemaConjuncts(alternative, (node) => node, (_, raw) => followLocalRef(raw, walked));
|
|
534
|
+
if (!conjuncts)
|
|
535
|
+
return null;
|
|
536
|
+
let declared;
|
|
537
|
+
let groupDeclares = false;
|
|
538
|
+
for (const conjunct of conjuncts) {
|
|
539
|
+
// A value type declaring members is read by those members alone: they are
|
|
540
|
+
// its fields, closed as the CEL type the type registers.
|
|
541
|
+
const members = membersOf(conjunct);
|
|
542
|
+
const props = members ?? conjunct.properties;
|
|
543
|
+
if (!props)
|
|
544
|
+
continue;
|
|
545
|
+
groupDeclares = true;
|
|
546
|
+
if (key in props) {
|
|
547
|
+
declared = props[key];
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
if (!members && conjunct.additionalProperties === true)
|
|
551
|
+
return null;
|
|
552
|
+
for (const name of Object.keys(props))
|
|
553
|
+
if (!available.includes(name))
|
|
554
|
+
available.push(name);
|
|
555
|
+
}
|
|
556
|
+
// An alternative saying nothing about its members could hold this one.
|
|
557
|
+
if (!groupDeclares)
|
|
558
|
+
return null;
|
|
559
|
+
declares = true;
|
|
560
|
+
if (declared !== undefined)
|
|
561
|
+
found.push(declared);
|
|
562
|
+
}
|
|
563
|
+
if (found.length > 0) {
|
|
564
|
+
if (i < chain.length - 1 && found.some((node) => isLiveSlot(node))) {
|
|
516
565
|
const path = chain.slice(0, i + 1).join(".");
|
|
517
566
|
return {
|
|
518
567
|
path,
|
|
519
568
|
message: `'${path}' yields a stream — pipe it through an Encoder or iterate in a JS.Script step (no member access on stream-typed values)`,
|
|
520
569
|
};
|
|
521
570
|
}
|
|
522
|
-
current =
|
|
571
|
+
current = found;
|
|
523
572
|
continue;
|
|
524
573
|
}
|
|
525
|
-
if (!
|
|
574
|
+
if (!declares)
|
|
526
575
|
return null;
|
|
527
576
|
const path = chain.slice(0, i + 1).join(".");
|
|
528
|
-
|
|
529
|
-
return { path, message: `'${path}' is not defined (available: ${available})` };
|
|
577
|
+
return { path, message: `'${path}' is not defined (available: ${available.join(", ")})` };
|
|
530
578
|
}
|
|
531
579
|
return null;
|
|
532
580
|
}
|
|
581
|
+
/**
|
|
582
|
+
* The alternatives a value at `nodes` may be read through: each node with its
|
|
583
|
+
* references followed and every union split into its branches, recursively,
|
|
584
|
+
* keeping only those `holds` accepts — what can hold the member or element the
|
|
585
|
+
* chain reads next. Undefined when a reference cannot be read.
|
|
586
|
+
*/
|
|
587
|
+
function alternativesOf(nodes, walked, holds) {
|
|
588
|
+
const out = [];
|
|
589
|
+
const visit = (raw, depth) => {
|
|
590
|
+
if (depth > 32)
|
|
591
|
+
return false;
|
|
592
|
+
const node = followLocalRef(raw, [...walked, ...out]);
|
|
593
|
+
if (!node || typeof node !== "object")
|
|
594
|
+
return false;
|
|
595
|
+
const branches = Array.isArray(node.anyOf) ? node.anyOf : Array.isArray(node.oneOf) ? node.oneOf : undefined;
|
|
596
|
+
if (branches && !node.properties && !node.allOf) {
|
|
597
|
+
for (const branch of branches) {
|
|
598
|
+
if (!branch || typeof branch !== "object" || !visit(branch, depth + 1))
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
return true;
|
|
602
|
+
}
|
|
603
|
+
if (holds(node))
|
|
604
|
+
out.push(node);
|
|
605
|
+
return true;
|
|
606
|
+
};
|
|
607
|
+
for (const node of nodes)
|
|
608
|
+
if (!visit(node, 0))
|
|
609
|
+
return undefined;
|
|
610
|
+
return out;
|
|
611
|
+
}
|
|
612
|
+
/** Whether a value at `node` can be a list or a map a chain indexes into. */
|
|
613
|
+
function holdsCollection(node) {
|
|
614
|
+
if (node["x-telo-type"] !== undefined)
|
|
615
|
+
return false;
|
|
616
|
+
const types = Array.isArray(node.type) ? node.type : node.type === undefined ? undefined : [node.type];
|
|
617
|
+
return !types || types.includes("array") || types.includes("object");
|
|
618
|
+
}
|
|
619
|
+
/** Whether a value at `node` can be an object whose members a chain reads: a
|
|
620
|
+
* node typed as no object (null, a scalar, a list) or a value type with no
|
|
621
|
+
* members cannot. */
|
|
622
|
+
function holdsObject(node) {
|
|
623
|
+
if (membersOf(node))
|
|
624
|
+
return true;
|
|
625
|
+
if (node["x-telo-type"] !== undefined)
|
|
626
|
+
return false;
|
|
627
|
+
const types = Array.isArray(node.type) ? node.type : node.type === undefined ? undefined : [node.type];
|
|
628
|
+
if (types && !types.includes("object"))
|
|
629
|
+
return false;
|
|
630
|
+
if ("const" in node && (node.const === null || typeof node.const !== "object"))
|
|
631
|
+
return false;
|
|
632
|
+
return true;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* A schema node and each of its `allOf` members, recursively — or undefined when
|
|
636
|
+
* a member cannot be read, which leaves the node unjudged. `schemaOf` reads the
|
|
637
|
+
* schema off whatever the caller carries per node (a node alone, or a node with
|
|
638
|
+
* the document its references resolve against), and `member` reads one written
|
|
639
|
+
* member in its parent's context, following its references.
|
|
640
|
+
*
|
|
641
|
+
* Shared by every walk reading a node as the conjunction it is — the CEL chain
|
|
642
|
+
* check and the analyzer's plain-literal decoding.
|
|
643
|
+
*/
|
|
644
|
+
export function schemaConjuncts(node, schemaOf, member) {
|
|
645
|
+
const out = [node];
|
|
646
|
+
const all = schemaOf(node).allOf;
|
|
647
|
+
if (!Array.isArray(all))
|
|
648
|
+
return out;
|
|
649
|
+
for (const raw of all) {
|
|
650
|
+
const read = member(node, raw);
|
|
651
|
+
if (read === undefined)
|
|
652
|
+
return undefined;
|
|
653
|
+
const nested = schemaConjuncts(read, schemaOf, member);
|
|
654
|
+
if (!nested)
|
|
655
|
+
return undefined;
|
|
656
|
+
out.push(...nested);
|
|
657
|
+
}
|
|
658
|
+
return out;
|
|
659
|
+
}
|
|
660
|
+
/** `node` with every document-local `$ref` it is followed through, resolved
|
|
661
|
+
* against the nearest node of `walked` holding the target. A reference that is
|
|
662
|
+
* not local, or whose target no walked node holds, yields undefined. */
|
|
663
|
+
function followLocalRef(node, walked) {
|
|
664
|
+
const seen = new Set();
|
|
665
|
+
let current = node;
|
|
666
|
+
while (current && typeof current === "object" && typeof current.$ref === "string") {
|
|
667
|
+
if (seen.has(current))
|
|
668
|
+
return undefined;
|
|
669
|
+
seen.add(current);
|
|
670
|
+
const ref = current.$ref;
|
|
671
|
+
if (!ref.startsWith("#"))
|
|
672
|
+
return undefined;
|
|
673
|
+
let target;
|
|
674
|
+
for (let i = walked.length - 1; i >= 0 && target === undefined; i--) {
|
|
675
|
+
target = schemaAtPointer(walked[i], ref.slice(1));
|
|
676
|
+
}
|
|
677
|
+
current = target;
|
|
678
|
+
}
|
|
679
|
+
return current;
|
|
680
|
+
}
|
|
681
|
+
/** The node a JSON Pointer (the part of a reference after `#`) names in `root`. */
|
|
682
|
+
function schemaAtPointer(root, pointer) {
|
|
683
|
+
if (pointer === "")
|
|
684
|
+
return root;
|
|
685
|
+
if (!pointer.startsWith("/"))
|
|
686
|
+
return undefined;
|
|
687
|
+
let current = root;
|
|
688
|
+
for (const raw of pointer.slice(1).split("/")) {
|
|
689
|
+
const segment = decodeURIComponent(raw).replace(/~1/g, "/").replace(/~0/g, "~");
|
|
690
|
+
if (!current || typeof current !== "object")
|
|
691
|
+
return undefined;
|
|
692
|
+
current = current[segment];
|
|
693
|
+
}
|
|
694
|
+
return current && typeof current === "object" && !Array.isArray(current)
|
|
695
|
+
? current
|
|
696
|
+
: undefined;
|
|
697
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { buildCelEnvironment, buildCelLanguageEnvironment, celBuiltinFunctions, celFunctionCatalog, type CelHandlers, } from "./cel/environment.js";
|
|
2
2
|
export { compileExpression, namespaceDispatchOf, siteEnvironment, type CelProgramSite, } from "./cel/compile.js";
|
|
3
3
|
export { interpolationShape, literalFragments, readInterpolationHoles, type HoleReading, type InterpolationHole, type InterpolationShape, } from "./cel/interpolation-holes.js";
|
|
4
|
-
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
4
|
+
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, schemaConjuncts, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
5
5
|
export { auditCalls, explainUnresolved, type CallAudit, type ModuleCallFlags, } from "./cel/diagnose.js";
|
|
6
6
|
export { celNamespaceNames, MODULE_CALL_DISPATCH_KEY, moduleCallNames, moduleCallOf, type ModuleCallDispatch, type ModuleCallTypeResolver, } from "./cel/module-call.js";
|
|
7
7
|
export { walkCelExpressions } from "./cel/walk.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EACxB,aAAa,EACb,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,KAAK,SAAS,EACd,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,cAAc,EACd,Y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UAAU,EACV,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,KAAK,SAAS,EACd,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,cAAc,EACd,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,eAA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,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ export { buildCelEnvironment, buildCelLanguageEnvironment, celBuiltinFunctions,
|
|
|
5
5
|
// adapter is what let it convert arguments differently from the kernel.
|
|
6
6
|
export { compileExpression, namespaceDispatchOf, siteEnvironment, } from "./cel/compile.js";
|
|
7
7
|
export { interpolationShape, literalFragments, readInterpolationHoles, } from "./cel/interpolation-holes.js";
|
|
8
|
-
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
8
|
+
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, schemaConjuncts, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
9
9
|
export { auditCalls, explainUnresolved, } from "./cel/diagnose.js";
|
|
10
10
|
// The rewrite, its reader, and the activation key the kernel binds a scope's
|
|
11
11
|
// dispatch table under. The unbound message and the receiver and bound-name
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/templating",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.129.0",
|
|
4
4
|
"description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"re2js": "2.8.3",
|
|
40
40
|
"uuid": "14.0.1",
|
|
41
41
|
"yaml": "^2.8.3",
|
|
42
|
-
"@telorun/cel": "0.
|
|
42
|
+
"@telorun/cel": "0.129.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/d3-format": "^3.0.4",
|
|
46
46
|
"@types/node": "^20.0.0",
|
|
47
47
|
"typescript": "^5.0.0",
|
|
48
48
|
"vitest": "^2.1.8",
|
|
49
|
-
"@telorun/sdk": "0.
|
|
49
|
+
"@telorun/sdk": "0.129.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@telorun/sdk": "*"
|
package/src/cel/analyze.ts
CHANGED
|
@@ -329,7 +329,12 @@ interface NullableIssue {
|
|
|
329
329
|
function schemaIsNullable(schema: Record<string, any> | undefined): boolean {
|
|
330
330
|
if (!schema || typeof schema !== "object") return false;
|
|
331
331
|
const t = schema.type;
|
|
332
|
-
|
|
332
|
+
if (t === "null" || (Array.isArray(t) && t.includes("null"))) return true;
|
|
333
|
+
// A union admits null when one of its branches does.
|
|
334
|
+
const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : [];
|
|
335
|
+
return (branches as unknown[]).some((branch) =>
|
|
336
|
+
schemaIsNullable(branch as Record<string, any> | undefined),
|
|
337
|
+
);
|
|
333
338
|
}
|
|
334
339
|
|
|
335
340
|
/** Navigate `schema` following a member chain, descending through `properties`.
|
|
@@ -513,6 +518,17 @@ function walkNullable(
|
|
|
513
518
|
* reach inside a `live` value type — one whose consumption has effects, so its
|
|
514
519
|
* contents exist only for a consumer that drains it.
|
|
515
520
|
* Returns null when the chain is valid or the schema is too open to judge.
|
|
521
|
+
*
|
|
522
|
+
* A node is read with its `allOf` conjuncts ({@link schemaConjuncts}), and a
|
|
523
|
+
* document-local `$ref` (`#…`) is followed against the nearest node on the walk
|
|
524
|
+
* that holds its target — the innermost document a contract embedded whole
|
|
525
|
+
* carries its reached types in. A union (`anyOf` / `oneOf`) is read in every
|
|
526
|
+
* alternative: one that cannot hold an object (a null or scalar type, a value
|
|
527
|
+
* type with no members) or a closed object not declaring the member contributes
|
|
528
|
+
* nothing, and the chain continues through every alternative declaring it. A
|
|
529
|
+
* member is unknown only when every alternative is read and none declares it
|
|
530
|
+
* nor is open; an alternative, conjunct or reference that cannot be read leaves
|
|
531
|
+
* the chain unjudged.
|
|
516
532
|
*/
|
|
517
533
|
export function validateChainAgainstSchema(
|
|
518
534
|
chain: string[],
|
|
@@ -550,49 +566,199 @@ export function chainFailureAgainstSchema(
|
|
|
550
566
|
chain: string[],
|
|
551
567
|
schema: Record<string, any>,
|
|
552
568
|
): { path: string; message: string } | null {
|
|
553
|
-
|
|
569
|
+
const walked: Record<string, any>[] = [];
|
|
570
|
+
let current: Record<string, any>[] = [schema];
|
|
554
571
|
for (let i = 0; i < chain.length; i++) {
|
|
555
572
|
const key = chain[i]!;
|
|
556
|
-
|
|
573
|
+
const alternatives = alternativesOf(
|
|
574
|
+
current,
|
|
575
|
+
walked,
|
|
576
|
+
key === INDEX_SEGMENT ? holdsCollection : holdsObject,
|
|
577
|
+
);
|
|
578
|
+
if (!alternatives || alternatives.length === 0) return null;
|
|
579
|
+
walked.push(...alternatives);
|
|
557
580
|
// An index reads an ELEMENT: an array's `items`, or a map's value schema. What
|
|
558
581
|
// is below it is checked like any member access, which is what reaches a
|
|
559
582
|
// typo inside a list of records (`items[0].amont`). An object that also
|
|
560
583
|
// declares `properties` may be indexed by one of those names, which the
|
|
561
584
|
// chain does not record, so nothing below it is judged.
|
|
562
585
|
if (key === INDEX_SEGMENT) {
|
|
563
|
-
const
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
586
|
+
const elements: Record<string, any>[] = [];
|
|
587
|
+
for (const alternative of alternatives) {
|
|
588
|
+
const element =
|
|
589
|
+
alternative.items && typeof alternative.items === "object" && !Array.isArray(alternative.items)
|
|
590
|
+
? alternative.items
|
|
591
|
+
: !alternative.properties &&
|
|
592
|
+
alternative.additionalProperties &&
|
|
593
|
+
typeof alternative.additionalProperties === "object"
|
|
594
|
+
? alternative.additionalProperties
|
|
595
|
+
: undefined;
|
|
596
|
+
if (!element) return null;
|
|
597
|
+
elements.push(element);
|
|
598
|
+
}
|
|
599
|
+
current = elements;
|
|
573
600
|
continue;
|
|
574
601
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
const
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
602
|
+
const found: Record<string, any>[] = [];
|
|
603
|
+
let declares = false;
|
|
604
|
+
const available: string[] = [];
|
|
605
|
+
for (const alternative of alternatives) {
|
|
606
|
+
const conjuncts = schemaConjuncts(
|
|
607
|
+
alternative,
|
|
608
|
+
(node) => node,
|
|
609
|
+
(_, raw) => followLocalRef(raw as Record<string, any>, walked),
|
|
610
|
+
);
|
|
611
|
+
if (!conjuncts) return null;
|
|
612
|
+
let declared: Record<string, any> | undefined;
|
|
613
|
+
let groupDeclares = false;
|
|
614
|
+
for (const conjunct of conjuncts) {
|
|
615
|
+
// A value type declaring members is read by those members alone: they are
|
|
616
|
+
// its fields, closed as the CEL type the type registers.
|
|
617
|
+
const members = membersOf(conjunct);
|
|
618
|
+
const props: Record<string, any> | undefined = members ?? conjunct.properties;
|
|
619
|
+
if (!props) continue;
|
|
620
|
+
groupDeclares = true;
|
|
621
|
+
if (key in props) {
|
|
622
|
+
declared = props[key];
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
if (!members && conjunct.additionalProperties === true) return null;
|
|
626
|
+
for (const name of Object.keys(props)) if (!available.includes(name)) available.push(name);
|
|
627
|
+
}
|
|
628
|
+
// An alternative saying nothing about its members could hold this one.
|
|
629
|
+
if (!groupDeclares) return null;
|
|
630
|
+
declares = true;
|
|
631
|
+
if (declared !== undefined) found.push(declared);
|
|
632
|
+
}
|
|
633
|
+
if (found.length > 0) {
|
|
634
|
+
if (i < chain.length - 1 && found.some((node) => isLiveSlot(node))) {
|
|
583
635
|
const path = chain.slice(0, i + 1).join(".");
|
|
584
636
|
return {
|
|
585
637
|
path,
|
|
586
638
|
message: `'${path}' yields a stream — pipe it through an Encoder or iterate in a JS.Script step (no member access on stream-typed values)`,
|
|
587
639
|
};
|
|
588
640
|
}
|
|
589
|
-
current =
|
|
641
|
+
current = found;
|
|
590
642
|
continue;
|
|
591
643
|
}
|
|
592
|
-
if (!
|
|
644
|
+
if (!declares) return null;
|
|
593
645
|
const path = chain.slice(0, i + 1).join(".");
|
|
594
|
-
|
|
595
|
-
return { path, message: `'${path}' is not defined (available: ${available})` };
|
|
646
|
+
return { path, message: `'${path}' is not defined (available: ${available.join(", ")})` };
|
|
596
647
|
}
|
|
597
648
|
return null;
|
|
598
649
|
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* The alternatives a value at `nodes` may be read through: each node with its
|
|
653
|
+
* references followed and every union split into its branches, recursively,
|
|
654
|
+
* keeping only those `holds` accepts — what can hold the member or element the
|
|
655
|
+
* chain reads next. Undefined when a reference cannot be read.
|
|
656
|
+
*/
|
|
657
|
+
function alternativesOf(
|
|
658
|
+
nodes: readonly Record<string, any>[],
|
|
659
|
+
walked: readonly Record<string, any>[],
|
|
660
|
+
holds: (node: Record<string, any>) => boolean,
|
|
661
|
+
): Record<string, any>[] | undefined {
|
|
662
|
+
const out: Record<string, any>[] = [];
|
|
663
|
+
const visit = (raw: Record<string, any>, depth: number): boolean => {
|
|
664
|
+
if (depth > 32) return false;
|
|
665
|
+
const node = followLocalRef(raw, [...walked, ...out]);
|
|
666
|
+
if (!node || typeof node !== "object") return false;
|
|
667
|
+
const branches = Array.isArray(node.anyOf) ? node.anyOf : Array.isArray(node.oneOf) ? node.oneOf : undefined;
|
|
668
|
+
if (branches && !node.properties && !node.allOf) {
|
|
669
|
+
for (const branch of branches as Record<string, any>[]) {
|
|
670
|
+
if (!branch || typeof branch !== "object" || !visit(branch, depth + 1)) return false;
|
|
671
|
+
}
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
if (holds(node)) out.push(node);
|
|
675
|
+
return true;
|
|
676
|
+
};
|
|
677
|
+
for (const node of nodes) if (!visit(node, 0)) return undefined;
|
|
678
|
+
return out;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Whether a value at `node` can be a list or a map a chain indexes into. */
|
|
682
|
+
function holdsCollection(node: Record<string, any>): boolean {
|
|
683
|
+
if (node["x-telo-type"] !== undefined) return false;
|
|
684
|
+
const types = Array.isArray(node.type) ? node.type : node.type === undefined ? undefined : [node.type];
|
|
685
|
+
return !types || types.includes("array") || types.includes("object");
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** Whether a value at `node` can be an object whose members a chain reads: a
|
|
689
|
+
* node typed as no object (null, a scalar, a list) or a value type with no
|
|
690
|
+
* members cannot. */
|
|
691
|
+
function holdsObject(node: Record<string, any>): boolean {
|
|
692
|
+
if (membersOf(node)) return true;
|
|
693
|
+
if (node["x-telo-type"] !== undefined) return false;
|
|
694
|
+
const types = Array.isArray(node.type) ? node.type : node.type === undefined ? undefined : [node.type];
|
|
695
|
+
if (types && !types.includes("object")) return false;
|
|
696
|
+
if ("const" in node && (node.const === null || typeof node.const !== "object")) return false;
|
|
697
|
+
return true;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* A schema node and each of its `allOf` members, recursively — or undefined when
|
|
702
|
+
* a member cannot be read, which leaves the node unjudged. `schemaOf` reads the
|
|
703
|
+
* schema off whatever the caller carries per node (a node alone, or a node with
|
|
704
|
+
* the document its references resolve against), and `member` reads one written
|
|
705
|
+
* member in its parent's context, following its references.
|
|
706
|
+
*
|
|
707
|
+
* Shared by every walk reading a node as the conjunction it is — the CEL chain
|
|
708
|
+
* check and the analyzer's plain-literal decoding.
|
|
709
|
+
*/
|
|
710
|
+
export function schemaConjuncts<N>(
|
|
711
|
+
node: N,
|
|
712
|
+
schemaOf: (node: N) => Record<string, any>,
|
|
713
|
+
member: (parent: N, raw: unknown) => N | undefined,
|
|
714
|
+
): N[] | undefined {
|
|
715
|
+
const out = [node];
|
|
716
|
+
const all = schemaOf(node).allOf;
|
|
717
|
+
if (!Array.isArray(all)) return out;
|
|
718
|
+
for (const raw of all as unknown[]) {
|
|
719
|
+
const read = member(node, raw);
|
|
720
|
+
if (read === undefined) return undefined;
|
|
721
|
+
const nested = schemaConjuncts(read, schemaOf, member);
|
|
722
|
+
if (!nested) return undefined;
|
|
723
|
+
out.push(...nested);
|
|
724
|
+
}
|
|
725
|
+
return out;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** `node` with every document-local `$ref` it is followed through, resolved
|
|
729
|
+
* against the nearest node of `walked` holding the target. A reference that is
|
|
730
|
+
* not local, or whose target no walked node holds, yields undefined. */
|
|
731
|
+
function followLocalRef(
|
|
732
|
+
node: Record<string, any> | undefined,
|
|
733
|
+
walked: readonly Record<string, any>[],
|
|
734
|
+
): Record<string, any> | undefined {
|
|
735
|
+
const seen = new Set<object>();
|
|
736
|
+
let current = node;
|
|
737
|
+
while (current && typeof current === "object" && typeof current.$ref === "string") {
|
|
738
|
+
if (seen.has(current)) return undefined;
|
|
739
|
+
seen.add(current);
|
|
740
|
+
const ref = current.$ref as string;
|
|
741
|
+
if (!ref.startsWith("#")) return undefined;
|
|
742
|
+
let target: Record<string, any> | undefined;
|
|
743
|
+
for (let i = walked.length - 1; i >= 0 && target === undefined; i--) {
|
|
744
|
+
target = schemaAtPointer(walked[i]!, ref.slice(1));
|
|
745
|
+
}
|
|
746
|
+
current = target;
|
|
747
|
+
}
|
|
748
|
+
return current;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/** The node a JSON Pointer (the part of a reference after `#`) names in `root`. */
|
|
752
|
+
function schemaAtPointer(root: Record<string, any>, pointer: string): Record<string, any> | undefined {
|
|
753
|
+
if (pointer === "") return root;
|
|
754
|
+
if (!pointer.startsWith("/")) return undefined;
|
|
755
|
+
let current: unknown = root;
|
|
756
|
+
for (const raw of pointer.slice(1).split("/")) {
|
|
757
|
+
const segment = decodeURIComponent(raw).replace(/~1/g, "/").replace(/~0/g, "~");
|
|
758
|
+
if (!current || typeof current !== "object") return undefined;
|
|
759
|
+
current = (current as Record<string, unknown>)[segment];
|
|
760
|
+
}
|
|
761
|
+
return current && typeof current === "object" && !Array.isArray(current)
|
|
762
|
+
? (current as Record<string, any>)
|
|
763
|
+
: undefined;
|
|
764
|
+
}
|