@rippling/rippling-sdk 0.2.0-alpha.107 → 0.2.0-alpha.109
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/client.d.mts +10 -1
- package/client.d.mts.map +1 -1
- package/client.d.ts +10 -1
- package/client.d.ts.map +1 -1
- package/client.js +10 -1
- package/client.js.map +1 -1
- package/client.mjs +10 -1
- package/client.mjs.map +1 -1
- package/lib/charts/builders.d.mts +18 -19
- package/lib/charts/builders.d.mts.map +1 -1
- package/lib/charts/builders.d.ts +18 -19
- package/lib/charts/builders.d.ts.map +1 -1
- package/lib/charts/builders.js +35 -28
- package/lib/charts/builders.js.map +1 -1
- package/lib/charts/builders.mjs +35 -28
- package/lib/charts/builders.mjs.map +1 -1
- package/lib/charts/describe.d.mts +6 -7
- package/lib/charts/describe.d.mts.map +1 -1
- package/lib/charts/describe.d.ts +6 -7
- package/lib/charts/describe.d.ts.map +1 -1
- package/lib/charts/describe.js +26 -27
- package/lib/charts/describe.js.map +1 -1
- package/lib/charts/describe.mjs +27 -28
- package/lib/charts/describe.mjs.map +1 -1
- package/lib/charts/errors.d.mts +2 -6
- package/lib/charts/errors.d.mts.map +1 -1
- package/lib/charts/errors.d.ts +2 -6
- package/lib/charts/errors.d.ts.map +1 -1
- package/lib/charts/errors.js +2 -6
- package/lib/charts/errors.js.map +1 -1
- package/lib/charts/errors.mjs +2 -6
- package/lib/charts/errors.mjs.map +1 -1
- package/lib/charts/evaluate.d.mts +5 -7
- package/lib/charts/evaluate.d.mts.map +1 -1
- package/lib/charts/evaluate.d.ts +5 -7
- package/lib/charts/evaluate.d.ts.map +1 -1
- package/lib/charts/evaluate.js +21 -35
- package/lib/charts/evaluate.js.map +1 -1
- package/lib/charts/evaluate.mjs +22 -36
- package/lib/charts/evaluate.mjs.map +1 -1
- package/lib/charts/schema.d.mts +8 -97
- package/lib/charts/schema.d.mts.map +1 -1
- package/lib/charts/schema.d.ts +8 -97
- package/lib/charts/schema.d.ts.map +1 -1
- package/lib/charts/schema.js +186 -65
- package/lib/charts/schema.js.map +1 -1
- package/lib/charts/schema.mjs +185 -65
- package/lib/charts/schema.mjs.map +1 -1
- package/lib/charts/types.d.mts +40 -25
- package/lib/charts/types.d.mts.map +1 -1
- package/lib/charts/types.d.ts +40 -25
- package/lib/charts/types.d.ts.map +1 -1
- package/lib/charts/types.js +8 -10
- package/lib/charts/types.js.map +1 -1
- package/lib/charts/types.mjs +8 -10
- package/lib/charts/types.mjs.map +1 -1
- package/lib/charts/visualization-grammar.d.mts +66 -0
- package/lib/charts/visualization-grammar.d.mts.map +1 -0
- package/lib/charts/visualization-grammar.d.ts +66 -0
- package/lib/charts/visualization-grammar.d.ts.map +1 -0
- package/lib/charts/visualization-grammar.js +70 -0
- package/lib/charts/visualization-grammar.js.map +1 -0
- package/lib/charts/visualization-grammar.mjs +67 -0
- package/lib/charts/visualization-grammar.mjs.map +1 -0
- package/lib/charts/vocabulary.d.mts +7 -0
- package/lib/charts/vocabulary.d.mts.map +1 -0
- package/lib/charts/vocabulary.d.ts +7 -0
- package/lib/charts/vocabulary.d.ts.map +1 -0
- package/lib/charts/vocabulary.js +10 -0
- package/lib/charts/vocabulary.js.map +1 -0
- package/lib/charts/vocabulary.mjs +7 -0
- package/lib/charts/vocabulary.mjs.map +1 -0
- package/lib/charts.d.mts +3 -3
- package/lib/charts.d.mts.map +1 -1
- package/lib/charts.d.ts +3 -3
- package/lib/charts.d.ts.map +1 -1
- package/lib/charts.js +2 -1
- package/lib/charts.js.map +1 -1
- package/lib/charts.mjs +1 -1
- package/lib/charts.mjs.map +1 -1
- package/package.json +4 -2
- package/resources/card-transactions.d.mts +579 -0
- package/resources/card-transactions.d.mts.map +1 -0
- package/resources/card-transactions.d.ts +579 -0
- package/resources/card-transactions.d.ts.map +1 -0
- package/resources/card-transactions.js +116 -0
- package/resources/card-transactions.js.map +1 -0
- package/resources/card-transactions.mjs +112 -0
- package/resources/card-transactions.mjs.map +1 -0
- package/resources/earning-types.d.mts +50 -10
- package/resources/earning-types.d.mts.map +1 -1
- package/resources/earning-types.d.ts +50 -10
- package/resources/earning-types.d.ts.map +1 -1
- package/resources/earning-types.js +21 -2
- package/resources/earning-types.js.map +1 -1
- package/resources/earning-types.mjs +21 -2
- package/resources/earning-types.mjs.map +1 -1
- package/resources/index.d.mts +4 -1
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +4 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +10 -4
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +4 -1
- package/resources/index.mjs.map +1 -1
- package/resources/reimbursements.d.mts +954 -0
- package/resources/reimbursements.d.mts.map +1 -0
- package/resources/reimbursements.d.ts +954 -0
- package/resources/reimbursements.d.ts.map +1 -0
- package/resources/reimbursements.js +140 -0
- package/resources/reimbursements.js.map +1 -0
- package/resources/reimbursements.mjs +136 -0
- package/resources/reimbursements.mjs.map +1 -0
- package/resources/spend-categories.d.mts +140 -0
- package/resources/spend-categories.d.mts.map +1 -0
- package/resources/spend-categories.d.ts +140 -0
- package/resources/spend-categories.d.ts.map +1 -0
- package/resources/spend-categories.js +83 -0
- package/resources/spend-categories.js.map +1 -0
- package/resources/spend-categories.mjs +79 -0
- package/resources/spend-categories.mjs.map +1 -0
- package/resources/workflows/executions.d.mts +153 -0
- package/resources/workflows/executions.d.mts.map +1 -0
- package/resources/workflows/executions.d.ts +153 -0
- package/resources/workflows/executions.d.ts.map +1 -0
- package/resources/workflows/executions.js +74 -0
- package/resources/workflows/executions.js.map +1 -0
- package/resources/workflows/executions.mjs +70 -0
- package/resources/workflows/executions.mjs.map +1 -0
- package/resources/workflows/index.d.mts +3 -0
- package/resources/workflows/index.d.mts.map +1 -0
- package/resources/workflows/index.d.ts +3 -0
- package/resources/workflows/index.d.ts.map +1 -0
- package/resources/workflows/index.js +9 -0
- package/resources/workflows/index.js.map +1 -0
- package/resources/workflows/index.mjs +4 -0
- package/resources/workflows/index.mjs.map +1 -0
- package/resources/workflows/workflows.d.mts +977 -0
- package/resources/workflows/workflows.d.mts.map +1 -0
- package/resources/workflows/workflows.d.ts +977 -0
- package/resources/workflows/workflows.d.ts.map +1 -0
- package/resources/workflows/workflows.js +246 -0
- package/resources/workflows/workflows.js.map +1 -0
- package/resources/workflows/workflows.mjs +241 -0
- package/resources/workflows/workflows.mjs.map +1 -0
- package/resources/workflows.d.mts +1 -972
- package/resources/workflows.d.mts.map +1 -1
- package/resources/workflows.d.ts +1 -972
- package/resources/workflows.d.ts.map +1 -1
- package/resources/workflows.js +2 -234
- package/resources/workflows.js.map +1 -1
- package/resources/workflows.mjs +1 -232
- package/resources/workflows.mjs.map +1 -1
- package/src/client.ts +63 -12
- package/src/lib/charts/README.md +35 -28
- package/src/lib/charts/agent-reference.md +73 -0
- package/src/lib/charts/builders.ts +51 -35
- package/src/lib/charts/capabilities.json +96 -0
- package/src/lib/charts/describe.ts +31 -29
- package/src/lib/charts/errors.ts +2 -6
- package/src/lib/charts/evaluate.ts +25 -38
- package/src/lib/charts/schema.ts +232 -66
- package/src/lib/charts/types.ts +42 -31
- package/src/lib/charts/visualization-grammar.ts +67 -0
- package/src/lib/charts/vocabulary.ts +7 -0
- package/src/lib/charts.ts +8 -1
- package/src/resources/card-transactions.ts +718 -0
- package/src/resources/earning-types.ts +52 -10
- package/src/resources/index.ts +22 -1
- package/src/resources/reimbursements.ts +1157 -0
- package/src/resources/spend-categories.ts +175 -0
- package/src/resources/workflows/executions.ts +191 -0
- package/src/resources/workflows/index.ts +15 -0
- package/src/resources/workflows/workflows.ts +1661 -0
- package/src/resources/workflows.ts +1 -1647
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/lib/charts/guards.d.mts +0 -15
- package/lib/charts/guards.d.mts.map +0 -1
- package/lib/charts/guards.d.ts +0 -15
- package/lib/charts/guards.d.ts.map +0 -1
- package/lib/charts/guards.js +0 -38
- package/lib/charts/guards.js.map +0 -1
- package/lib/charts/guards.mjs +0 -34
- package/lib/charts/guards.mjs.map +0 -1
- package/src/lib/charts/guards.ts +0 -42
|
@@ -1,56 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { MAX_RULE_DEPTH, type Control, type Rule, type RuleNode } from './types';
|
|
1
|
+
import { UnknownNodeError } from './errors';
|
|
2
|
+
import { parseChartRule } from './schema';
|
|
3
|
+
import { type ComparisonOperandNode, type Control, type RuleNode } from './types';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Turn a rule into the shape the settings panel renders: a label plus its
|
|
8
|
-
* editable parts. Accepts a `{$rule}` placeholder or a bare
|
|
9
|
-
*
|
|
7
|
+
* editable parts. Accepts a `{$rule}` placeholder or a bare node. Reads the same
|
|
8
|
+
* value `evaluate` runs — and validates it the same way — so a malformed or
|
|
10
9
|
* unknown node throws a `ChartRuleError` here too, never a native `TypeError`.
|
|
11
|
-
* The
|
|
12
|
-
*
|
|
13
|
-
* A tree nested past `MAX_RULE_DEPTH` throws `RuleDepthError`.
|
|
10
|
+
* The Valibot parser rejects malformed or nested values before this flat
|
|
11
|
+
* formatter reads them.
|
|
14
12
|
*/
|
|
15
|
-
export function describe(rule:
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
export function describe(rule: unknown): Control {
|
|
14
|
+
const parsedRule = parseChartRule(rule);
|
|
15
|
+
if ('$rule' in parsedRule) {
|
|
16
|
+
return visit(parsedRule.$rule);
|
|
18
17
|
}
|
|
19
|
-
return visit(
|
|
18
|
+
return visit(parsedRule);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
function visit(node: RuleNode
|
|
23
|
-
if (depth > MAX_RULE_DEPTH) {
|
|
24
|
-
throw new RuleDepthError(MAX_RULE_DEPTH);
|
|
25
|
-
}
|
|
26
|
-
assertRuleNode(node);
|
|
27
|
-
const next = depth + 1;
|
|
21
|
+
function visit(node: RuleNode): Control {
|
|
28
22
|
switch (node.kind) {
|
|
29
23
|
case 'value':
|
|
30
24
|
return { role: 'ref', label: "this row's value" };
|
|
31
25
|
case 'literal': {
|
|
32
|
-
const scalar =
|
|
26
|
+
const scalar = node.value;
|
|
33
27
|
return { role: 'literal', label: String(scalar), value: scalar };
|
|
34
28
|
}
|
|
35
29
|
|
|
36
30
|
case 'equals':
|
|
37
|
-
return comparison('is', node.left, node.right
|
|
31
|
+
return comparison('is', node.left, node.right);
|
|
38
32
|
case 'isNot':
|
|
39
|
-
return comparison('is not', node.left, node.right
|
|
33
|
+
return comparison('is not', node.left, node.right);
|
|
40
34
|
case 'lessThan':
|
|
41
|
-
return comparison('is under', node.left, node.right
|
|
35
|
+
return comparison('is under', node.left, node.right);
|
|
42
36
|
case 'greaterThan':
|
|
43
|
-
return comparison('is over', node.left, node.right
|
|
37
|
+
return comparison('is over', node.left, node.right);
|
|
44
38
|
case 'lessOrEqual':
|
|
45
|
-
return comparison('is at most', node.left, node.right
|
|
39
|
+
return comparison('is at most', node.left, node.right);
|
|
46
40
|
case 'greaterOrEqual':
|
|
47
|
-
return comparison('is at least', node.left, node.right
|
|
41
|
+
return comparison('is at least', node.left, node.right);
|
|
48
42
|
|
|
49
43
|
case 'ifElse':
|
|
50
44
|
return {
|
|
51
45
|
role: 'rule',
|
|
52
46
|
label: 'when',
|
|
53
|
-
parts: [visit(node.cond
|
|
47
|
+
parts: [visit(node.cond), literalControl(node.then.value), literalControl(node.else.value)],
|
|
54
48
|
};
|
|
55
49
|
|
|
56
50
|
default:
|
|
@@ -58,8 +52,16 @@ function visit(node: RuleNode, depth: number): Control {
|
|
|
58
52
|
}
|
|
59
53
|
}
|
|
60
54
|
|
|
61
|
-
function comparison(label: string, left:
|
|
62
|
-
return { role: 'comparison', label, parts: [
|
|
55
|
+
function comparison(label: string, left: ComparisonOperandNode, right: ComparisonOperandNode): Control {
|
|
56
|
+
return { role: 'comparison', label, parts: [operandControl(left), operandControl(right)] };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function operandControl(node: ComparisonOperandNode): Control {
|
|
60
|
+
return node.kind === 'value' ? { role: 'ref', label: "this row's value" } : literalControl(node.value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function literalControl(value: string | number | boolean | null): Control {
|
|
64
|
+
return { role: 'literal', label: String(value), value };
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
function unreachable(node: never): never {
|
package/src/lib/charts/errors.ts
CHANGED
|
@@ -21,7 +21,7 @@ export class RuleTypeError extends ChartRuleError {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* A structurally invalid node in a deserialized
|
|
24
|
+
* A structurally invalid node in a deserialized value: not an object, no string
|
|
25
25
|
* `kind`, or a `literal` without a scalar `value`. Distinct from
|
|
26
26
|
* `UnknownNodeError` (a well-formed node whose `kind` is simply not in the
|
|
27
27
|
* vocabulary — the intentional escape hatch). Like the others it is a
|
|
@@ -35,11 +35,7 @@ export class MalformedRuleError extends ChartRuleError {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
/**
|
|
39
|
-
* A rule nested deeper than the evaluator will walk. Guards against a
|
|
40
|
-
* stack-overflowing tree from untrusted, deserialized input; like the others it
|
|
41
|
-
* is a `ChartRuleError`, so the caller's escape hatch catches it.
|
|
42
|
-
*/
|
|
38
|
+
/** @deprecated v1 is non-recursive. Retained for source compatibility. */
|
|
43
39
|
export class RuleDepthError extends ChartRuleError {
|
|
44
40
|
constructor(maxDepth: number) {
|
|
45
41
|
super(`chart rule nested deeper than ${maxDepth} levels`);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { assertRuleNode, literalValue } from './guards';
|
|
1
|
+
import { MalformedRuleError, RuleTypeError, UnknownNodeError } from './errors';
|
|
2
|
+
import { parseChartRule } from './schema';
|
|
4
3
|
import {
|
|
5
|
-
|
|
6
|
-
type
|
|
4
|
+
type ComparisonOperandNode,
|
|
5
|
+
type NumericOperandNode,
|
|
7
6
|
type RuleChannel,
|
|
8
7
|
type RuleContext,
|
|
9
8
|
type RuleNode,
|
|
@@ -12,51 +11,45 @@ import {
|
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Resolve a rule to a value against `ctx`. Accepts a `{$rule}` placeholder or a
|
|
15
|
-
* bare
|
|
14
|
+
* bare node. A malformed or unknown node throws a `ChartRuleError` (never a
|
|
16
15
|
* native `TypeError`), so a caller reading an untrusted, deserialized rule can
|
|
17
16
|
* catch every failure with one type. When the rule is a colour placeholder, the
|
|
18
17
|
* result is checked to be a colour (`string | null`) — a rule that resolves to a
|
|
19
18
|
* number or boolean is a `RuleTypeError`, not something the renderer paints.
|
|
20
19
|
*
|
|
21
|
-
* Pure: the only inputs are the
|
|
22
|
-
*
|
|
23
|
-
* and identical across the renderer, the panel readout, and a test. A tree
|
|
24
|
-
* nested past `MAX_RULE_DEPTH` throws `RuleDepthError` rather than overflowing.
|
|
20
|
+
* Pure and non-recursive: the only inputs are the validated flat rule and the
|
|
21
|
+
* context. No clock, randomness, callbacks, or I/O.
|
|
25
22
|
*/
|
|
26
|
-
export function evaluate(rule:
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
export function evaluate(rule: unknown, ctx: RuleContext): RuleValue {
|
|
24
|
+
const parsedRule = parseChartRule(rule);
|
|
25
|
+
if ('$rule' in parsedRule) {
|
|
26
|
+
return coerceForChannel(parsedRule.channel, visit(parsedRule.$rule, ctx));
|
|
29
27
|
}
|
|
30
|
-
return visit(
|
|
28
|
+
return visit(parsedRule, ctx);
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
function visit(node: RuleNode, ctx: RuleContext
|
|
34
|
-
if (depth > MAX_RULE_DEPTH) {
|
|
35
|
-
throw new RuleDepthError(MAX_RULE_DEPTH);
|
|
36
|
-
}
|
|
37
|
-
assertRuleNode(node);
|
|
38
|
-
const next = depth + 1;
|
|
31
|
+
function visit(node: RuleNode, ctx: RuleContext): RuleValue {
|
|
39
32
|
switch (node.kind) {
|
|
40
33
|
case 'value':
|
|
41
34
|
return ctx.value;
|
|
42
35
|
case 'literal':
|
|
43
|
-
return
|
|
36
|
+
return node.value;
|
|
44
37
|
|
|
45
38
|
case 'equals':
|
|
46
|
-
return
|
|
39
|
+
return operand(node.left, ctx) === operand(node.right, ctx);
|
|
47
40
|
case 'isNot':
|
|
48
|
-
return
|
|
41
|
+
return operand(node.left, ctx) !== operand(node.right, ctx);
|
|
49
42
|
case 'lessThan':
|
|
50
|
-
return
|
|
43
|
+
return numericOperand(node.left, ctx) < numericOperand(node.right, ctx);
|
|
51
44
|
case 'greaterThan':
|
|
52
|
-
return
|
|
45
|
+
return numericOperand(node.left, ctx) > numericOperand(node.right, ctx);
|
|
53
46
|
case 'lessOrEqual':
|
|
54
|
-
return
|
|
47
|
+
return numericOperand(node.left, ctx) <= numericOperand(node.right, ctx);
|
|
55
48
|
case 'greaterOrEqual':
|
|
56
|
-
return
|
|
49
|
+
return numericOperand(node.left, ctx) >= numericOperand(node.right, ctx);
|
|
57
50
|
|
|
58
51
|
case 'ifElse':
|
|
59
|
-
return
|
|
52
|
+
return visit(node.cond, ctx) ? node.then.value : node.else.value;
|
|
60
53
|
|
|
61
54
|
default:
|
|
62
55
|
return unreachable(node);
|
|
@@ -67,18 +60,12 @@ function visit(node: RuleNode, ctx: RuleContext, depth: number): RuleValue {
|
|
|
67
60
|
/* Coercions — narrow a RuleValue, or throw a typed error. */
|
|
68
61
|
/* -------------------------------------------------------------------------- */
|
|
69
62
|
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
return value;
|
|
73
|
-
}
|
|
74
|
-
throw new RuleTypeError(`expected a number, got ${typeName(value)}`);
|
|
63
|
+
function operand(node: ComparisonOperandNode, ctx: RuleContext): RuleValue {
|
|
64
|
+
return node.kind === 'value' ? ctx.value : node.value;
|
|
75
65
|
}
|
|
76
66
|
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
throw new RuleTypeError(`expected a boolean, got ${typeName(value)}`);
|
|
67
|
+
function numericOperand(node: NumericOperandNode, ctx: RuleContext): number {
|
|
68
|
+
return node.kind === 'value' ? ctx.value : node.value;
|
|
82
69
|
}
|
|
83
70
|
|
|
84
71
|
/** A colour channel must resolve to a colour string, or null for "no override". */
|
package/src/lib/charts/schema.ts
CHANGED
|
@@ -1,72 +1,238 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type BaseIssue,
|
|
3
|
+
boolean,
|
|
4
|
+
type GenericSchema,
|
|
5
|
+
finite,
|
|
6
|
+
literal,
|
|
7
|
+
null_,
|
|
8
|
+
number,
|
|
9
|
+
picklist,
|
|
10
|
+
pipe,
|
|
11
|
+
safeParse,
|
|
12
|
+
strictObject,
|
|
13
|
+
string,
|
|
14
|
+
summarize,
|
|
15
|
+
union,
|
|
16
|
+
variant,
|
|
17
|
+
} from 'valibot';
|
|
2
18
|
|
|
3
|
-
|
|
19
|
+
import { MalformedRuleError, UnknownNodeError } from './errors';
|
|
20
|
+
import { CHART_RULE_CONTRACT_VERSION, type ParsedRule, type Rule, type RuleNode, type Scalar } from './types';
|
|
21
|
+
import {
|
|
22
|
+
EQUALITY_COMPARISON_KINDS,
|
|
23
|
+
NUMERIC_COMPARISON_KINDS,
|
|
24
|
+
RULE_CHANNELS,
|
|
25
|
+
RULE_NODE_KINDS,
|
|
26
|
+
} from './vocabulary';
|
|
27
|
+
|
|
28
|
+
const ruleNodeKinds: ReadonlySet<string> = new Set(RULE_NODE_KINDS);
|
|
29
|
+
const placeholderKeys: ReadonlySet<string> = new Set(['$rule', 'channel']);
|
|
30
|
+
const ruleFieldKeys = ['$rule', 'channel', 'kind', 'value', 'left', 'right', 'cond', 'then', 'else'] as const;
|
|
31
|
+
const scalarSchema: GenericSchema<unknown, Scalar> = union([
|
|
32
|
+
pipe(number(), finite()),
|
|
33
|
+
string(),
|
|
34
|
+
boolean(),
|
|
35
|
+
null_(),
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
const parsedRules = new WeakSet<object>();
|
|
39
|
+
|
|
40
|
+
/** @deprecated Use `CHART_RULE_CONTRACT_VERSION` to identify the executable contract. */
|
|
4
41
|
export const CHART_RULE_SCHEMA_ID = `https://rippling.com/schemas/${CHART_RULE_CONTRACT_VERSION}.json`;
|
|
5
42
|
|
|
43
|
+
function hasOwn(value: object, key: PropertyKey): boolean {
|
|
44
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const refNodeSchema = strictObject({ kind: literal('value') });
|
|
48
|
+
const literalNodeSchema = strictObject({ kind: literal('literal'), value: scalarSchema });
|
|
49
|
+
const numericLiteralNodeSchema = strictObject({
|
|
50
|
+
kind: literal('literal'),
|
|
51
|
+
value: pipe(number(), finite()),
|
|
52
|
+
});
|
|
53
|
+
const requiredColorLiteralNodeSchema = strictObject({
|
|
54
|
+
kind: literal('literal'),
|
|
55
|
+
value: string(),
|
|
56
|
+
});
|
|
57
|
+
const optionalColorLiteralNodeSchema = strictObject({
|
|
58
|
+
kind: literal('literal'),
|
|
59
|
+
value: union([string(), null_()]),
|
|
60
|
+
});
|
|
61
|
+
const comparisonOperandSchema = variant('kind', [refNodeSchema, literalNodeSchema]);
|
|
62
|
+
const numericOperandSchema = variant('kind', [refNodeSchema, numericLiteralNodeSchema]);
|
|
63
|
+
|
|
64
|
+
const equalityComparisonNodeSchema = strictObject({
|
|
65
|
+
kind: picklist(EQUALITY_COMPARISON_KINDS),
|
|
66
|
+
left: comparisonOperandSchema,
|
|
67
|
+
right: comparisonOperandSchema,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const numericComparisonNodeSchema = strictObject({
|
|
71
|
+
kind: picklist(NUMERIC_COMPARISON_KINDS),
|
|
72
|
+
left: numericOperandSchema,
|
|
73
|
+
right: numericOperandSchema,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const comparisonNodeSchema = variant('kind', [equalityComparisonNodeSchema, numericComparisonNodeSchema]);
|
|
77
|
+
|
|
78
|
+
const ifElseNodeSchema = strictObject({
|
|
79
|
+
kind: literal('ifElse'),
|
|
80
|
+
cond: comparisonNodeSchema,
|
|
81
|
+
then: requiredColorLiteralNodeSchema,
|
|
82
|
+
else: optionalColorLiteralNodeSchema,
|
|
83
|
+
});
|
|
84
|
+
|
|
6
85
|
/**
|
|
7
|
-
*
|
|
86
|
+
* Executable runtime schema for a chart-rule node.
|
|
8
87
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
88
|
+
* v1 is intentionally non-recursive. A comparison contains only `value` or
|
|
89
|
+
* literal operands, and a conditional contains one comparison plus two colour
|
|
90
|
+
* literals. Contract tests prove that every public builder output parses.
|
|
91
|
+
*/
|
|
92
|
+
const ruleNodeSchema: GenericSchema<unknown, RuleNode, BaseIssue<unknown>> = variant('kind', [
|
|
93
|
+
refNodeSchema,
|
|
94
|
+
literalNodeSchema,
|
|
95
|
+
equalityComparisonNodeSchema,
|
|
96
|
+
numericComparisonNodeSchema,
|
|
97
|
+
ifElseNodeSchema,
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
/** Runtime schema for the `{$rule, channel}` value embedded in a chart option. */
|
|
101
|
+
const rulePlaceholderSchema = strictObject({
|
|
102
|
+
$rule: ifElseNodeSchema,
|
|
103
|
+
channel: picklist(RULE_CHANNELS),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Runtime schema for either a bare rule node or a chart-option placeholder.
|
|
108
|
+
* Persisted and agent-authored JSON crosses this boundary as `unknown`.
|
|
109
|
+
*/
|
|
110
|
+
export const chartRuleSchema: GenericSchema<unknown, Rule, BaseIssue<unknown>> = union([
|
|
111
|
+
rulePlaceholderSchema,
|
|
112
|
+
ruleNodeSchema,
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
/** Parse untrusted chart-rule JSON and return an immutable validated clone. */
|
|
116
|
+
export function parseChartRule(input: unknown): ParsedRule {
|
|
117
|
+
if (isParsedRule(input)) {
|
|
118
|
+
return input;
|
|
119
|
+
}
|
|
120
|
+
preflight(input);
|
|
121
|
+
const result = safeParse(chartRuleSchema, input);
|
|
122
|
+
if (result.success) {
|
|
123
|
+
return markParsedRule(result.output);
|
|
124
|
+
}
|
|
125
|
+
throw new MalformedRuleError(summarize(result.issues));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Valibot's object schemas use `key in input`, which also reads inherited
|
|
130
|
+
* properties. Reject inherited rule fields before parsing so prototype data
|
|
131
|
+
* cannot supply required operands or bypass the flat v1 contract.
|
|
12
132
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
133
|
+
function preflight(input: unknown): void {
|
|
134
|
+
assertNoInheritedRuleFields(input);
|
|
135
|
+
const root = unwrapPlaceholder(input);
|
|
136
|
+
assertNoInheritedRuleFields(root);
|
|
137
|
+
assertKnownNode(root);
|
|
138
|
+
|
|
139
|
+
if (!isRecord(root) || !hasOwn(root, 'kind')) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (root['kind'] === 'ifElse') {
|
|
143
|
+
inspectChildren(root, ['cond', 'then', 'else']);
|
|
144
|
+
const condition = root['cond'];
|
|
145
|
+
inspectComparison(condition);
|
|
146
|
+
} else if (typeof root['kind'] === 'string' && isComparisonKind(root['kind'])) {
|
|
147
|
+
inspectComparison(root);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function inspectComparison(value: unknown): void {
|
|
152
|
+
assertNoInheritedRuleFields(value);
|
|
153
|
+
assertKnownNode(value);
|
|
154
|
+
if (isRecord(value)) {
|
|
155
|
+
inspectChildren(value, ['left', 'right']);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function inspectChildren(parent: Record<string, unknown>, keys: readonly string[]): void {
|
|
160
|
+
for (const key of keys) {
|
|
161
|
+
if (hasOwn(parent, key)) {
|
|
162
|
+
const child = parent[key];
|
|
163
|
+
assertNoInheritedRuleFields(child);
|
|
164
|
+
assertKnownNode(child);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function assertNoInheritedRuleFields(value: unknown): void {
|
|
170
|
+
if (!isRecord(value)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
for (const key of ruleFieldKeys) {
|
|
174
|
+
if (key in value && !hasOwn(value, key)) {
|
|
175
|
+
throw new MalformedRuleError(`a chart rule must not inherit the ${key} property`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function assertKnownNode(value: unknown): void {
|
|
181
|
+
if (
|
|
182
|
+
isRecord(value) &&
|
|
183
|
+
hasOwn(value, 'kind') &&
|
|
184
|
+
typeof value['kind'] === 'string' &&
|
|
185
|
+
!ruleNodeKinds.has(value['kind'])
|
|
186
|
+
) {
|
|
187
|
+
throw new UnknownNodeError(value['kind']);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function isComparisonKind(kind: string): boolean {
|
|
192
|
+
return kind !== 'literal' && kind !== 'value' && kind !== 'ifElse' && ruleNodeKinds.has(kind);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
196
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function unwrapPlaceholder(input: unknown): unknown {
|
|
200
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input) || !hasOwn(input, '$rule')) {
|
|
201
|
+
return input;
|
|
202
|
+
}
|
|
203
|
+
const unexpectedKey = Object.keys(input).find((key) => !placeholderKeys.has(key));
|
|
204
|
+
if (unexpectedKey !== undefined) {
|
|
205
|
+
throw new MalformedRuleError(`a rule placeholder has an unexpected property: ${unexpectedKey}`);
|
|
206
|
+
}
|
|
207
|
+
return (input as { $rule?: unknown }).$rule;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function isParsedRule(input: unknown): input is ParsedRule {
|
|
211
|
+
return input !== null && typeof input === 'object' && !Array.isArray(input) && parsedRules.has(input);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Mark the validated clone so `evaluate(parseChartRule(input), ctx)` does not
|
|
216
|
+
* reparse the full value for every datum. Freezing prevents a caller from
|
|
217
|
+
* mutating a trusted value after validation.
|
|
218
|
+
*/
|
|
219
|
+
function markParsedRule(rule: Rule): ParsedRule {
|
|
220
|
+
const stack: object[] = [rule];
|
|
221
|
+
const seen = new WeakSet<object>();
|
|
222
|
+
while (stack.length > 0) {
|
|
223
|
+
const current = stack.pop();
|
|
224
|
+
if (!current || seen.has(current)) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
seen.add(current);
|
|
228
|
+
for (const value of Object.values(current)) {
|
|
229
|
+
if (value !== null && typeof value === 'object') {
|
|
230
|
+
stack.push(value);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
Object.freeze(current);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
parsedRules.add(rule);
|
|
237
|
+
return rule as ParsedRule;
|
|
238
|
+
}
|
package/src/lib/charts/types.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Chart rule vocabulary — shared types.
|
|
3
3
|
*
|
|
4
|
-
* A rule is a small, serializable
|
|
4
|
+
* A rule is a small, serializable value discriminated by `kind`. The
|
|
5
5
|
* agent builds it with the helpers in `builders.ts`, which emit it wrapped in a
|
|
6
6
|
* `{$rule}` placeholder — the same idea as the `{$fn}` placeholders a chart
|
|
7
7
|
* already emits. The renderer resolves it with `evaluate`; the settings panel
|
|
8
8
|
* reads it with `describe`. All three agree because a node can only ever be one
|
|
9
|
-
* of the kinds defined here — the closed set the `chartRuleSchema`
|
|
9
|
+
* of the kinds defined here — the closed set the executable `chartRuleSchema`
|
|
10
|
+
* validates at runtime.
|
|
10
11
|
*
|
|
11
|
-
* v1 is
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* v1 is deliberately non-recursive: compare this datum's `value` or a literal
|
|
13
|
+
* against another literal, then choose between two colour literals. Rules may
|
|
14
|
+
* not appear inside operands or branches. Richer expressions require a future
|
|
15
|
+
* contract version.
|
|
14
16
|
*/
|
|
15
17
|
|
|
18
|
+
import { COMPARISON_KINDS, RULE_CHANNELS } from './vocabulary';
|
|
19
|
+
|
|
16
20
|
export const CHART_RULE_CONTRACT_VERSION = 'chart_rule.v1' as const;
|
|
17
21
|
export type ChartRuleContractVersion = typeof CHART_RULE_CONTRACT_VERSION;
|
|
18
22
|
|
|
@@ -26,33 +30,39 @@ export type Scalar = number | string | boolean | null;
|
|
|
26
30
|
/** Reads a value out of the evaluation context — this datum's plotted value. */
|
|
27
31
|
export type RefNode = { kind: 'value' };
|
|
28
32
|
|
|
29
|
-
export interface LiteralNode {
|
|
33
|
+
export interface LiteralNode<TValue extends Scalar = Scalar> {
|
|
30
34
|
kind: 'literal';
|
|
31
|
-
value:
|
|
35
|
+
value: TValue;
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
export type
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
| '
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
kind: ComparisonKind;
|
|
43
|
-
left: RuleNode;
|
|
44
|
-
right: RuleNode;
|
|
38
|
+
export type ComparisonOperandNode = RefNode | LiteralNode;
|
|
39
|
+
export type NumericOperandNode = RefNode | LiteralNode<number>;
|
|
40
|
+
|
|
41
|
+
export type ComparisonKind = (typeof COMPARISON_KINDS)[number];
|
|
42
|
+
export interface EqualityComparisonNode {
|
|
43
|
+
kind: 'equals' | 'isNot';
|
|
44
|
+
left: ComparisonOperandNode;
|
|
45
|
+
right: ComparisonOperandNode;
|
|
45
46
|
}
|
|
47
|
+
export interface NumericComparisonNode {
|
|
48
|
+
kind: Exclude<ComparisonKind, EqualityComparisonNode['kind']>;
|
|
49
|
+
left: NumericOperandNode;
|
|
50
|
+
right: NumericOperandNode;
|
|
51
|
+
}
|
|
52
|
+
export type ComparisonNode = EqualityComparisonNode | NumericComparisonNode;
|
|
53
|
+
|
|
54
|
+
/** A node whose evaluated result is valid as a `when(...)` condition in v1. */
|
|
55
|
+
export type ConditionNode = ComparisonNode;
|
|
46
56
|
|
|
47
57
|
/** The conditional-value form produced by `when(cond, then, else)`. */
|
|
48
58
|
export interface IfElseNode {
|
|
49
59
|
kind: 'ifElse';
|
|
50
|
-
cond:
|
|
51
|
-
then:
|
|
52
|
-
else:
|
|
60
|
+
cond: ConditionNode;
|
|
61
|
+
then: LiteralNode<string>;
|
|
62
|
+
else: LiteralNode<string | null>;
|
|
53
63
|
}
|
|
54
64
|
|
|
55
|
-
/** Any node
|
|
65
|
+
/** Any top-level node accepted by the v1 runtime boundary. */
|
|
56
66
|
export type RuleNode = RefNode | LiteralNode | ComparisonNode | IfElseNode;
|
|
57
67
|
|
|
58
68
|
/* -------------------------------------------------------------------------- */
|
|
@@ -64,21 +74,26 @@ export type RuleNode = RefNode | LiteralNode | ComparisonNode | IfElseNode;
|
|
|
64
74
|
* colour of a series/datum. Other channels (opacity, label visibility, …) are
|
|
65
75
|
* the documented roadmap and would be added here.
|
|
66
76
|
*/
|
|
67
|
-
export type RuleChannel =
|
|
77
|
+
export type RuleChannel = (typeof RULE_CHANNELS)[number];
|
|
68
78
|
|
|
69
79
|
/**
|
|
70
|
-
* What a builder returns: a rule
|
|
80
|
+
* What a builder returns: a flat rule value, marked so the renderer can find it, and
|
|
71
81
|
* tagged with the channel it targets. `channel: 'color'` says this resolves to a
|
|
72
82
|
* colour — so a reader sees the target without inferring it from where it sits.
|
|
73
83
|
*/
|
|
74
84
|
export interface RulePlaceholder {
|
|
75
|
-
$rule:
|
|
85
|
+
$rule: IfElseNode;
|
|
76
86
|
channel: RuleChannel;
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
/** A placeholder, or
|
|
89
|
+
/** A placeholder, or a bare node — what `evaluate` and `describe` accept. */
|
|
80
90
|
export type Rule = RulePlaceholder | RuleNode;
|
|
81
91
|
|
|
92
|
+
type DeepReadonly<T> = T extends object ? { readonly [Key in keyof T]: DeepReadonly<T[Key]> } : T;
|
|
93
|
+
|
|
94
|
+
/** The deeply frozen value returned by `parseChartRule`. */
|
|
95
|
+
export type ParsedRule = DeepReadonly<Rule>;
|
|
96
|
+
|
|
82
97
|
/* -------------------------------------------------------------------------- */
|
|
83
98
|
/* Evaluation */
|
|
84
99
|
/* -------------------------------------------------------------------------- */
|
|
@@ -91,11 +106,7 @@ export interface RuleContext {
|
|
|
91
106
|
/** The result of evaluating a node. */
|
|
92
107
|
export type RuleValue = Scalar;
|
|
93
108
|
|
|
94
|
-
/**
|
|
95
|
-
* The deepest a rule may nest before `evaluate`/`describe` refuse it. Real rules
|
|
96
|
-
* are a handful of levels; this only stops a pathological deserialized tree from
|
|
97
|
-
* overflowing the stack. Far below the JS call-stack limit.
|
|
98
|
-
*/
|
|
109
|
+
/** @deprecated v1 is non-recursive. Retained for source compatibility. */
|
|
99
110
|
export const MAX_RULE_DEPTH = 256;
|
|
100
111
|
|
|
101
112
|
/* -------------------------------------------------------------------------- */
|