@fogpipe/forma-core 0.6.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/README.md +169 -0
- package/dist/chunk-IRLYWN3R.js +790 -0
- package/dist/chunk-IRLYWN3R.js.map +1 -0
- package/dist/chunk-QTLXVG6P.js +135 -0
- package/dist/chunk-QTLXVG6P.js.map +1 -0
- package/dist/engine/index.cjs +885 -0
- package/dist/engine/index.cjs.map +1 -0
- package/dist/engine/index.d.cts +258 -0
- package/dist/engine/index.d.ts +258 -0
- package/dist/engine/index.js +32 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/feel/index.cjs +165 -0
- package/dist/feel/index.cjs.map +1 -0
- package/dist/feel/index.d.cts +105 -0
- package/dist/feel/index.d.ts +105 -0
- package/dist/feel/index.js +19 -0
- package/dist/feel/index.js.map +1 -0
- package/dist/index.cjs +962 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/types-Bs3CG9JZ.d.cts +283 -0
- package/dist/types-Bs3CG9JZ.d.ts +283 -0
- package/package.json +82 -0
- package/src/engine/calculate.ts +363 -0
- package/src/engine/enabled.ts +147 -0
- package/src/engine/index.ts +54 -0
- package/src/engine/required.ts +151 -0
- package/src/engine/validate.ts +647 -0
- package/src/engine/visibility.ts +228 -0
- package/src/feel/index.ts +299 -0
- package/src/index.ts +15 -0
- package/src/types.ts +364 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Forma Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* This module defines the complete type system for Forma specifications.
|
|
5
|
+
* All conditional logic uses FEEL (Friendly Enough Expression Language).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* FEEL expression - a string that will be evaluated at runtime.
|
|
9
|
+
*
|
|
10
|
+
* Available context variables:
|
|
11
|
+
* - `fieldName` - Any field value by name
|
|
12
|
+
* - `computed.name` - Computed value by name
|
|
13
|
+
* - `ref.path` - Reference data lookup (external lookup tables)
|
|
14
|
+
* - `item.fieldName` - Field within current array item
|
|
15
|
+
* - `itemIndex` - Current item index (0-based)
|
|
16
|
+
* - `value` - Current field value (in validation expressions)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* "age >= 18"
|
|
20
|
+
* "claimType = \"Auto\" and wasDriver = true"
|
|
21
|
+
* "computed.bmi > 30"
|
|
22
|
+
* "item.frequency = \"daily\""
|
|
23
|
+
*/
|
|
24
|
+
type FEELExpression = string;
|
|
25
|
+
/**
|
|
26
|
+
* Standard JSON Schema (subset we support)
|
|
27
|
+
*/
|
|
28
|
+
interface JSONSchema {
|
|
29
|
+
type: "object";
|
|
30
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
31
|
+
required?: string[];
|
|
32
|
+
$defs?: Record<string, JSONSchemaProperty>;
|
|
33
|
+
}
|
|
34
|
+
type JSONSchemaProperty = JSONSchemaString | JSONSchemaNumber | JSONSchemaInteger | JSONSchemaBoolean | JSONSchemaArray | JSONSchemaObject | JSONSchemaEnum;
|
|
35
|
+
interface JSONSchemaBase {
|
|
36
|
+
description?: string;
|
|
37
|
+
title?: string;
|
|
38
|
+
}
|
|
39
|
+
interface JSONSchemaString extends JSONSchemaBase {
|
|
40
|
+
type: "string";
|
|
41
|
+
minLength?: number;
|
|
42
|
+
maxLength?: number;
|
|
43
|
+
pattern?: string;
|
|
44
|
+
format?: "date" | "date-time" | "email" | "uri" | "uuid";
|
|
45
|
+
enum?: string[];
|
|
46
|
+
}
|
|
47
|
+
interface JSONSchemaNumber extends JSONSchemaBase {
|
|
48
|
+
type: "number";
|
|
49
|
+
minimum?: number;
|
|
50
|
+
maximum?: number;
|
|
51
|
+
exclusiveMinimum?: number;
|
|
52
|
+
exclusiveMaximum?: number;
|
|
53
|
+
}
|
|
54
|
+
interface JSONSchemaInteger extends JSONSchemaBase {
|
|
55
|
+
type: "integer";
|
|
56
|
+
minimum?: number;
|
|
57
|
+
maximum?: number;
|
|
58
|
+
}
|
|
59
|
+
interface JSONSchemaBoolean extends JSONSchemaBase {
|
|
60
|
+
type: "boolean";
|
|
61
|
+
}
|
|
62
|
+
interface JSONSchemaArray extends JSONSchemaBase {
|
|
63
|
+
type: "array";
|
|
64
|
+
items: JSONSchemaProperty;
|
|
65
|
+
minItems?: number;
|
|
66
|
+
maxItems?: number;
|
|
67
|
+
}
|
|
68
|
+
interface JSONSchemaObject extends JSONSchemaBase {
|
|
69
|
+
type: "object";
|
|
70
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
71
|
+
required?: string[];
|
|
72
|
+
}
|
|
73
|
+
interface JSONSchemaEnum extends JSONSchemaBase {
|
|
74
|
+
type: "string";
|
|
75
|
+
enum: string[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Field type enumeration - maps to UI component types
|
|
79
|
+
*/
|
|
80
|
+
type FieldType = "text" | "password" | "number" | "integer" | "boolean" | "select" | "multiselect" | "date" | "datetime" | "email" | "url" | "textarea" | "array" | "object" | "computed";
|
|
81
|
+
/**
|
|
82
|
+
* Validation rule with FEEL expression
|
|
83
|
+
*/
|
|
84
|
+
interface ValidationRule {
|
|
85
|
+
/** FEEL expression that should evaluate to true for valid data */
|
|
86
|
+
rule: FEELExpression;
|
|
87
|
+
/** Error message shown when validation fails */
|
|
88
|
+
message: string;
|
|
89
|
+
/** Severity level - errors block submission, warnings are informational */
|
|
90
|
+
severity?: "error" | "warning";
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Option for select/multiselect fields
|
|
94
|
+
*/
|
|
95
|
+
interface SelectOption {
|
|
96
|
+
/** Option value - can be string or number to match schema types */
|
|
97
|
+
value: string | number;
|
|
98
|
+
label: string;
|
|
99
|
+
/** When to show this option */
|
|
100
|
+
visibleWhen?: FEELExpression;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Field definition with all conditional logic
|
|
104
|
+
*/
|
|
105
|
+
interface FieldDefinition {
|
|
106
|
+
/** Display label for the field */
|
|
107
|
+
label?: string;
|
|
108
|
+
/** Help text or description */
|
|
109
|
+
description?: string;
|
|
110
|
+
/** Placeholder text for input fields */
|
|
111
|
+
placeholder?: string;
|
|
112
|
+
/** Field type override (inferred from schema if not provided) */
|
|
113
|
+
type?: FieldType;
|
|
114
|
+
/** When to show this field. If omitted, always visible. */
|
|
115
|
+
visibleWhen?: FEELExpression;
|
|
116
|
+
/** When this field is required. If omitted, uses schema required. */
|
|
117
|
+
requiredWhen?: FEELExpression;
|
|
118
|
+
/** When this field is editable. If omitted, always enabled. */
|
|
119
|
+
enabledWhen?: FEELExpression;
|
|
120
|
+
/** Custom validation rules using FEEL expressions */
|
|
121
|
+
validations?: ValidationRule[];
|
|
122
|
+
/** For array fields: field definitions for each item. Requires type: "array" */
|
|
123
|
+
itemFields?: Record<string, FieldDefinition>;
|
|
124
|
+
/** Minimum number of items (overrides schema) */
|
|
125
|
+
minItems?: number;
|
|
126
|
+
/** Maximum number of items (overrides schema) */
|
|
127
|
+
maxItems?: number;
|
|
128
|
+
/** For select fields: available options */
|
|
129
|
+
options?: SelectOption[];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Computed field definition - derived values from form data
|
|
133
|
+
*/
|
|
134
|
+
interface ComputedField {
|
|
135
|
+
/** FEEL expression to calculate the value */
|
|
136
|
+
expression: FEELExpression;
|
|
137
|
+
/** Display label */
|
|
138
|
+
label?: string;
|
|
139
|
+
/** Display format (e.g., "decimal(1)", "currency", "percent") */
|
|
140
|
+
format?: string;
|
|
141
|
+
/** Whether to display this value in the form */
|
|
142
|
+
display?: boolean;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Page definition for multi-step wizard forms
|
|
146
|
+
*/
|
|
147
|
+
interface PageDefinition {
|
|
148
|
+
/** Unique page identifier */
|
|
149
|
+
id: string;
|
|
150
|
+
/** Page title */
|
|
151
|
+
title: string;
|
|
152
|
+
/** Page description/instructions */
|
|
153
|
+
description?: string;
|
|
154
|
+
/** When to show this page */
|
|
155
|
+
visibleWhen?: FEELExpression;
|
|
156
|
+
/** Field paths to include on this page */
|
|
157
|
+
fields: string[];
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Form metadata
|
|
161
|
+
*/
|
|
162
|
+
interface FormMeta {
|
|
163
|
+
/** Unique form identifier */
|
|
164
|
+
id: string;
|
|
165
|
+
/** Form title */
|
|
166
|
+
title: string;
|
|
167
|
+
/** Form description */
|
|
168
|
+
description?: string;
|
|
169
|
+
/** Form version */
|
|
170
|
+
version?: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Complete Form Specification
|
|
174
|
+
*
|
|
175
|
+
* This is the root type that defines an entire form including:
|
|
176
|
+
* - Data schema (JSON Schema)
|
|
177
|
+
* - Computed values
|
|
178
|
+
* - Reference data (lookup tables for calculations)
|
|
179
|
+
* - Field-level configuration and conditional logic
|
|
180
|
+
* - Field ordering
|
|
181
|
+
* - Optional wizard/page structure
|
|
182
|
+
*/
|
|
183
|
+
interface Forma {
|
|
184
|
+
/** Schema version identifier */
|
|
185
|
+
$schema?: string;
|
|
186
|
+
/** Spec version */
|
|
187
|
+
version: "1.0";
|
|
188
|
+
/** Form metadata */
|
|
189
|
+
meta: FormMeta;
|
|
190
|
+
/** JSON Schema defining the data structure */
|
|
191
|
+
schema: JSONSchema;
|
|
192
|
+
/** Computed/calculated values */
|
|
193
|
+
computed?: Record<string, ComputedField>;
|
|
194
|
+
/** Reference data for lookups (external data sources, lookup tables) */
|
|
195
|
+
referenceData?: Record<string, unknown>;
|
|
196
|
+
/** Field-level definitions and rules */
|
|
197
|
+
fields: Record<string, FieldDefinition>;
|
|
198
|
+
/** Order in which fields should be displayed */
|
|
199
|
+
fieldOrder: string[];
|
|
200
|
+
/** Optional multi-page wizard structure */
|
|
201
|
+
pages?: PageDefinition[];
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Context provided when evaluating FEEL expressions
|
|
205
|
+
*/
|
|
206
|
+
interface EvaluationContext {
|
|
207
|
+
/** Current form data */
|
|
208
|
+
data: Record<string, unknown>;
|
|
209
|
+
/** Computed values (calculated before field evaluation) */
|
|
210
|
+
computed?: Record<string, unknown>;
|
|
211
|
+
/** Reference data for lookups (external data sources, lookup tables) */
|
|
212
|
+
referenceData?: Record<string, unknown>;
|
|
213
|
+
/** Current array item (when evaluating within array context) */
|
|
214
|
+
item?: Record<string, unknown>;
|
|
215
|
+
/** Current array item index (0-based) */
|
|
216
|
+
itemIndex?: number;
|
|
217
|
+
/** Current field value (for validation expressions) */
|
|
218
|
+
value?: unknown;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Field visibility result - map of field path to visibility state
|
|
222
|
+
*/
|
|
223
|
+
interface VisibilityResult {
|
|
224
|
+
[fieldPath: string]: boolean;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Required fields result - map of field path to required state
|
|
228
|
+
*/
|
|
229
|
+
interface RequiredResult {
|
|
230
|
+
[fieldPath: string]: boolean;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Alias for RequiredResult (backwards compatibility)
|
|
234
|
+
*/
|
|
235
|
+
type RequiredFieldsResult = RequiredResult;
|
|
236
|
+
/**
|
|
237
|
+
* Enabled fields result - map of field path to enabled state
|
|
238
|
+
*/
|
|
239
|
+
interface EnabledResult {
|
|
240
|
+
[fieldPath: string]: boolean;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Field validation error
|
|
244
|
+
*/
|
|
245
|
+
interface FieldError {
|
|
246
|
+
/** Field path that has the error */
|
|
247
|
+
field: string;
|
|
248
|
+
/** Error message */
|
|
249
|
+
message: string;
|
|
250
|
+
/** Error severity */
|
|
251
|
+
severity: "error" | "warning";
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Validation result
|
|
255
|
+
*/
|
|
256
|
+
interface ValidationResult {
|
|
257
|
+
/** Whether the form data is valid */
|
|
258
|
+
valid: boolean;
|
|
259
|
+
/** List of validation errors */
|
|
260
|
+
errors: FieldError[];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Calculation error
|
|
264
|
+
*/
|
|
265
|
+
interface CalculationError {
|
|
266
|
+
/** Computed field name */
|
|
267
|
+
field: string;
|
|
268
|
+
/** Error message */
|
|
269
|
+
message: string;
|
|
270
|
+
/** The expression that failed */
|
|
271
|
+
expression: string;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Calculation result with potential errors
|
|
275
|
+
*/
|
|
276
|
+
interface CalculationResult {
|
|
277
|
+
/** Computed values */
|
|
278
|
+
values: Record<string, unknown>;
|
|
279
|
+
/** Errors encountered during calculation */
|
|
280
|
+
errors: CalculationError[];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export type { ComputedField as C, EvaluationContext as E, FEELExpression as F, JSONSchema as J, PageDefinition as P, RequiredResult as R, SelectOption as S, ValidationRule as V, JSONSchemaProperty as a, JSONSchemaBase as b, JSONSchemaString as c, JSONSchemaNumber as d, JSONSchemaInteger as e, JSONSchemaBoolean as f, JSONSchemaArray as g, JSONSchemaObject as h, JSONSchemaEnum as i, FieldType as j, FieldDefinition as k, FormMeta as l, Forma as m, VisibilityResult as n, RequiredFieldsResult as o, EnabledResult as p, FieldError as q, ValidationResult as r, CalculationError as s, CalculationResult as t };
|
package/package.json
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fogpipe/forma-core",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Forma core runtime: Types and evaluation engines for dynamic forms with FEEL expressions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./engine": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/engine/index.d.ts",
|
|
22
|
+
"default": "./dist/engine/index.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/engine/index.d.ts",
|
|
26
|
+
"default": "./dist/engine/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"./feel": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/feel/index.d.ts",
|
|
32
|
+
"default": "./dist/feel/index.js"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/feel/index.d.ts",
|
|
36
|
+
"default": "./dist/feel/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"dev": "tsup --watch",
|
|
47
|
+
"lint": "eslint .",
|
|
48
|
+
"check-types": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"feelin": "^5.1.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@fogpipe/eslint-config": "*",
|
|
57
|
+
"@fogpipe/typescript-config": "*",
|
|
58
|
+
"tsup": "^8.5.1",
|
|
59
|
+
"typescript": "^5.9.2",
|
|
60
|
+
"vitest": "^3.2.4"
|
|
61
|
+
},
|
|
62
|
+
"keywords": [
|
|
63
|
+
"form",
|
|
64
|
+
"feel",
|
|
65
|
+
"validation",
|
|
66
|
+
"conditional-logic",
|
|
67
|
+
"dynamic-forms"
|
|
68
|
+
],
|
|
69
|
+
"license": "MIT",
|
|
70
|
+
"repository": {
|
|
71
|
+
"type": "git",
|
|
72
|
+
"url": "git+https://github.com/fogpipe/forma.git",
|
|
73
|
+
"directory": "packages/forma-core"
|
|
74
|
+
},
|
|
75
|
+
"bugs": {
|
|
76
|
+
"url": "https://github.com/fogpipe/forma/issues"
|
|
77
|
+
},
|
|
78
|
+
"homepage": "https://github.com/fogpipe/forma#readme",
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"access": "public"
|
|
81
|
+
}
|
|
82
|
+
}
|