json-renderer 0.0.1 → 0.3.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 +2 -2
- package/README.md +127 -1
- package/adapter.d.ts +91 -0
- package/adapter.js +7 -0
- package/bindings.d.ts +26 -0
- package/bindings.js +56 -0
- package/compose.d.ts +66 -0
- package/compose.js +120 -0
- package/constraints.d.ts +19 -0
- package/constraints.js +52 -0
- package/envelope.d.ts +59 -0
- package/envelope.js +28 -0
- package/esm/adapter.js +4 -0
- package/esm/bindings.js +50 -0
- package/esm/compose.js +113 -0
- package/esm/constraints.js +49 -0
- package/esm/envelope.js +23 -0
- package/esm/fields.js +43 -0
- package/esm/index.js +10 -0
- package/esm/json-schema.js +15 -0
- package/esm/node.js +54 -0
- package/esm/registry.js +35 -0
- package/esm/zod.js +85 -0
- package/fields.d.ts +20 -0
- package/fields.js +48 -0
- package/index.d.ts +10 -0
- package/index.js +26 -0
- package/json-schema.d.ts +8 -0
- package/json-schema.js +20 -0
- package/node.d.ts +60 -0
- package/node.js +62 -0
- package/package.json +36 -64
- package/registry.d.ts +18 -0
- package/registry.js +41 -0
- package/zod.d.ts +74 -0
- package/zod.js +93 -0
- package/main/index.js +0 -41
- package/module/index.js +0 -39
- package/src/index.ts +0 -42
- package/types/index.d.ts +0 -6
package/node.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The node tree model: a document is a tree of typed nodes, and a node's `type`
|
|
3
|
+
* is a plain string resolved by an adapter's registry. The vocabulary is a
|
|
4
|
+
* parameter, never a fixed list — a host that renders `Input`/`DataTable` and a
|
|
5
|
+
* host that renders `chart.line` share this model.
|
|
6
|
+
*/
|
|
7
|
+
/** Node props are open: an adapter reads what its components need. */
|
|
8
|
+
export interface NodeProps {
|
|
9
|
+
[propName: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
/** Prop name → template expression, e.g. `{ label: '{{ row.title }}' }`. */
|
|
12
|
+
export interface NodeBindings {
|
|
13
|
+
[propName: string]: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A declarative reference to behaviour that lives outside the document. Keeping
|
|
17
|
+
* actions declarative is what stops the format becoming a programming language.
|
|
18
|
+
*/
|
|
19
|
+
export interface NodeAction {
|
|
20
|
+
type: string;
|
|
21
|
+
flowId?: string;
|
|
22
|
+
handler?: string;
|
|
23
|
+
inputMapping?: Record<string, string>;
|
|
24
|
+
params?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
/** Event name → action, e.g. `{ submit: { type: 'flow', flowId } }`. */
|
|
27
|
+
export interface NodeActions {
|
|
28
|
+
[eventName: string]: NodeAction;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A node in the document tree, generic over the node vocabulary (`TType`) and
|
|
32
|
+
* the props shape (`TProps`) a vocabulary declares.
|
|
33
|
+
*/
|
|
34
|
+
export interface DocumentNode<TType extends string = string, TProps extends NodeProps = NodeProps> {
|
|
35
|
+
type: TType;
|
|
36
|
+
key: string;
|
|
37
|
+
props: TProps;
|
|
38
|
+
children: DocumentNode<TType, TProps>[];
|
|
39
|
+
bindings?: NodeBindings;
|
|
40
|
+
actions?: NodeActions;
|
|
41
|
+
}
|
|
42
|
+
/** Any node tree, whatever its vocabulary. */
|
|
43
|
+
export type AnyDocumentNode = DocumentNode<string, NodeProps>;
|
|
44
|
+
/** Depth-first walk over a node and its descendants. */
|
|
45
|
+
export declare function walkNodes<TNode extends AnyDocumentNode>(node: TNode): Generator<TNode>;
|
|
46
|
+
export declare function findNodeByKey<TNode extends AnyDocumentNode>(node: TNode, key: string): TNode | undefined;
|
|
47
|
+
export declare function collectNodes<TNode extends AnyDocumentNode>(node: TNode, predicate: (node: TNode) => boolean): TNode[];
|
|
48
|
+
/**
|
|
49
|
+
* Rewrite a tree bottom-up. Pure: the input node is never mutated, so a
|
|
50
|
+
* transform is safe to run on a document that is also being rendered.
|
|
51
|
+
*/
|
|
52
|
+
export declare function mapNodes<TNode extends AnyDocumentNode>(node: TNode, transform: (node: TNode) => TNode): TNode;
|
|
53
|
+
/** Node types used anywhere in a tree, sorted for stable output. */
|
|
54
|
+
export declare function collectNodeTypes(node: AnyDocumentNode): string[];
|
|
55
|
+
export declare function createNode<TType extends string = string, TProps extends NodeProps = NodeProps>(type: TType, key: string, options?: {
|
|
56
|
+
props?: TProps;
|
|
57
|
+
children?: DocumentNode<TType, TProps>[];
|
|
58
|
+
bindings?: NodeBindings;
|
|
59
|
+
actions?: NodeActions;
|
|
60
|
+
}): DocumentNode<TType, TProps>;
|
package/node.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The node tree model: a document is a tree of typed nodes, and a node's `type`
|
|
4
|
+
* is a plain string resolved by an adapter's registry. The vocabulary is a
|
|
5
|
+
* parameter, never a fixed list — a host that renders `Input`/`DataTable` and a
|
|
6
|
+
* host that renders `chart.line` share this model.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.walkNodes = walkNodes;
|
|
10
|
+
exports.findNodeByKey = findNodeByKey;
|
|
11
|
+
exports.collectNodes = collectNodes;
|
|
12
|
+
exports.mapNodes = mapNodes;
|
|
13
|
+
exports.collectNodeTypes = collectNodeTypes;
|
|
14
|
+
exports.createNode = createNode;
|
|
15
|
+
/** Depth-first walk over a node and its descendants. */
|
|
16
|
+
function* walkNodes(node) {
|
|
17
|
+
yield node;
|
|
18
|
+
for (const child of (node.children ?? [])) {
|
|
19
|
+
yield* walkNodes(child);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function findNodeByKey(node, key) {
|
|
23
|
+
for (const current of walkNodes(node)) {
|
|
24
|
+
if (current.key === key)
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
function collectNodes(node, predicate) {
|
|
30
|
+
const matches = [];
|
|
31
|
+
for (const current of walkNodes(node)) {
|
|
32
|
+
if (predicate(current))
|
|
33
|
+
matches.push(current);
|
|
34
|
+
}
|
|
35
|
+
return matches;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Rewrite a tree bottom-up. Pure: the input node is never mutated, so a
|
|
39
|
+
* transform is safe to run on a document that is also being rendered.
|
|
40
|
+
*/
|
|
41
|
+
function mapNodes(node, transform) {
|
|
42
|
+
const children = (node.children ?? []).map((child) => mapNodes(child, transform));
|
|
43
|
+
return transform({ ...node, children });
|
|
44
|
+
}
|
|
45
|
+
/** Node types used anywhere in a tree, sorted for stable output. */
|
|
46
|
+
function collectNodeTypes(node) {
|
|
47
|
+
const types = new Set();
|
|
48
|
+
for (const current of walkNodes(node)) {
|
|
49
|
+
types.add(current.type);
|
|
50
|
+
}
|
|
51
|
+
return [...types].sort();
|
|
52
|
+
}
|
|
53
|
+
function createNode(type, key, options = {}) {
|
|
54
|
+
return {
|
|
55
|
+
type,
|
|
56
|
+
key,
|
|
57
|
+
props: options.props ?? {},
|
|
58
|
+
children: options.children ?? [],
|
|
59
|
+
...(options.bindings ? { bindings: options.bindings } : {}),
|
|
60
|
+
...(options.actions ? { actions: options.actions } : {}),
|
|
61
|
+
};
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,73 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-renderer",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"lib": "src",
|
|
13
|
-
"test": "__tests__"
|
|
14
|
-
},
|
|
15
|
-
"files": [
|
|
16
|
-
"types",
|
|
17
|
-
"module",
|
|
18
|
-
"src",
|
|
19
|
-
"main"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build:main": "yarn tsc -p tsconfig.json --outDir main --module commonjs",
|
|
23
|
-
"build:module": "yarn tsc -p tsconfig.json --outDir module --module es2022",
|
|
24
|
-
"build": "npm run build:module && npm run build:main",
|
|
25
|
-
"clean": "rimraf ./types && rimraf ./main && rimraf ./module",
|
|
26
|
-
"prepare": "npm run clean && npm run build",
|
|
27
|
-
"lint": "eslint .",
|
|
28
|
-
"format": "eslint . --fix",
|
|
29
|
-
"test": "jest",
|
|
30
|
-
"test:watch": "jest --watch",
|
|
31
|
-
"test:debug": "node --inspect node_modules/.bin/jest --runInBand"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Framework-agnostic JSON UI document core: envelope and node tree, validation, composition, binding resolution, and the renderer adapter contract",
|
|
5
|
+
"private": false,
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://constructive-io.github.io/blocks/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/constructive-io/blocks.git",
|
|
11
|
+
"directory": "packages/json-renderer"
|
|
32
12
|
},
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"testEnvironment": "node",
|
|
36
|
-
"transform": {
|
|
37
|
-
"^.+\\.ts?$": "ts-jest"
|
|
38
|
-
},
|
|
39
|
-
"transformIgnorePatterns": [
|
|
40
|
-
"<rootDir>/node_modules/"
|
|
41
|
-
],
|
|
42
|
-
"testPathIgnorePatterns": [
|
|
43
|
-
"main/",
|
|
44
|
-
"module/",
|
|
45
|
-
"types/"
|
|
46
|
-
]
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/constructive-io/blocks/issues"
|
|
47
15
|
},
|
|
48
16
|
"publishConfig": {
|
|
49
|
-
"access": "public"
|
|
17
|
+
"access": "public",
|
|
18
|
+
"directory": "dist"
|
|
50
19
|
},
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"types": "index.d.ts",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"prepack": "npm run build",
|
|
28
|
+
"lint:types": "tsc --noEmit -p tsconfig.lint.json",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"clean": "makage clean"
|
|
54
32
|
},
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
"url": "https://github.com/constructive-io/json-renderer/issues"
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"zod": "^4.3.4"
|
|
58
35
|
},
|
|
59
36
|
"devDependencies": {
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"ts-jest": "^29.1.0",
|
|
70
|
-
"ts-node": "10.9.2",
|
|
71
|
-
"typescript": "^5.0.4"
|
|
72
|
-
}
|
|
73
|
-
}
|
|
37
|
+
"makage": "^0.6.0",
|
|
38
|
+
"typescript": "^5.9.3",
|
|
39
|
+
"vitest": "^3.2.4"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=24.0.0"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "8321f44d0cc91bb6885f3c843ef8467ebdccec40"
|
|
45
|
+
}
|
package/registry.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry resolution: node type → whatever an adapter renders with (a React
|
|
3
|
+
* component, a template function, an HTML serializer). The core owns the
|
|
4
|
+
* layering rules; the handler type is the adapter's business.
|
|
5
|
+
*/
|
|
6
|
+
/** Node type → handler. Layered by {@link composeRegistry}. */
|
|
7
|
+
export type NodeRegistry<THandler> = Record<string, THandler>;
|
|
8
|
+
/**
|
|
9
|
+
* Layer registries left-to-right, later layers winning. This is how a host
|
|
10
|
+
* customizes rendering: base primitives, then an app registry, then per-document
|
|
11
|
+
* overrides — no forking of the renderer, and no single global map.
|
|
12
|
+
*/
|
|
13
|
+
export declare function composeRegistry<THandler>(...layers: (NodeRegistry<THandler> | undefined)[]): NodeRegistry<THandler>;
|
|
14
|
+
export declare function resolveHandler<THandler>(registry: NodeRegistry<THandler>, type: string): THandler | undefined;
|
|
15
|
+
/** Node types the registry can render, sorted for stable output. */
|
|
16
|
+
export declare function registeredTypes<THandler>(registry: NodeRegistry<THandler>): string[];
|
|
17
|
+
/** Node types a document uses that no registry layer satisfies. */
|
|
18
|
+
export declare function missingTypes<THandler>(registry: NodeRegistry<THandler>, usedTypes: Iterable<string>): string[];
|
package/registry.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Registry resolution: node type → whatever an adapter renders with (a React
|
|
4
|
+
* component, a template function, an HTML serializer). The core owns the
|
|
5
|
+
* layering rules; the handler type is the adapter's business.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.composeRegistry = composeRegistry;
|
|
9
|
+
exports.resolveHandler = resolveHandler;
|
|
10
|
+
exports.registeredTypes = registeredTypes;
|
|
11
|
+
exports.missingTypes = missingTypes;
|
|
12
|
+
/**
|
|
13
|
+
* Layer registries left-to-right, later layers winning. This is how a host
|
|
14
|
+
* customizes rendering: base primitives, then an app registry, then per-document
|
|
15
|
+
* overrides — no forking of the renderer, and no single global map.
|
|
16
|
+
*/
|
|
17
|
+
function composeRegistry(...layers) {
|
|
18
|
+
const composed = {};
|
|
19
|
+
for (const layer of layers) {
|
|
20
|
+
if (!layer)
|
|
21
|
+
continue;
|
|
22
|
+
Object.assign(composed, layer);
|
|
23
|
+
}
|
|
24
|
+
return composed;
|
|
25
|
+
}
|
|
26
|
+
function resolveHandler(registry, type) {
|
|
27
|
+
return registry[type];
|
|
28
|
+
}
|
|
29
|
+
/** Node types the registry can render, sorted for stable output. */
|
|
30
|
+
function registeredTypes(registry) {
|
|
31
|
+
return Object.keys(registry).sort();
|
|
32
|
+
}
|
|
33
|
+
/** Node types a document uses that no registry layer satisfies. */
|
|
34
|
+
function missingTypes(registry, usedTypes) {
|
|
35
|
+
const missing = new Set();
|
|
36
|
+
for (const type of usedTypes) {
|
|
37
|
+
if (!(type in registry))
|
|
38
|
+
missing.add(type);
|
|
39
|
+
}
|
|
40
|
+
return [...missing].sort();
|
|
41
|
+
}
|
package/zod.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime validation for the generic envelope. Concrete formats build their own
|
|
3
|
+
* schemas from these factories, so a vocabulary narrows props, actions, or node
|
|
4
|
+
* types without restating the tree rules.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import type { AnyDocumentEnvelope, EnvelopeKind } from './envelope';
|
|
8
|
+
import type { AnyDocumentNode } from './node';
|
|
9
|
+
export declare const nodeConstraintsSchema: z.ZodObject<{
|
|
10
|
+
minLength: z.ZodOptional<z.ZodNumber>;
|
|
11
|
+
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
12
|
+
minValue: z.ZodOptional<z.ZodNumber>;
|
|
13
|
+
maxValue: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
pattern: z.ZodOptional<z.ZodString>;
|
|
15
|
+
precision: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
scale: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
}, z.core.$strip>;
|
|
18
|
+
/** Props are open by default: a vocabulary extends this with what it knows. */
|
|
19
|
+
export declare const nodePropsSchema: z.ZodObject<{}, z.core.$loose>;
|
|
20
|
+
export declare const nodeBindingsSchema: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
21
|
+
export declare const nodeActionSchema: z.ZodObject<{
|
|
22
|
+
type: z.ZodString;
|
|
23
|
+
flowId: z.ZodOptional<z.ZodString>;
|
|
24
|
+
handler: z.ZodOptional<z.ZodString>;
|
|
25
|
+
inputMapping: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
26
|
+
params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
27
|
+
}, z.core.$strip>;
|
|
28
|
+
export declare const nodeActionsSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
29
|
+
type: z.ZodString;
|
|
30
|
+
flowId: z.ZodOptional<z.ZodString>;
|
|
31
|
+
handler: z.ZodOptional<z.ZodString>;
|
|
32
|
+
inputMapping: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
33
|
+
params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
34
|
+
}, z.core.$strip>>;
|
|
35
|
+
export declare const registrySourceSchema: z.ZodObject<{
|
|
36
|
+
name: z.ZodString;
|
|
37
|
+
url: z.ZodString;
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
export declare const dataSourceSchema: z.ZodObject<{
|
|
40
|
+
name: z.ZodString;
|
|
41
|
+
query: z.ZodOptional<z.ZodString>;
|
|
42
|
+
variables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
43
|
+
}, z.core.$loose>;
|
|
44
|
+
export declare const documentMetadataSchema: z.ZodObject<{
|
|
45
|
+
title: z.ZodOptional<z.ZodString>;
|
|
46
|
+
description: z.ZodOptional<z.ZodString>;
|
|
47
|
+
}, z.core.$loose>;
|
|
48
|
+
export interface NodeSchemaOptions {
|
|
49
|
+
/** Narrow the node vocabulary; unknown strings stay valid by default. */
|
|
50
|
+
typeSchema?: z.ZodType<string>;
|
|
51
|
+
propsSchema?: z.ZodType<Record<string, unknown>>;
|
|
52
|
+
actionsSchema?: z.ZodType<Record<string, unknown>>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build the recursive node schema. Unknown node types pass by default: a
|
|
56
|
+
* registry may satisfy types this package has never heard of, and rejecting them
|
|
57
|
+
* here would make the format closed.
|
|
58
|
+
*/
|
|
59
|
+
export declare function createNodeSchema<TNode extends AnyDocumentNode = AnyDocumentNode>(options?: NodeSchemaOptions): z.ZodType<TNode>;
|
|
60
|
+
export declare const documentNodeSchema: z.ZodType<AnyDocumentNode>;
|
|
61
|
+
export interface DocumentSchemaOptions<TNode extends AnyDocumentNode = AnyDocumentNode> extends NodeSchemaOptions {
|
|
62
|
+
/** Pin the envelope discriminator and version, e.g. `UISchema` / `1.0`. */
|
|
63
|
+
kind?: Partial<EnvelopeKind>;
|
|
64
|
+
nodeSchema?: z.ZodType<TNode>;
|
|
65
|
+
}
|
|
66
|
+
/** Build the envelope schema around a node schema. */
|
|
67
|
+
export declare function createDocumentSchema<TDocument extends AnyDocumentEnvelope = AnyDocumentEnvelope, TNode extends AnyDocumentNode = AnyDocumentNode>(options?: DocumentSchemaOptions<TNode>): z.ZodType<TDocument>;
|
|
68
|
+
export declare const documentEnvelopeSchema: z.ZodType<AnyDocumentEnvelope>;
|
|
69
|
+
/** Throws a `ZodError` describing every problem in the document. */
|
|
70
|
+
export declare function parseEnvelope(value: unknown): AnyDocumentEnvelope;
|
|
71
|
+
export declare function safeParseEnvelope(value: unknown): z.ZodSafeParseResult<AnyDocumentEnvelope>;
|
|
72
|
+
export declare function parseNode(value: unknown): AnyDocumentNode;
|
|
73
|
+
/** The version this package's generic envelope schema accepts by default. */
|
|
74
|
+
export declare const SCHEMA_FORMAT_VERSION = "1.0";
|
package/zod.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SCHEMA_FORMAT_VERSION = exports.documentEnvelopeSchema = exports.documentNodeSchema = exports.documentMetadataSchema = exports.dataSourceSchema = exports.registrySourceSchema = exports.nodeActionsSchema = exports.nodeActionSchema = exports.nodeBindingsSchema = exports.nodePropsSchema = exports.nodeConstraintsSchema = void 0;
|
|
4
|
+
exports.createNodeSchema = createNodeSchema;
|
|
5
|
+
exports.createDocumentSchema = createDocumentSchema;
|
|
6
|
+
exports.parseEnvelope = parseEnvelope;
|
|
7
|
+
exports.safeParseEnvelope = safeParseEnvelope;
|
|
8
|
+
exports.parseNode = parseNode;
|
|
9
|
+
/**
|
|
10
|
+
* Runtime validation for the generic envelope. Concrete formats build their own
|
|
11
|
+
* schemas from these factories, so a vocabulary narrows props, actions, or node
|
|
12
|
+
* types without restating the tree rules.
|
|
13
|
+
*/
|
|
14
|
+
const zod_1 = require("zod");
|
|
15
|
+
const envelope_1 = require("./envelope");
|
|
16
|
+
exports.nodeConstraintsSchema = zod_1.z.object({
|
|
17
|
+
minLength: zod_1.z.number().int().nonnegative().optional(),
|
|
18
|
+
maxLength: zod_1.z.number().int().nonnegative().optional(),
|
|
19
|
+
minValue: zod_1.z.number().optional(),
|
|
20
|
+
maxValue: zod_1.z.number().optional(),
|
|
21
|
+
pattern: zod_1.z.string().optional(),
|
|
22
|
+
precision: zod_1.z.number().int().nonnegative().optional(),
|
|
23
|
+
scale: zod_1.z.number().int().nonnegative().optional(),
|
|
24
|
+
});
|
|
25
|
+
/** Props are open by default: a vocabulary extends this with what it knows. */
|
|
26
|
+
exports.nodePropsSchema = zod_1.z.looseObject({});
|
|
27
|
+
exports.nodeBindingsSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.string());
|
|
28
|
+
exports.nodeActionSchema = zod_1.z.object({
|
|
29
|
+
type: zod_1.z.string().min(1),
|
|
30
|
+
flowId: zod_1.z.string().optional(),
|
|
31
|
+
handler: zod_1.z.string().optional(),
|
|
32
|
+
inputMapping: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
|
|
33
|
+
params: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
|
|
34
|
+
});
|
|
35
|
+
exports.nodeActionsSchema = zod_1.z.record(zod_1.z.string(), exports.nodeActionSchema);
|
|
36
|
+
exports.registrySourceSchema = zod_1.z.object({
|
|
37
|
+
name: zod_1.z.string().min(1),
|
|
38
|
+
url: zod_1.z.string().min(1),
|
|
39
|
+
});
|
|
40
|
+
exports.dataSourceSchema = zod_1.z.looseObject({
|
|
41
|
+
name: zod_1.z.string().min(1),
|
|
42
|
+
query: zod_1.z.string().optional(),
|
|
43
|
+
variables: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
|
|
44
|
+
});
|
|
45
|
+
exports.documentMetadataSchema = zod_1.z.looseObject({
|
|
46
|
+
title: zod_1.z.string().optional(),
|
|
47
|
+
description: zod_1.z.string().optional(),
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* Build the recursive node schema. Unknown node types pass by default: a
|
|
51
|
+
* registry may satisfy types this package has never heard of, and rejecting them
|
|
52
|
+
* here would make the format closed.
|
|
53
|
+
*/
|
|
54
|
+
function createNodeSchema(options = {}) {
|
|
55
|
+
const schema = zod_1.z.lazy(() => zod_1.z.object({
|
|
56
|
+
type: options.typeSchema ?? zod_1.z.string().min(1),
|
|
57
|
+
key: zod_1.z.string().min(1),
|
|
58
|
+
props: (options.propsSchema ?? exports.nodePropsSchema).default({}),
|
|
59
|
+
children: zod_1.z.array(schema).default([]),
|
|
60
|
+
bindings: exports.nodeBindingsSchema.optional(),
|
|
61
|
+
actions: (options.actionsSchema ?? exports.nodeActionsSchema).optional(),
|
|
62
|
+
}));
|
|
63
|
+
return schema;
|
|
64
|
+
}
|
|
65
|
+
exports.documentNodeSchema = createNodeSchema();
|
|
66
|
+
/** Build the envelope schema around a node schema. */
|
|
67
|
+
function createDocumentSchema(options = {}) {
|
|
68
|
+
const nodeSchema = options.nodeSchema ?? createNodeSchema(options);
|
|
69
|
+
return zod_1.z.object({
|
|
70
|
+
formatVersion: options.kind?.formatVersion
|
|
71
|
+
? zod_1.z.literal(options.kind.formatVersion)
|
|
72
|
+
: zod_1.z.string().min(1),
|
|
73
|
+
type: options.kind?.documentType ? zod_1.z.literal(options.kind.documentType) : zod_1.z.string().min(1),
|
|
74
|
+
id: zod_1.z.string().min(1),
|
|
75
|
+
meta: exports.documentMetadataSchema.optional(),
|
|
76
|
+
registries: zod_1.z.array(exports.registrySourceSchema).optional(),
|
|
77
|
+
dataSources: zod_1.z.array(exports.dataSourceSchema).optional(),
|
|
78
|
+
page: nodeSchema,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
exports.documentEnvelopeSchema = createDocumentSchema();
|
|
82
|
+
/** Throws a `ZodError` describing every problem in the document. */
|
|
83
|
+
function parseEnvelope(value) {
|
|
84
|
+
return exports.documentEnvelopeSchema.parse(value);
|
|
85
|
+
}
|
|
86
|
+
function safeParseEnvelope(value) {
|
|
87
|
+
return exports.documentEnvelopeSchema.safeParse(value);
|
|
88
|
+
}
|
|
89
|
+
function parseNode(value) {
|
|
90
|
+
return exports.documentNodeSchema.parse(value);
|
|
91
|
+
}
|
|
92
|
+
/** The version this package's generic envelope schema accepts by default. */
|
|
93
|
+
exports.SCHEMA_FORMAT_VERSION = envelope_1.DOCUMENT_FORMAT_VERSION;
|
package/main/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = {
|
|
4
|
-
get(obj, path) {
|
|
5
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
6
|
-
let result = obj;
|
|
7
|
-
for (const key of keys) {
|
|
8
|
-
if (result == null) {
|
|
9
|
-
return undefined;
|
|
10
|
-
}
|
|
11
|
-
result = result[key];
|
|
12
|
-
}
|
|
13
|
-
return result;
|
|
14
|
-
},
|
|
15
|
-
set(obj, path, value) {
|
|
16
|
-
if (value === undefined) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
20
|
-
let current = obj;
|
|
21
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
22
|
-
const key = keys[i];
|
|
23
|
-
if (typeof current[key] !== 'object') {
|
|
24
|
-
current[key] = {};
|
|
25
|
-
}
|
|
26
|
-
current = current[key];
|
|
27
|
-
}
|
|
28
|
-
current[keys[keys.length - 1]] = value;
|
|
29
|
-
},
|
|
30
|
-
has(obj, path) {
|
|
31
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
32
|
-
let current = obj;
|
|
33
|
-
for (const key of keys) {
|
|
34
|
-
if (current == null || !(key in current)) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
current = current[key];
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
};
|
package/module/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
get(obj, path) {
|
|
3
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
4
|
-
let result = obj;
|
|
5
|
-
for (const key of keys) {
|
|
6
|
-
if (result == null) {
|
|
7
|
-
return undefined;
|
|
8
|
-
}
|
|
9
|
-
result = result[key];
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
12
|
-
},
|
|
13
|
-
set(obj, path, value) {
|
|
14
|
-
if (value === undefined) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
18
|
-
let current = obj;
|
|
19
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
20
|
-
const key = keys[i];
|
|
21
|
-
if (typeof current[key] !== 'object') {
|
|
22
|
-
current[key] = {};
|
|
23
|
-
}
|
|
24
|
-
current = current[key];
|
|
25
|
-
}
|
|
26
|
-
current[keys[keys.length - 1]] = value;
|
|
27
|
-
},
|
|
28
|
-
has(obj, path) {
|
|
29
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
30
|
-
let current = obj;
|
|
31
|
-
for (const key of keys) {
|
|
32
|
-
if (current == null || !(key in current)) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
current = current[key];
|
|
36
|
-
}
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
get<T>(obj: Record<string, any>, path: string): T | undefined {
|
|
3
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
4
|
-
let result: any = obj;
|
|
5
|
-
for (const key of keys) {
|
|
6
|
-
if (result == null) {
|
|
7
|
-
return undefined;
|
|
8
|
-
}
|
|
9
|
-
result = result[key];
|
|
10
|
-
}
|
|
11
|
-
return result as T;
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
set(obj: Record<string, any>, path: string, value: any): void {
|
|
15
|
-
if (value === undefined) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
20
|
-
let current = obj;
|
|
21
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
22
|
-
const key = keys[i];
|
|
23
|
-
if (typeof current[key] !== 'object') {
|
|
24
|
-
current[key] = {};
|
|
25
|
-
}
|
|
26
|
-
current = current[key];
|
|
27
|
-
}
|
|
28
|
-
current[keys[keys.length - 1]] = value;
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
has(obj: Record<string, any>, path: string): boolean {
|
|
32
|
-
const keys = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
33
|
-
let current = obj;
|
|
34
|
-
for (const key of keys) {
|
|
35
|
-
if (current == null || !(key in current)) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
current = current[key];
|
|
39
|
-
}
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
};
|
package/types/index.d.ts
DELETED