@flowget/graph-validation 0.1.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/LICENSE +105 -0
- package/README.md +71 -0
- package/dist/catalog.d.ts +64 -0
- package/dist/catalog.js +89 -0
- package/dist/coherence.d.ts +52 -0
- package/dist/coherence.js +94 -0
- package/dist/field-rules.d.ts +18 -0
- package/dist/field-rules.js +183 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +28 -0
- package/dist/issues.d.ts +84 -0
- package/dist/issues.js +66 -0
- package/dist/reachability.d.ts +37 -0
- package/dist/reachability.js +60 -0
- package/dist/references.d.ts +30 -0
- package/dist/references.js +312 -0
- package/dist/template-ast.d.ts +101 -0
- package/dist/template-ast.js +211 -0
- package/dist/validate-graph.d.ts +32 -0
- package/dist/validate-graph.js +53 -0
- package/dist/value-type.d.ts +41 -0
- package/dist/value-type.js +94 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Flowget
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @flowget/graph-validation
|
|
2
|
+
|
|
3
|
+
Isomorphic, zero-runtime-dependency **static validator** for Flowget workflow
|
|
4
|
+
graphs.
|
|
5
|
+
|
|
6
|
+
This is the **single source of validation logic** shared by the Flowget
|
|
7
|
+
builder (design-time, pre-submit) and the Flowget worker (host-side,
|
|
8
|
+
pre-flight). Because both call the exact same pure functions over the exact
|
|
9
|
+
same `(graph, catalog)` input, the builder's verdict and the worker's verdict
|
|
10
|
+
are **equal by construction** — a graph the builder accepts is a graph the
|
|
11
|
+
worker accepts.
|
|
12
|
+
|
|
13
|
+
> **Contingency.** "Equal by construction" holds *given both consumers feed
|
|
14
|
+
> catalogs derived from the same node-definition source.* Catalog
|
|
15
|
+
> `outputFields` / `triggerPayload` / `options` can be **factory functions** —
|
|
16
|
+
> code, not data — so this package guarantees the validation *function* is
|
|
17
|
+
> identical, not that the two sides' catalog *inputs* are. Keeping those
|
|
18
|
+
> definitions in sync is the job of the shared registry and the
|
|
19
|
+
> `compareFieldCoherence` boot check, not of this validator.
|
|
20
|
+
|
|
21
|
+
What it does:
|
|
22
|
+
|
|
23
|
+
- **Template-grammar classifier** — parses `{{ … }}` template strings into the
|
|
24
|
+
`@flowget/types` `TemplateAst` (v2-step / v1-reserved / v1-step operands,
|
|
25
|
+
`??` fallback chains). ReDoS-safe on untrusted input.
|
|
26
|
+
- **Reference existence + reachability** — every `{{ steps.<id>.output.<key> }}`
|
|
27
|
+
token must name a real upstream node and a declared output.
|
|
28
|
+
- **Coarse value-type lattice** — `isAssignable(producer, consumer)` over the
|
|
29
|
+
`FieldValueType` set; permissive on `unknown` / `object` / `array` so a
|
|
30
|
+
"valid" verdict never lies (zero false positives).
|
|
31
|
+
- **Declarative field rules** — `required`, coarse type, enum membership,
|
|
32
|
+
`min` / `max` / `pattern`.
|
|
33
|
+
- **`validateGraphStatic(graph, catalog)`** — the aggregator, returning a flat
|
|
34
|
+
`ValidationIssue[]`.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
npm install @flowget/graph-validation
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`@flowget/types` is a peer dependency (`>=0.5.0 <1.0.0`) — install it too if it
|
|
43
|
+
is not already in your tree.
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { validateGraphStatic } from "@flowget/graph-validation";
|
|
49
|
+
|
|
50
|
+
const issues = validateGraphStatic(graph, catalog);
|
|
51
|
+
|
|
52
|
+
const blocking = issues.filter((i) => i.severity === "error");
|
|
53
|
+
if (blocking.length > 0) {
|
|
54
|
+
// builder: disable the Run button; worker: refuse the pre-flight
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`graph` is a `@flowget/types` `WorkflowGraph`; `catalog` is the list of node
|
|
59
|
+
definitions (structurally a subset of `NodeMetadata<BuilderHints>`), fed
|
|
60
|
+
identically by both consumers.
|
|
61
|
+
|
|
62
|
+
## ⚠️ 0.x is unstable
|
|
63
|
+
|
|
64
|
+
This package is on the `0.x` line. The surface may change in breaking ways
|
|
65
|
+
between `0.x` minors until `1.0.0` (`0.x` semver: minor = breaking, patch =
|
|
66
|
+
additive). Pin a caret range and read the `CHANGELOG` before upgrading.
|
|
67
|
+
|
|
68
|
+
## Links
|
|
69
|
+
|
|
70
|
+
- Docs: https://docs.flowget.io
|
|
71
|
+
- Contract types: [`@flowget/types`](https://github.com/flowgethq/types)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The INPUT interface — the catalog half of `validateGraphStatic(graph,
|
|
3
|
+
* catalog)`. Defined so the builder and the worker feed the validator
|
|
4
|
+
* IDENTICALLY: both already hold `NodeMetadata<BuilderHints>` objects, and
|
|
5
|
+
* {@link CatalogNode} is a structural SUBSET of that shape, so each side
|
|
6
|
+
* passes its existing catalog list unchanged (structural typing → assignable).
|
|
7
|
+
* The `@flowget/types` `WorkflowGraph` is the graph half.
|
|
8
|
+
*
|
|
9
|
+
* The output-field / trigger-payload resolution below is lifted from the
|
|
10
|
+
* builder's `variables.ts` engine (the `outputVariants` → factory → static
|
|
11
|
+
* precedence) so the "referenceable set" the validator computes is
|
|
12
|
+
* byte-for-byte the set the builder's variable picker offers — reference
|
|
13
|
+
* validity is equal to picker availability BY CONSTRUCTION.
|
|
14
|
+
*/
|
|
15
|
+
import type { ConfigField, NodeCategory, OutputField, OutputVariants } from "@flowget/types";
|
|
16
|
+
/**
|
|
17
|
+
* A single catalog entry the validator reads. STRUCTURAL SUBSET of
|
|
18
|
+
* `NodeMetadata<BuilderHints>` — every field is optional except `id`, so a
|
|
19
|
+
* consumer's richer catalog object is assignable to it without adaptation.
|
|
20
|
+
* The complete-shape compile-time proof of that assignability lives in
|
|
21
|
+
* `catalog.types-test.ts` (a tsc-only pin, not run by vitest, not published).
|
|
22
|
+
*/
|
|
23
|
+
export interface CatalogNode {
|
|
24
|
+
/** Registry lookup key — matched against `WorkflowNode.type`. */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Walker behavior + trigger/decorator gates. `"trigger"` marks an entrypoint. */
|
|
27
|
+
category?: NodeCategory;
|
|
28
|
+
/** Declarative config-form schema — the fields whose values field-rules validate. */
|
|
29
|
+
fields?: readonly ConfigField[];
|
|
30
|
+
/**
|
|
31
|
+
* Output fields the node produces under `output.<key>` — the reference
|
|
32
|
+
* walker's producer surface. Static array OR a pure `(config)=>fields`
|
|
33
|
+
* factory (config-dependent shapes, e.g. `switch`).
|
|
34
|
+
*/
|
|
35
|
+
outputFields?: readonly OutputField[] | ((config: Record<string, unknown>) => readonly OutputField[]);
|
|
36
|
+
/** Reserved `{{ trigger.<key> }}` payload — only `category: "trigger"` nodes populate it. */
|
|
37
|
+
triggerPayload?: readonly OutputField[] | ((config: Record<string, unknown>) => readonly OutputField[]);
|
|
38
|
+
/** Declarative, JSON-serializable variant output (precedence over the static forms). */
|
|
39
|
+
outputVariants?: OutputVariants;
|
|
40
|
+
/** Field keys NEVER template-resolved by the walker — skipped by reference validation. */
|
|
41
|
+
opaqueFields?: readonly string[];
|
|
42
|
+
/** Decorator-host allow-list (surfaced for completeness; not yet enforced by the walker). */
|
|
43
|
+
allowedDecorators?: readonly string[];
|
|
44
|
+
}
|
|
45
|
+
/** An indexed catalog: node-type id → {@link CatalogNode}. */
|
|
46
|
+
export type CatalogIndex = ReadonlyMap<string, CatalogNode>;
|
|
47
|
+
/**
|
|
48
|
+
* Build the type-id → node index. On a duplicate id the FIRST wins (stable,
|
|
49
|
+
* deterministic) — matches the builder's `catalog.list().find(...)` and the
|
|
50
|
+
* worker's registry "first registration wins".
|
|
51
|
+
*/
|
|
52
|
+
export declare function indexCatalog(catalog: readonly CatalogNode[]): CatalogIndex;
|
|
53
|
+
/**
|
|
54
|
+
* The node's output fields for a given instance `config`, honoring
|
|
55
|
+
* `outputVariants` precedence over the static / factory `outputFields`.
|
|
56
|
+
* Faithful port of `variables.ts`'s `resolveOutputFields`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function resolveNodeOutputFields(node: CatalogNode, config: Record<string, unknown>): readonly OutputField[];
|
|
59
|
+
/**
|
|
60
|
+
* The node's trigger-payload fields for a given instance `config`, honoring
|
|
61
|
+
* `outputVariants` precedence. Faithful port of `variables.ts`'s
|
|
62
|
+
* `resolveTriggerPayload`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function resolveNodeTriggerPayload(node: CatalogNode, config: Record<string, unknown>): readonly OutputField[];
|
package/dist/catalog.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The INPUT interface — the catalog half of `validateGraphStatic(graph,
|
|
3
|
+
* catalog)`. Defined so the builder and the worker feed the validator
|
|
4
|
+
* IDENTICALLY: both already hold `NodeMetadata<BuilderHints>` objects, and
|
|
5
|
+
* {@link CatalogNode} is a structural SUBSET of that shape, so each side
|
|
6
|
+
* passes its existing catalog list unchanged (structural typing → assignable).
|
|
7
|
+
* The `@flowget/types` `WorkflowGraph` is the graph half.
|
|
8
|
+
*
|
|
9
|
+
* The output-field / trigger-payload resolution below is lifted from the
|
|
10
|
+
* builder's `variables.ts` engine (the `outputVariants` → factory → static
|
|
11
|
+
* precedence) so the "referenceable set" the validator computes is
|
|
12
|
+
* byte-for-byte the set the builder's variable picker offers — reference
|
|
13
|
+
* validity is equal to picker availability BY CONSTRUCTION.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Build the type-id → node index. On a duplicate id the FIRST wins (stable,
|
|
17
|
+
* deterministic) — matches the builder's `catalog.list().find(...)` and the
|
|
18
|
+
* worker's registry "first registration wins".
|
|
19
|
+
*/
|
|
20
|
+
export function indexCatalog(catalog) {
|
|
21
|
+
const index = new Map();
|
|
22
|
+
for (const node of catalog) {
|
|
23
|
+
if (!index.has(node.id))
|
|
24
|
+
index.set(node.id, node);
|
|
25
|
+
}
|
|
26
|
+
return index;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolve the active variant for a node whose output shape varies by a
|
|
30
|
+
* config field value. Reads `data[variantBy]`, matches a variant (coercing
|
|
31
|
+
* a non-string key to "no match"), else the `fallback` variant, else
|
|
32
|
+
* `undefined` (caller falls through to the static forms). Mirrors
|
|
33
|
+
* `variables.ts`'s `resolveVariant`.
|
|
34
|
+
*/
|
|
35
|
+
function resolveVariant(ov, config) {
|
|
36
|
+
const raw = config[ov.variantBy];
|
|
37
|
+
const key = typeof raw === "string" ? raw : undefined;
|
|
38
|
+
const matched = key !== undefined ? ov.variants[key] : undefined;
|
|
39
|
+
if (matched !== undefined)
|
|
40
|
+
return matched;
|
|
41
|
+
if (ov.fallback !== undefined)
|
|
42
|
+
return ov.variants[ov.fallback];
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Invoke a `readonly OutputField[] | (config)=>readonly OutputField[]` slot,
|
|
47
|
+
* resolving the factory form against `config`. A throwing factory yields
|
|
48
|
+
* `[]` (treated as "unknown outputs" → permissive) — the validator must
|
|
49
|
+
* never crash on a hostile/half-edited config.
|
|
50
|
+
*/
|
|
51
|
+
function resolveFieldSlot(slot, config) {
|
|
52
|
+
if (slot === undefined)
|
|
53
|
+
return [];
|
|
54
|
+
if (typeof slot === "function") {
|
|
55
|
+
try {
|
|
56
|
+
return slot(config);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return slot;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The node's output fields for a given instance `config`, honoring
|
|
66
|
+
* `outputVariants` precedence over the static / factory `outputFields`.
|
|
67
|
+
* Faithful port of `variables.ts`'s `resolveOutputFields`.
|
|
68
|
+
*/
|
|
69
|
+
export function resolveNodeOutputFields(node, config) {
|
|
70
|
+
if (node.outputVariants !== undefined) {
|
|
71
|
+
const variant = resolveVariant(node.outputVariants, config);
|
|
72
|
+
if (variant !== undefined)
|
|
73
|
+
return variant.outputFields ?? [];
|
|
74
|
+
}
|
|
75
|
+
return resolveFieldSlot(node.outputFields, config);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The node's trigger-payload fields for a given instance `config`, honoring
|
|
79
|
+
* `outputVariants` precedence. Faithful port of `variables.ts`'s
|
|
80
|
+
* `resolveTriggerPayload`.
|
|
81
|
+
*/
|
|
82
|
+
export function resolveNodeTriggerPayload(node, config) {
|
|
83
|
+
if (node.outputVariants !== undefined) {
|
|
84
|
+
const variant = resolveVariant(node.outputVariants, config);
|
|
85
|
+
if (variant !== undefined)
|
|
86
|
+
return variant.triggerPayload ?? [];
|
|
87
|
+
}
|
|
88
|
+
return resolveFieldSlot(node.triggerPayload, config);
|
|
89
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Best-effort node.json ↔ schema coherence comparator — the helper the
|
|
3
|
+
* worker's boot-coherence check calls to catch a node whose declarative
|
|
4
|
+
* `node.json` fields have drifted from its runtime `zod` input
|
|
5
|
+
* schema (a field renamed in one place but not the other, a type widened on
|
|
6
|
+
* one side).
|
|
7
|
+
*
|
|
8
|
+
* **Zod-free by construction.** This package must never import `zod` (it has
|
|
9
|
+
* to run isomorphically, and staying schema-free is the honesty boundary the
|
|
10
|
+
* "valid" verdict rests on). The worker introspects its live `ZodObject` into
|
|
11
|
+
* a plain {@link SchemaProjection} — `key → { valueType, required }` — and
|
|
12
|
+
* passes THAT here. All the zod-specific reflection stays host-side in the
|
|
13
|
+
* worker; this comparator only diffs two coarse projections.
|
|
14
|
+
*
|
|
15
|
+
* **Best-effort.** Coarse value-types cannot see refinements, unions, or
|
|
16
|
+
* transforms, so every mismatch is advisory (a `warning`-grade signal for the
|
|
17
|
+
* worker's boot log), never a hard graph error.
|
|
18
|
+
*/
|
|
19
|
+
import type { ConfigField, FieldValueType } from "@flowget/types";
|
|
20
|
+
/** One field of an introspected schema, projected onto the coarse lattice. */
|
|
21
|
+
export type SchemaFieldProjection = {
|
|
22
|
+
/** Coarse value-type of the schema field, if determinable. */
|
|
23
|
+
valueType?: FieldValueType;
|
|
24
|
+
/** Whether the schema requires the field (a non-optional key). */
|
|
25
|
+
required?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** A whole introspected input schema, projected: field key → {@link SchemaFieldProjection}. */
|
|
28
|
+
export type SchemaProjection = Record<string, SchemaFieldProjection>;
|
|
29
|
+
/** The kind of coherence drift found between a node's `node.json` and its schema. */
|
|
30
|
+
export type CoherenceMismatchKind = "missing-in-schema" | "missing-in-fields" | "type-drift" | "required-drift";
|
|
31
|
+
/** One best-effort coherence finding. Advisory — never a hard graph error. */
|
|
32
|
+
export type CoherenceMismatch = {
|
|
33
|
+
fieldKey: string;
|
|
34
|
+
kind: CoherenceMismatchKind;
|
|
35
|
+
message: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Diff a node's declarative `fields` (`node.json`) against an introspected
|
|
39
|
+
* `schema` projection, returning best-effort coherence mismatches:
|
|
40
|
+
*
|
|
41
|
+
* - `missing-in-schema` — `node.json` declares a field the schema omits.
|
|
42
|
+
* - `missing-in-fields` — the schema requires a field `node.json` omits
|
|
43
|
+
* (an optional schema key is NOT flagged — a node need not surface every
|
|
44
|
+
* optional input as an editable field).
|
|
45
|
+
* - `type-drift` — both sides declare the field with DIFFERENT concrete
|
|
46
|
+
* value-types (coarse lattice; wildcards never drift).
|
|
47
|
+
* - `required-drift` — required on one side, optional on the other.
|
|
48
|
+
*
|
|
49
|
+
* Deterministic: findings are emitted in `fields` order, then in `schema`
|
|
50
|
+
* key-insertion order. No `Date` / random.
|
|
51
|
+
*/
|
|
52
|
+
export declare function compareFieldCoherence(fields: readonly ConfigField[], schema: SchemaProjection): CoherenceMismatch[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Best-effort node.json ↔ schema coherence comparator — the helper the
|
|
3
|
+
* worker's boot-coherence check calls to catch a node whose declarative
|
|
4
|
+
* `node.json` fields have drifted from its runtime `zod` input
|
|
5
|
+
* schema (a field renamed in one place but not the other, a type widened on
|
|
6
|
+
* one side).
|
|
7
|
+
*
|
|
8
|
+
* **Zod-free by construction.** This package must never import `zod` (it has
|
|
9
|
+
* to run isomorphically, and staying schema-free is the honesty boundary the
|
|
10
|
+
* "valid" verdict rests on). The worker introspects its live `ZodObject` into
|
|
11
|
+
* a plain {@link SchemaProjection} — `key → { valueType, required }` — and
|
|
12
|
+
* passes THAT here. All the zod-specific reflection stays host-side in the
|
|
13
|
+
* worker; this comparator only diffs two coarse projections.
|
|
14
|
+
*
|
|
15
|
+
* **Best-effort.** Coarse value-types cannot see refinements, unions, or
|
|
16
|
+
* transforms, so every mismatch is advisory (a `warning`-grade signal for the
|
|
17
|
+
* worker's boot log), never a hard graph error.
|
|
18
|
+
*/
|
|
19
|
+
import { resolveFieldValueType } from "@flowget/types";
|
|
20
|
+
import { isAssignable, isConcreteScalar } from "./value-type.js";
|
|
21
|
+
/**
|
|
22
|
+
* Diff a node's declarative `fields` (`node.json`) against an introspected
|
|
23
|
+
* `schema` projection, returning best-effort coherence mismatches:
|
|
24
|
+
*
|
|
25
|
+
* - `missing-in-schema` — `node.json` declares a field the schema omits.
|
|
26
|
+
* - `missing-in-fields` — the schema requires a field `node.json` omits
|
|
27
|
+
* (an optional schema key is NOT flagged — a node need not surface every
|
|
28
|
+
* optional input as an editable field).
|
|
29
|
+
* - `type-drift` — both sides declare the field with DIFFERENT concrete
|
|
30
|
+
* value-types (coarse lattice; wildcards never drift).
|
|
31
|
+
* - `required-drift` — required on one side, optional on the other.
|
|
32
|
+
*
|
|
33
|
+
* Deterministic: findings are emitted in `fields` order, then in `schema`
|
|
34
|
+
* key-insertion order. No `Date` / random.
|
|
35
|
+
*/
|
|
36
|
+
export function compareFieldCoherence(fields, schema) {
|
|
37
|
+
const mismatches = [];
|
|
38
|
+
const fieldKeys = new Set(fields.map((f) => f.key));
|
|
39
|
+
for (const field of fields) {
|
|
40
|
+
const projected = schema[field.key];
|
|
41
|
+
if (projected === undefined) {
|
|
42
|
+
mismatches.push({
|
|
43
|
+
fieldKey: field.key,
|
|
44
|
+
kind: "missing-in-schema",
|
|
45
|
+
message: `node.json declares field "${field.key}" but the schema has no such key.`,
|
|
46
|
+
});
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// "Author-may-tighten": node.json is coherent with the schema when its
|
|
50
|
+
// value-type is EQUAL or MORE SPECIFIC — the author may narrow, not
|
|
51
|
+
// contradict. `isAssignable(nodeJson, schema)` encodes exactly that:
|
|
52
|
+
// - `date` node.json vs `string` schema → coherent (`date ⊆ string`;
|
|
53
|
+
// dates are JSON strings, so `z.string()` is the real date schema),
|
|
54
|
+
// - `object` / `array` node.json vs `unknown` schema → coherent
|
|
55
|
+
// (wildcard schema absorbs anything),
|
|
56
|
+
// - only two DISJOINT concrete scalars (e.g. `number` vs `string`) drift.
|
|
57
|
+
const fieldType = resolveFieldValueType(field);
|
|
58
|
+
const schemaType = projected.valueType;
|
|
59
|
+
if (schemaType !== undefined &&
|
|
60
|
+
isConcreteScalar(fieldType) &&
|
|
61
|
+
isConcreteScalar(schemaType) &&
|
|
62
|
+
!isAssignable(fieldType, schemaType)) {
|
|
63
|
+
mismatches.push({
|
|
64
|
+
fieldKey: field.key,
|
|
65
|
+
kind: "type-drift",
|
|
66
|
+
message: `field "${field.key}" is ${fieldType} in node.json but ${schemaType} in the schema.`,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// A field required by node.json but optional in the schema (or vice
|
|
70
|
+
// versa) is a real authoring drift worth surfacing.
|
|
71
|
+
if (projected.required !== undefined &&
|
|
72
|
+
Boolean(field.required) !== projected.required) {
|
|
73
|
+
mismatches.push({
|
|
74
|
+
fieldKey: field.key,
|
|
75
|
+
kind: "required-drift",
|
|
76
|
+
message: `field "${field.key}" required=${Boolean(field.required)} in node.json but required=${projected.required} in the schema.`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const key of Object.keys(schema)) {
|
|
81
|
+
if (fieldKeys.has(key))
|
|
82
|
+
continue;
|
|
83
|
+
// Only a REQUIRED schema key missing from node.json is a real gap — an
|
|
84
|
+
// optional input need not be an editable field.
|
|
85
|
+
if (schema[key]?.required === true) {
|
|
86
|
+
mismatches.push({
|
|
87
|
+
fieldKey: key,
|
|
88
|
+
kind: "missing-in-fields",
|
|
89
|
+
message: `the schema requires "${key}" but node.json declares no such field.`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return mismatches;
|
|
94
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative field-rule validators — the "field layer" the strict
|
|
3
|
+
* two-layer validation epic adds ahead of each node's imperative
|
|
4
|
+
* `validate?(config)` hook. Reads the catalog `ConfigField` declarations and
|
|
5
|
+
* checks a node's `data` against them: `required`, coarse value-type, enum
|
|
6
|
+
* membership (`select`), and `min` / `max` / `pattern` bounds.
|
|
7
|
+
*
|
|
8
|
+
* CONSERVATIVE by design — every check skips template values (their runtime
|
|
9
|
+
* value is unknown), skips hidden fields (`showWhen`), and fires only on a
|
|
10
|
+
* certain, unambiguous violation. The coarse type check deliberately covers
|
|
11
|
+
* only `number` / `boolean` fields (a `string` / `date` field accepts almost
|
|
12
|
+
* any stringifiable literal, so checking it would risk false positives).
|
|
13
|
+
*/
|
|
14
|
+
import type { WorkflowGraph } from "@flowget/types";
|
|
15
|
+
import type { CatalogIndex } from "./catalog.js";
|
|
16
|
+
import { type RawIssue } from "./issues.js";
|
|
17
|
+
/** Collect every field-rule issue across the graph. */
|
|
18
|
+
export declare function collectFieldRuleIssues(graph: WorkflowGraph, index: CatalogIndex): RawIssue[];
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative field-rule validators — the "field layer" the strict
|
|
3
|
+
* two-layer validation epic adds ahead of each node's imperative
|
|
4
|
+
* `validate?(config)` hook. Reads the catalog `ConfigField` declarations and
|
|
5
|
+
* checks a node's `data` against them: `required`, coarse value-type, enum
|
|
6
|
+
* membership (`select`), and `min` / `max` / `pattern` bounds.
|
|
7
|
+
*
|
|
8
|
+
* CONSERVATIVE by design — every check skips template values (their runtime
|
|
9
|
+
* value is unknown), skips hidden fields (`showWhen`), and fires only on a
|
|
10
|
+
* certain, unambiguous violation. The coarse type check deliberately covers
|
|
11
|
+
* only `number` / `boolean` fields (a `string` / `date` field accepts almost
|
|
12
|
+
* any stringifiable literal, so checking it would risk false positives).
|
|
13
|
+
*/
|
|
14
|
+
import { resolveFieldValueType } from "@flowget/types";
|
|
15
|
+
import { ISSUE_CODES } from "./issues.js";
|
|
16
|
+
import { hasTemplateExpression, parseTemplate } from "./template-ast.js";
|
|
17
|
+
/**
|
|
18
|
+
* Skip a `pattern` test on values longer than this. This BOUNDS the input
|
|
19
|
+
* length a `pattern` regex scans — it does NOT prevent catastrophic
|
|
20
|
+
* backtracking (an exponential author regex like `(a+)+$` blows up at ~30
|
|
21
|
+
* chars regardless of the cap). Backtracking-safety is an author concern; see
|
|
22
|
+
* {@link compilePattern}.
|
|
23
|
+
*/
|
|
24
|
+
const MAX_PATTERN_VALUE_LENGTH = 10_000;
|
|
25
|
+
/** Collect every field-rule issue across the graph. */
|
|
26
|
+
export function collectFieldRuleIssues(graph, index) {
|
|
27
|
+
const issues = [];
|
|
28
|
+
for (const node of graph.nodes) {
|
|
29
|
+
const def = index.get(node.type);
|
|
30
|
+
if (def?.fields === undefined)
|
|
31
|
+
continue; // unknown type / no fields — nothing to check
|
|
32
|
+
const data = (node.data ?? {});
|
|
33
|
+
for (const field of def.fields) {
|
|
34
|
+
if (!isFieldVisible(field, data))
|
|
35
|
+
continue;
|
|
36
|
+
validateField(field, data, node.id, issues);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return issues;
|
|
40
|
+
}
|
|
41
|
+
function validateField(field, data, nodeId, issues) {
|
|
42
|
+
const value = data[field.key];
|
|
43
|
+
if (field.required === true && isEmpty(value)) {
|
|
44
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_REQUIRED, nodeId, field, "is required."));
|
|
45
|
+
return; // nothing else to check on an empty value
|
|
46
|
+
}
|
|
47
|
+
if (isEmpty(value))
|
|
48
|
+
return;
|
|
49
|
+
// A template value resolves at runtime — its concrete value is unknown, so
|
|
50
|
+
// literal-value checks are skipped (reference validity is checked elsewhere).
|
|
51
|
+
if (typeof value === "string" && hasTemplateExpression(parseTemplate(value))) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const valueType = resolveFieldValueType(field);
|
|
55
|
+
// Coarse type check — only the unambiguous scalar widgets.
|
|
56
|
+
if (valueType === "number" && !isCoarseNumber(value)) {
|
|
57
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_TYPE, nodeId, field, "expects a number value."));
|
|
58
|
+
}
|
|
59
|
+
else if (valueType === "boolean" && !isCoarseBoolean(value)) {
|
|
60
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_TYPE, nodeId, field, "expects a boolean value."));
|
|
61
|
+
}
|
|
62
|
+
// Enum membership — `select` only (single-value). `multiselect` is skipped.
|
|
63
|
+
if (field.type === "select" && typeof value === "string") {
|
|
64
|
+
const options = resolveOptions(field, data);
|
|
65
|
+
if (options !== undefined && options.length > 0) {
|
|
66
|
+
const allowed = new Set(options.map((o) => o.value));
|
|
67
|
+
if (!allowed.has(value)) {
|
|
68
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_ENUM, nodeId, field, `value "${value}" is not one of the allowed options.`));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Range — only when the value coerces to a finite number.
|
|
73
|
+
const numeric = coerceNumber(value);
|
|
74
|
+
if (numeric !== undefined) {
|
|
75
|
+
if (field.min !== undefined && numeric < field.min) {
|
|
76
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_MIN, nodeId, field, `value ${numeric} is below the minimum ${field.min}.`));
|
|
77
|
+
}
|
|
78
|
+
if (field.max !== undefined && numeric > field.max) {
|
|
79
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_MAX, nodeId, field, `value ${numeric} is above the maximum ${field.max}.`));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Pattern — literal string values only, length-bounded.
|
|
83
|
+
if (field.pattern !== undefined &&
|
|
84
|
+
typeof value === "string" &&
|
|
85
|
+
value.length <= MAX_PATTERN_VALUE_LENGTH) {
|
|
86
|
+
const re = compilePattern(field.pattern);
|
|
87
|
+
if (re !== undefined && !re.test(value)) {
|
|
88
|
+
issues.push(fieldIssue(ISSUE_CODES.FIELD_PATTERN, nodeId, field, `value "${value}" does not match the required pattern.`));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// ── Predicates ──────────────────────────────────────────────
|
|
93
|
+
/** Empty ⟺ absent / null / empty-string / empty-array. `false` and `0` are NOT empty. */
|
|
94
|
+
function isEmpty(value) {
|
|
95
|
+
return (value === undefined ||
|
|
96
|
+
value === null ||
|
|
97
|
+
value === "" ||
|
|
98
|
+
(Array.isArray(value) && value.length === 0));
|
|
99
|
+
}
|
|
100
|
+
/** A finite number, or a non-blank numeric string. */
|
|
101
|
+
function isCoarseNumber(value) {
|
|
102
|
+
return coerceNumber(value) !== undefined;
|
|
103
|
+
}
|
|
104
|
+
/** A real boolean, or the literal string `"true"` / `"false"` (any case). */
|
|
105
|
+
function isCoarseBoolean(value) {
|
|
106
|
+
if (typeof value === "boolean")
|
|
107
|
+
return true;
|
|
108
|
+
if (typeof value === "string") {
|
|
109
|
+
const t = value.trim().toLowerCase();
|
|
110
|
+
return t === "true" || t === "false";
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
/** Coerce a number or non-blank numeric string to a finite number; else `undefined`. */
|
|
115
|
+
function coerceNumber(value) {
|
|
116
|
+
if (typeof value === "number")
|
|
117
|
+
return Number.isFinite(value) ? value : undefined;
|
|
118
|
+
if (typeof value === "string") {
|
|
119
|
+
const trimmed = value.trim();
|
|
120
|
+
if (trimmed === "")
|
|
121
|
+
return undefined;
|
|
122
|
+
const n = Number(trimmed);
|
|
123
|
+
return Number.isFinite(n) ? n : undefined;
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
/** Evaluate a field's `showWhen` visibility condition; absent condition → visible. */
|
|
128
|
+
function isFieldVisible(field, data) {
|
|
129
|
+
const cond = field.showWhen;
|
|
130
|
+
if (cond === undefined)
|
|
131
|
+
return true;
|
|
132
|
+
const other = data[cond.field];
|
|
133
|
+
if ("isSet" in cond) {
|
|
134
|
+
return other !== undefined && other !== null && other !== "";
|
|
135
|
+
}
|
|
136
|
+
return other === cond.equals;
|
|
137
|
+
}
|
|
138
|
+
/** Resolve a `select` field's options, invoking the factory form; `undefined` on any failure (permissive). */
|
|
139
|
+
function resolveOptions(field, data) {
|
|
140
|
+
const options = field.options;
|
|
141
|
+
if (options === undefined)
|
|
142
|
+
return undefined;
|
|
143
|
+
if (typeof options === "function") {
|
|
144
|
+
try {
|
|
145
|
+
return options(data);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return options;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Compile an author `pattern` source; `undefined` on an invalid regex
|
|
155
|
+
* (permissive).
|
|
156
|
+
*
|
|
157
|
+
* **Execution surface.** With this validator, an author `pattern` now runs
|
|
158
|
+
* host-side in the worker's pre-flight (a NEW execution surface beyond the
|
|
159
|
+
* browser builder), so catalog authors MUST keep `pattern`s linear /
|
|
160
|
+
* backtracking-safe (RE2-style — no nested quantifiers like `(a+)+`). There is
|
|
161
|
+
* deliberately NO runtime regex-safety guard here: `field.pattern` is TRUSTED
|
|
162
|
+
* catalog input (authored node.json), not customer-supplied JSON, so a guard
|
|
163
|
+
* would be over-engineering. The {@link MAX_PATTERN_VALUE_LENGTH} cap only
|
|
164
|
+
* bounds the scanned value length; it is not a backtracking defense.
|
|
165
|
+
*/
|
|
166
|
+
function compilePattern(pattern) {
|
|
167
|
+
try {
|
|
168
|
+
return new RegExp(pattern);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function fieldIssue(code, nodeId, field, detail) {
|
|
175
|
+
const label = field.label ?? field.key;
|
|
176
|
+
return {
|
|
177
|
+
code,
|
|
178
|
+
severity: "error",
|
|
179
|
+
nodeId,
|
|
180
|
+
fieldKey: field.key,
|
|
181
|
+
message: `Node "${nodeId}" field "${label}" ${detail}`,
|
|
182
|
+
};
|
|
183
|
+
}
|