@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
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# @fogpipe/forma-core
|
|
2
|
+
|
|
3
|
+
Core runtime for Forma - a declarative form specification with FEEL expressions for dynamic behavior.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @fogpipe/forma-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Type Definitions** - Complete TypeScript types for Forma specifications
|
|
14
|
+
- **FEEL Evaluation** - Evaluate FEEL expressions in form context
|
|
15
|
+
- **Visibility Engine** - Determine which fields should be visible
|
|
16
|
+
- **Required Engine** - Evaluate conditional required state
|
|
17
|
+
- **Enabled Engine** - Evaluate conditional enabled/disabled state
|
|
18
|
+
- **Calculation Engine** - Compute derived field values
|
|
19
|
+
- **Validation Engine** - Validate form data against rules
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Define a Forma Specification
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import type { Forma } from '@fogpipe/forma-core';
|
|
27
|
+
|
|
28
|
+
const form: Forma = {
|
|
29
|
+
meta: {
|
|
30
|
+
title: "Contact Form"
|
|
31
|
+
},
|
|
32
|
+
fields: [
|
|
33
|
+
{
|
|
34
|
+
id: "name",
|
|
35
|
+
type: "text",
|
|
36
|
+
label: "Full Name",
|
|
37
|
+
required: true
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "email",
|
|
41
|
+
type: "email",
|
|
42
|
+
label: "Email",
|
|
43
|
+
required: true
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "phone",
|
|
47
|
+
type: "text",
|
|
48
|
+
label: "Phone Number",
|
|
49
|
+
// Only required if email is not provided
|
|
50
|
+
required: "email = null or email = \"\""
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Evaluate Form State
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import {
|
|
60
|
+
getVisibility,
|
|
61
|
+
getRequired,
|
|
62
|
+
getEnabled,
|
|
63
|
+
validate,
|
|
64
|
+
calculate
|
|
65
|
+
} from '@fogpipe/forma-core';
|
|
66
|
+
|
|
67
|
+
const data = { name: "John", email: "john@example.com" };
|
|
68
|
+
|
|
69
|
+
// Get visibility state for all fields
|
|
70
|
+
const visibility = getVisibility(data, form);
|
|
71
|
+
|
|
72
|
+
// Get required state for all fields
|
|
73
|
+
const required = getRequired(data, form);
|
|
74
|
+
|
|
75
|
+
// Get enabled state for all fields
|
|
76
|
+
const enabled = getEnabled(data, form);
|
|
77
|
+
|
|
78
|
+
// Validate form data
|
|
79
|
+
const result = validate(data, form);
|
|
80
|
+
if (!result.valid) {
|
|
81
|
+
console.log('Errors:', result.errors);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Calculate computed values
|
|
85
|
+
const computed = calculate(data, form);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### FEEL Expression Evaluation
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { evaluate, evaluateBoolean } from '@fogpipe/forma-core/feel';
|
|
92
|
+
|
|
93
|
+
const context = {
|
|
94
|
+
data: { age: 25, country: "US" }
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Evaluate any expression
|
|
98
|
+
const result = evaluate<number>("age * 2", context);
|
|
99
|
+
if (result.success) {
|
|
100
|
+
console.log(result.value); // 50
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Evaluate boolean expression
|
|
104
|
+
const isAdult = evaluateBoolean("age >= 18", context); // true
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API Reference
|
|
108
|
+
|
|
109
|
+
### Types
|
|
110
|
+
|
|
111
|
+
- `Forma` - Complete form specification
|
|
112
|
+
- `FieldDefinition` - Field definition
|
|
113
|
+
- `ComputedField` - Computed field definition
|
|
114
|
+
- `PageDefinition` - Page definition for wizards
|
|
115
|
+
- `FieldError` - Validation error
|
|
116
|
+
- `ValidationResult` - Validation result
|
|
117
|
+
|
|
118
|
+
### Engine Functions
|
|
119
|
+
|
|
120
|
+
#### Visibility
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
getVisibility(data, spec, options?) → Record<string, boolean>
|
|
124
|
+
isFieldVisible(fieldId, data, spec, options?) → boolean
|
|
125
|
+
getPageVisibility(data, spec, options?) → Record<string, boolean>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Required
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
getRequired(data, spec, options?) → Record<string, boolean>
|
|
132
|
+
isRequired(fieldId, data, spec, options?) → boolean
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### Enabled
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
getEnabled(data, spec, options?) → Record<string, boolean>
|
|
139
|
+
isEnabled(fieldId, data, spec, options?) → boolean
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Validation
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
validate(data, spec, options?) → ValidationResult
|
|
146
|
+
validateSingleField(fieldId, value, data, spec, options?) → FieldError[]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### Calculation
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
calculate(data, spec, options?) → Record<string, unknown>
|
|
153
|
+
calculateWithErrors(data, spec, options?) → CalculationResult
|
|
154
|
+
calculateField(fieldName, data, spec, existingComputed?, options?) → unknown
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### FEEL Functions
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
evaluate<T>(expression, context) → EvaluationOutcome<T>
|
|
161
|
+
evaluateBoolean(expression, context) → boolean
|
|
162
|
+
evaluateNumber(expression, context) → number | null
|
|
163
|
+
evaluateString(expression, context) → string | null
|
|
164
|
+
isValidExpression(expression) → boolean
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|