@planeasyinc/fe-core 0.1.4
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/dist/index.d.ts +489 -0
- package/dist/index.js +843 -0
- package/dist/index.js.map +1 -0
- package/package.json +27 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
// src/runtime.ts
|
|
2
|
+
function createRuntime() {
|
|
3
|
+
const state = {
|
|
4
|
+
fields: /* @__PURE__ */ new Map(),
|
|
5
|
+
deps: /* @__PURE__ */ new Map()
|
|
6
|
+
};
|
|
7
|
+
const ensureFieldState = (path) => {
|
|
8
|
+
if (!state.fields.has(path)) {
|
|
9
|
+
state.fields.set(path, {
|
|
10
|
+
visible: true,
|
|
11
|
+
enabled: true,
|
|
12
|
+
required: false,
|
|
13
|
+
touched: false,
|
|
14
|
+
dirty: false,
|
|
15
|
+
status: "VALID",
|
|
16
|
+
errors: []
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return state.fields.get(path);
|
|
20
|
+
};
|
|
21
|
+
const getFieldState = (path) => {
|
|
22
|
+
return state.fields.get(path);
|
|
23
|
+
};
|
|
24
|
+
const setVisible = (path, visible) => {
|
|
25
|
+
const field = ensureFieldState(path);
|
|
26
|
+
if (field.visible !== visible) {
|
|
27
|
+
field.visible = visible;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const setEnabled = (path, enabled) => {
|
|
31
|
+
const field = ensureFieldState(path);
|
|
32
|
+
if (field.enabled !== enabled) {
|
|
33
|
+
field.enabled = enabled;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const setRequired = (path, required) => {
|
|
37
|
+
const field = ensureFieldState(path);
|
|
38
|
+
if (field.required !== required) {
|
|
39
|
+
field.required = required;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const setTouched = (path, touched) => {
|
|
43
|
+
const field = ensureFieldState(path);
|
|
44
|
+
if (field.touched !== touched) {
|
|
45
|
+
field.touched = touched;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const setDirty = (path, dirty) => {
|
|
49
|
+
const field = ensureFieldState(path);
|
|
50
|
+
if (field.dirty !== dirty) {
|
|
51
|
+
field.dirty = dirty;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const setStatus = (path, status) => {
|
|
55
|
+
const field = ensureFieldState(path);
|
|
56
|
+
if (field.status !== status) {
|
|
57
|
+
field.status = status;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const setErrors = (path, errors) => {
|
|
61
|
+
const field = ensureFieldState(path);
|
|
62
|
+
field.errors = errors;
|
|
63
|
+
if (errors.length > 0) {
|
|
64
|
+
field.status = "INVALID";
|
|
65
|
+
} else if (field.status === "INVALID") {
|
|
66
|
+
field.status = "VALID";
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const addDependency = (sourcePath, targetPath) => {
|
|
70
|
+
if (!state.deps.has(sourcePath)) {
|
|
71
|
+
state.deps.set(sourcePath, /* @__PURE__ */ new Set());
|
|
72
|
+
}
|
|
73
|
+
state.deps.get(sourcePath).add(targetPath);
|
|
74
|
+
};
|
|
75
|
+
const getDependents = (path) => {
|
|
76
|
+
return state.deps.get(path) || /* @__PURE__ */ new Set();
|
|
77
|
+
};
|
|
78
|
+
const api = {
|
|
79
|
+
state,
|
|
80
|
+
ensureFieldState,
|
|
81
|
+
getFieldState,
|
|
82
|
+
setVisible,
|
|
83
|
+
setEnabled,
|
|
84
|
+
setRequired,
|
|
85
|
+
setTouched,
|
|
86
|
+
setDirty,
|
|
87
|
+
setStatus,
|
|
88
|
+
setErrors,
|
|
89
|
+
addDependency,
|
|
90
|
+
getDependents
|
|
91
|
+
};
|
|
92
|
+
return { state, api };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/validators.ts
|
|
96
|
+
var syncValidators = /* @__PURE__ */ new Map();
|
|
97
|
+
var asyncValidators = /* @__PURE__ */ new Map();
|
|
98
|
+
function registerValidator(name, fn) {
|
|
99
|
+
syncValidators.set(name, fn);
|
|
100
|
+
}
|
|
101
|
+
function registerAsyncValidator(name, fn) {
|
|
102
|
+
asyncValidators.set(name, fn);
|
|
103
|
+
}
|
|
104
|
+
function getValidator(name) {
|
|
105
|
+
return syncValidators.get(name);
|
|
106
|
+
}
|
|
107
|
+
function getAsyncValidator(name) {
|
|
108
|
+
return asyncValidators.get(name);
|
|
109
|
+
}
|
|
110
|
+
registerValidator("required", (value) => {
|
|
111
|
+
if (value == null || value === "" || Array.isArray(value) && value.length === 0) {
|
|
112
|
+
return {
|
|
113
|
+
valid: false,
|
|
114
|
+
error: { code: "required", message: "This field is required" }
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return { valid: true };
|
|
118
|
+
});
|
|
119
|
+
registerValidator("min", (value, args) => {
|
|
120
|
+
if (value != null && typeof args === "number" && value < args) {
|
|
121
|
+
return {
|
|
122
|
+
valid: false,
|
|
123
|
+
error: { code: "min", message: `Minimum value is ${args}`, args: { min: args, actual: value } }
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return { valid: true };
|
|
127
|
+
});
|
|
128
|
+
registerValidator("max", (value, args) => {
|
|
129
|
+
if (value != null && typeof args === "number" && value > args) {
|
|
130
|
+
return {
|
|
131
|
+
valid: false,
|
|
132
|
+
error: { code: "max", message: `Maximum value is ${args}`, args: { max: args, actual: value } }
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return { valid: true };
|
|
136
|
+
});
|
|
137
|
+
registerValidator("minLength", (value, args) => {
|
|
138
|
+
if (typeof value === "string" && typeof args === "number" && value.length < args) {
|
|
139
|
+
return {
|
|
140
|
+
valid: false,
|
|
141
|
+
error: { code: "minLength", message: `Minimum length is ${args}`, args: { requiredLength: args, actualLength: value.length } }
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return { valid: true };
|
|
145
|
+
});
|
|
146
|
+
registerValidator("maxLength", (value, args) => {
|
|
147
|
+
if (typeof value === "string" && typeof args === "number" && value.length > args) {
|
|
148
|
+
return {
|
|
149
|
+
valid: false,
|
|
150
|
+
error: { code: "maxLength", message: `Maximum length is ${args}`, args: { requiredLength: args, actualLength: value.length } }
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return { valid: true };
|
|
154
|
+
});
|
|
155
|
+
registerValidator("pattern", (value, args) => {
|
|
156
|
+
if (typeof value === "string" && typeof args === "string") {
|
|
157
|
+
try {
|
|
158
|
+
const regex = new RegExp(args);
|
|
159
|
+
if (!regex.test(value)) {
|
|
160
|
+
return {
|
|
161
|
+
valid: false,
|
|
162
|
+
error: { code: "pattern", message: "Invalid format", args: { requiredPattern: args, actualValue: value } }
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
} catch (e) {
|
|
166
|
+
return { valid: true };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return { valid: true };
|
|
170
|
+
});
|
|
171
|
+
registerValidator("regex", (value, args) => {
|
|
172
|
+
return getValidator("pattern")(value, args);
|
|
173
|
+
});
|
|
174
|
+
registerValidator("email", (value) => {
|
|
175
|
+
if (typeof value === "string" && value) {
|
|
176
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
177
|
+
if (!emailRegex.test(value)) {
|
|
178
|
+
return {
|
|
179
|
+
valid: false,
|
|
180
|
+
error: { code: "email", message: "Invalid email address" }
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { valid: true };
|
|
185
|
+
});
|
|
186
|
+
registerValidator("number", (value) => {
|
|
187
|
+
if (value != null && (typeof value !== "number" || !Number.isFinite(value))) {
|
|
188
|
+
return {
|
|
189
|
+
valid: false,
|
|
190
|
+
error: { code: "number", message: "Must be a number" }
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return { valid: true };
|
|
194
|
+
});
|
|
195
|
+
registerValidator("oneOf", (value, args) => {
|
|
196
|
+
if (args && Array.isArray(args) && !args.includes(value)) {
|
|
197
|
+
return {
|
|
198
|
+
valid: false,
|
|
199
|
+
error: { code: "oneOf", message: "Value must be one of the allowed options", args: { allowedValues: args } }
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
return { valid: true };
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// src/schema-guard.ts
|
|
206
|
+
function assertFeForm(x) {
|
|
207
|
+
if (!x || typeof x !== "object") {
|
|
208
|
+
throw new Error("FeForm must be an object");
|
|
209
|
+
}
|
|
210
|
+
if (!Array.isArray(x.sections)) {
|
|
211
|
+
throw new Error("FeForm.sections must be an array");
|
|
212
|
+
}
|
|
213
|
+
for (let i = 0; i < x.sections.length; i++) {
|
|
214
|
+
const section = x.sections[i];
|
|
215
|
+
const sectionPath = `sections[${i}]`;
|
|
216
|
+
if (!section || typeof section !== "object") {
|
|
217
|
+
throw new Error(`${sectionPath} must be an object`);
|
|
218
|
+
}
|
|
219
|
+
if (typeof section.id !== "string" || !section.id) {
|
|
220
|
+
throw new Error(`${sectionPath}.id must be a non-empty string`);
|
|
221
|
+
}
|
|
222
|
+
if (!Array.isArray(section.controls)) {
|
|
223
|
+
throw new Error(`${sectionPath}.controls must be an array`);
|
|
224
|
+
}
|
|
225
|
+
validateControls(section.controls, `${sectionPath}.controls`);
|
|
226
|
+
}
|
|
227
|
+
if (x.controls !== void 0) {
|
|
228
|
+
if (!Array.isArray(x.controls)) {
|
|
229
|
+
throw new Error("FeForm.controls must be an array if present");
|
|
230
|
+
}
|
|
231
|
+
validateControls(x.controls, "controls");
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function validateControls(controls, path) {
|
|
235
|
+
for (let i = 0; i < controls.length; i++) {
|
|
236
|
+
const control = controls[i];
|
|
237
|
+
const controlPath = `${path}[${i}]`;
|
|
238
|
+
if (!control || typeof control !== "object") {
|
|
239
|
+
throw new Error(`${controlPath} must be an object`);
|
|
240
|
+
}
|
|
241
|
+
if (typeof control.type !== "string" || !control.type) {
|
|
242
|
+
throw new Error(`${controlPath}.type must be a non-empty string`);
|
|
243
|
+
}
|
|
244
|
+
if (typeof control.name !== "string" || !control.name) {
|
|
245
|
+
throw new Error(`${controlPath}.name must be a non-empty string`);
|
|
246
|
+
}
|
|
247
|
+
if (control.controls && Array.isArray(control.controls)) {
|
|
248
|
+
validateControls(control.controls, `${controlPath}.controls`);
|
|
249
|
+
}
|
|
250
|
+
if (control.control && typeof control.control === "object") {
|
|
251
|
+
validateControls([control.control], `${controlPath}.control`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/engine.ts
|
|
257
|
+
function getValueByPath(values, path) {
|
|
258
|
+
if (!path) return void 0;
|
|
259
|
+
if (path in values) return values[path];
|
|
260
|
+
const parts = path.split(/[./]/).filter(Boolean);
|
|
261
|
+
let current = values;
|
|
262
|
+
for (const part of parts) {
|
|
263
|
+
if (current == null || typeof current !== "object") return void 0;
|
|
264
|
+
current = current[part];
|
|
265
|
+
}
|
|
266
|
+
return current;
|
|
267
|
+
}
|
|
268
|
+
function setValueByPath(values, path, value) {
|
|
269
|
+
if (!path) return;
|
|
270
|
+
values[path] = value;
|
|
271
|
+
}
|
|
272
|
+
function isBackendGroup(x) {
|
|
273
|
+
return x && Array.isArray(x.conditions);
|
|
274
|
+
}
|
|
275
|
+
function evalBackendSimple(values, c) {
|
|
276
|
+
const path = c.path || c.field;
|
|
277
|
+
const opName = String(c.condition || "").toLowerCase();
|
|
278
|
+
const value = getValueByPath(values, path || "");
|
|
279
|
+
switch (opName) {
|
|
280
|
+
case "eq":
|
|
281
|
+
return value === c.value;
|
|
282
|
+
case "neq":
|
|
283
|
+
return value !== c.value;
|
|
284
|
+
case "gt":
|
|
285
|
+
return value != null && c.value != null && value > c.value;
|
|
286
|
+
case "gte":
|
|
287
|
+
return value != null && c.value != null && value >= c.value;
|
|
288
|
+
case "lt":
|
|
289
|
+
return value != null && c.value != null && value < c.value;
|
|
290
|
+
case "lte":
|
|
291
|
+
return value != null && c.value != null && value <= c.value;
|
|
292
|
+
case "exists":
|
|
293
|
+
return value !== null && value !== void 0;
|
|
294
|
+
default:
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function evalBackendGroup(values, g) {
|
|
299
|
+
const op = String(g.operation || g.operator || "and").toLowerCase();
|
|
300
|
+
const list = (g.conditions || []).filter(Boolean);
|
|
301
|
+
if (op === "or") {
|
|
302
|
+
return list.some(
|
|
303
|
+
(cond) => isBackendGroup(cond) ? evalBackendGroup(values, cond) : evalBackendSimple(values, cond)
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
return list.every(
|
|
307
|
+
(cond) => isBackendGroup(cond) ? evalBackendGroup(values, cond) : evalBackendSimple(values, cond)
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
var FeFormEngine = class {
|
|
311
|
+
schema;
|
|
312
|
+
state;
|
|
313
|
+
runtime;
|
|
314
|
+
listeners = /* @__PURE__ */ new Map();
|
|
315
|
+
batchMode = false;
|
|
316
|
+
pendingEmits = /* @__PURE__ */ new Set();
|
|
317
|
+
pendingValueChanges = false;
|
|
318
|
+
pendingFieldStateChanges = /* @__PURE__ */ new Map();
|
|
319
|
+
pendingErrorsChanges = /* @__PURE__ */ new Map();
|
|
320
|
+
asyncValidators = /* @__PURE__ */ new Map();
|
|
321
|
+
constructor(schema, options = {}) {
|
|
322
|
+
assertFeForm(schema);
|
|
323
|
+
this.schema = schema;
|
|
324
|
+
const { state: runtimeState, api: runtimeAPI } = createRuntime();
|
|
325
|
+
this.runtime = runtimeAPI;
|
|
326
|
+
this.state = {
|
|
327
|
+
values: options.initialValues || this.extractInitialValues(schema),
|
|
328
|
+
errors: {},
|
|
329
|
+
touched: {},
|
|
330
|
+
dirty: {},
|
|
331
|
+
valid: false
|
|
332
|
+
};
|
|
333
|
+
this.initializeFieldStates();
|
|
334
|
+
this.buildDependencyGraph();
|
|
335
|
+
if (options.validateOnChange !== false) {
|
|
336
|
+
this.validate();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
initializeFieldStates() {
|
|
340
|
+
const processControl = (control, path = "") => {
|
|
341
|
+
const fullPath = path ? `${path}.${control.name}` : control.name;
|
|
342
|
+
const fieldState = this.runtime.ensureFieldState(fullPath);
|
|
343
|
+
if (control.required) {
|
|
344
|
+
fieldState.required = true;
|
|
345
|
+
} else if (control.validators) {
|
|
346
|
+
const hasRequired = control.validators.some(
|
|
347
|
+
(v) => "function" in v && v.function === "required"
|
|
348
|
+
);
|
|
349
|
+
fieldState.required = hasRequired;
|
|
350
|
+
}
|
|
351
|
+
fieldState.visible = true;
|
|
352
|
+
fieldState.enabled = true;
|
|
353
|
+
if ("controls" in control && control.controls) {
|
|
354
|
+
control.controls.forEach((c) => processControl(c, fullPath));
|
|
355
|
+
}
|
|
356
|
+
if ("control" in control && control.control) {
|
|
357
|
+
processControl(control.control, fullPath);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
this.schema.sections.forEach((section) => {
|
|
361
|
+
section.controls.forEach((control) => processControl(control));
|
|
362
|
+
});
|
|
363
|
+
if (this.schema.controls) {
|
|
364
|
+
this.schema.controls.forEach((control) => processControl(control));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
buildDependencyGraph() {
|
|
368
|
+
const processControl = (control, path = "") => {
|
|
369
|
+
const fullPath = path ? `${path}.${control.name}` : control.name;
|
|
370
|
+
if (control.condition) {
|
|
371
|
+
this.extractConditionPaths(control.condition, fullPath);
|
|
372
|
+
}
|
|
373
|
+
if ("controls" in control && control.controls) {
|
|
374
|
+
control.controls.forEach((c) => processControl(c, fullPath));
|
|
375
|
+
}
|
|
376
|
+
if ("control" in control && control.control) {
|
|
377
|
+
processControl(control.control, fullPath);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
this.schema.sections.forEach((section) => {
|
|
381
|
+
section.controls.forEach((control) => processControl(control));
|
|
382
|
+
});
|
|
383
|
+
if (this.schema.controls) {
|
|
384
|
+
this.schema.controls.forEach((control) => processControl(control));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
extractConditionPaths(condition, targetPath) {
|
|
388
|
+
if (condition.conditions) {
|
|
389
|
+
for (const cond of condition.conditions) {
|
|
390
|
+
if (cond.path) {
|
|
391
|
+
this.runtime.addDependency(cond.path, targetPath);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
reevaluateDependents(changedPath) {
|
|
397
|
+
const visited = /* @__PURE__ */ new Set();
|
|
398
|
+
const queue = [changedPath];
|
|
399
|
+
while (queue.length > 0) {
|
|
400
|
+
const currentPath = queue.shift();
|
|
401
|
+
if (visited.has(currentPath)) continue;
|
|
402
|
+
visited.add(currentPath);
|
|
403
|
+
const dependents = this.runtime.getDependents(currentPath);
|
|
404
|
+
for (const dependentPath of dependents) {
|
|
405
|
+
if (!visited.has(dependentPath)) {
|
|
406
|
+
queue.push(dependentPath);
|
|
407
|
+
this.evaluateFieldConditions(dependentPath);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
evaluateFieldConditions(fieldPath) {
|
|
413
|
+
const control = this.findControlByPath(fieldPath);
|
|
414
|
+
if (!control || !control.condition) {
|
|
415
|
+
if (this.batchMode) {
|
|
416
|
+
const state = this.runtime.getFieldState(fieldPath);
|
|
417
|
+
if (state) {
|
|
418
|
+
this.pendingFieldStateChanges.set(fieldPath, state);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
const condition = control.condition;
|
|
424
|
+
const result = this.evaluateBackendCondition(condition);
|
|
425
|
+
const fieldState = this.runtime.getFieldState(fieldPath);
|
|
426
|
+
if (fieldState) {
|
|
427
|
+
const wasVisible = fieldState.visible;
|
|
428
|
+
this.runtime.setVisible(fieldPath, result);
|
|
429
|
+
if (this.batchMode || wasVisible !== result) {
|
|
430
|
+
this.emitFieldStateChange(fieldPath);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
evaluateBackendCondition(condition) {
|
|
435
|
+
const values = this.state.values;
|
|
436
|
+
const results = condition.conditions.map((c) => {
|
|
437
|
+
const path = c.path || "";
|
|
438
|
+
const value = getValueByPath(values, path);
|
|
439
|
+
switch (c.condition) {
|
|
440
|
+
case "eq":
|
|
441
|
+
return value === c.value;
|
|
442
|
+
case "neq":
|
|
443
|
+
return value !== c.value;
|
|
444
|
+
case "gt":
|
|
445
|
+
return value != null && c.value != null && value > c.value;
|
|
446
|
+
case "gte":
|
|
447
|
+
return value != null && c.value != null && value >= c.value;
|
|
448
|
+
case "lt":
|
|
449
|
+
return value != null && c.value != null && value < c.value;
|
|
450
|
+
case "lte":
|
|
451
|
+
return value != null && c.value != null && value <= c.value;
|
|
452
|
+
case "exists":
|
|
453
|
+
return value !== null && value !== void 0;
|
|
454
|
+
default:
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
if (condition.operation === "or") {
|
|
459
|
+
return results.some((r) => r === true);
|
|
460
|
+
}
|
|
461
|
+
return results.every((r) => r === true);
|
|
462
|
+
}
|
|
463
|
+
findControlByPath(path) {
|
|
464
|
+
const parts = path.split(/[./]/).filter(Boolean);
|
|
465
|
+
const findInControls = (controls, index) => {
|
|
466
|
+
if (index >= parts.length) return null;
|
|
467
|
+
const name = parts[index];
|
|
468
|
+
const control = controls.find((c) => c.name === name);
|
|
469
|
+
if (!control) return null;
|
|
470
|
+
if (index === parts.length - 1) return control;
|
|
471
|
+
if ("controls" in control && control.controls) {
|
|
472
|
+
return findInControls(control.controls, index + 1);
|
|
473
|
+
}
|
|
474
|
+
if ("control" in control && control.control) {
|
|
475
|
+
return findInControls([control.control], index + 1);
|
|
476
|
+
}
|
|
477
|
+
return null;
|
|
478
|
+
};
|
|
479
|
+
for (const section of this.schema.sections) {
|
|
480
|
+
const found = findInControls(section.controls, 0);
|
|
481
|
+
if (found) return found;
|
|
482
|
+
}
|
|
483
|
+
if (this.schema.controls) {
|
|
484
|
+
const found = findInControls(this.schema.controls, 0);
|
|
485
|
+
if (found) return found;
|
|
486
|
+
}
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
emitFieldStateChange(path) {
|
|
490
|
+
const state = this.runtime.getFieldState(path);
|
|
491
|
+
if (!state) return;
|
|
492
|
+
if (this.batchMode) {
|
|
493
|
+
this.pendingFieldStateChanges.set(path, state);
|
|
494
|
+
this.pendingEmits.add("fieldStateChanges");
|
|
495
|
+
} else {
|
|
496
|
+
this.emit("fieldStateChanges", path, state);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
emitErrorsChange(path, errors) {
|
|
500
|
+
if (this.batchMode) {
|
|
501
|
+
this.pendingErrorsChanges.set(path, errors);
|
|
502
|
+
this.pendingEmits.add("errorsChanges");
|
|
503
|
+
} else {
|
|
504
|
+
this.emit("errorsChanges", path, errors);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
extractInitialValues(schema) {
|
|
508
|
+
const values = {};
|
|
509
|
+
const processControl = (control) => {
|
|
510
|
+
if ("value" in control && control.value !== void 0) {
|
|
511
|
+
values[control.name] = control.value;
|
|
512
|
+
}
|
|
513
|
+
if ("controls" in control && control.controls) {
|
|
514
|
+
control.controls.forEach(processControl);
|
|
515
|
+
}
|
|
516
|
+
if ("control" in control && control.control) {
|
|
517
|
+
processControl(control.control);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
schema.sections.forEach((section) => {
|
|
521
|
+
section.controls.forEach(processControl);
|
|
522
|
+
});
|
|
523
|
+
if (schema.controls) {
|
|
524
|
+
schema.controls.forEach(processControl);
|
|
525
|
+
}
|
|
526
|
+
return values;
|
|
527
|
+
}
|
|
528
|
+
get values() {
|
|
529
|
+
return {
|
|
530
|
+
get: (path) => getValueByPath(this.state.values, path),
|
|
531
|
+
set: (path, value) => {
|
|
532
|
+
setValueByPath(this.state.values, path, value);
|
|
533
|
+
this.state.dirty[path] = true;
|
|
534
|
+
this.runtime.setDirty(path, true);
|
|
535
|
+
if (this.batchMode) {
|
|
536
|
+
const state = this.runtime.getFieldState(path);
|
|
537
|
+
if (state) {
|
|
538
|
+
this.pendingFieldStateChanges.set(path, state);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
this.reevaluateDependents(path);
|
|
542
|
+
if (this.batchMode) {
|
|
543
|
+
this.pendingValueChanges = true;
|
|
544
|
+
this.pendingEmits.add("valueChanges");
|
|
545
|
+
} else {
|
|
546
|
+
this.emit("valueChanges", this.state.values);
|
|
547
|
+
this.validate();
|
|
548
|
+
}
|
|
549
|
+
},
|
|
550
|
+
getAll: () => ({ ...this.state.values })
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
get status() {
|
|
554
|
+
return {
|
|
555
|
+
valid: this.state.valid,
|
|
556
|
+
errors: { ...this.state.errors },
|
|
557
|
+
touched: { ...this.state.touched },
|
|
558
|
+
dirty: { ...this.state.dirty }
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
getFieldState(path) {
|
|
562
|
+
return this.runtime.ensureFieldState(path);
|
|
563
|
+
}
|
|
564
|
+
registerValidator(name, fn) {
|
|
565
|
+
registerValidator(name, fn);
|
|
566
|
+
}
|
|
567
|
+
registerAsyncValidator(name, fn) {
|
|
568
|
+
registerAsyncValidator(name, fn);
|
|
569
|
+
}
|
|
570
|
+
batch(fn) {
|
|
571
|
+
const wasBatching = this.batchMode;
|
|
572
|
+
this.batchMode = true;
|
|
573
|
+
this.pendingEmits.clear();
|
|
574
|
+
this.pendingValueChanges = false;
|
|
575
|
+
this.pendingFieldStateChanges.clear();
|
|
576
|
+
this.pendingErrorsChanges.clear();
|
|
577
|
+
try {
|
|
578
|
+
fn();
|
|
579
|
+
} finally {
|
|
580
|
+
this.batchMode = wasBatching;
|
|
581
|
+
if (!wasBatching) {
|
|
582
|
+
if (this.pendingValueChanges) {
|
|
583
|
+
this.emit("valueChanges", this.state.values);
|
|
584
|
+
}
|
|
585
|
+
const affectedPaths = /* @__PURE__ */ new Set();
|
|
586
|
+
for (const path of this.pendingFieldStateChanges.keys()) {
|
|
587
|
+
affectedPaths.add(path);
|
|
588
|
+
}
|
|
589
|
+
for (const path of affectedPaths) {
|
|
590
|
+
const state = this.runtime.getFieldState(path);
|
|
591
|
+
if (state) {
|
|
592
|
+
this.emit("fieldStateChanges", path, state);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
for (const [path, errors] of this.pendingErrorsChanges) {
|
|
596
|
+
this.emit("errorsChanges", path, errors);
|
|
597
|
+
}
|
|
598
|
+
this.validate();
|
|
599
|
+
for (const path of affectedPaths) {
|
|
600
|
+
const state = this.runtime.getFieldState(path);
|
|
601
|
+
if (state && !this.pendingFieldStateChanges.has(path)) {
|
|
602
|
+
this.emit("fieldStateChanges", path, state);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
on(event, listener) {
|
|
609
|
+
if (!this.listeners.has(event)) {
|
|
610
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
611
|
+
}
|
|
612
|
+
this.listeners.get(event).add(listener);
|
|
613
|
+
return () => {
|
|
614
|
+
this.listeners.get(event)?.delete(listener);
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
emit(event, ...args) {
|
|
618
|
+
this.listeners.get(event)?.forEach((listener) => {
|
|
619
|
+
try {
|
|
620
|
+
listener(...args);
|
|
621
|
+
} catch (error) {
|
|
622
|
+
console.error(`Error in ${event} listener:`, error);
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
validate() {
|
|
627
|
+
const errors = {};
|
|
628
|
+
let isValid = true;
|
|
629
|
+
const validateControl = (control, path = "") => {
|
|
630
|
+
const fullPath = path ? `${path}.${control.name}` : control.name;
|
|
631
|
+
const value = getValueByPath(this.state.values, fullPath);
|
|
632
|
+
const fieldErrors = [];
|
|
633
|
+
if (control.validators) {
|
|
634
|
+
for (const validator of control.validators) {
|
|
635
|
+
if ("function" in validator) {
|
|
636
|
+
const registryValidator = getValidator(validator.function);
|
|
637
|
+
if (registryValidator) {
|
|
638
|
+
const ctx = {
|
|
639
|
+
get: (p) => getValueByPath(this.state.values, p)
|
|
640
|
+
};
|
|
641
|
+
const result = registryValidator(value, validator.value, ctx);
|
|
642
|
+
if (!result.valid && result.error) {
|
|
643
|
+
errors[fullPath] = result.error.args || { [result.error.code]: true };
|
|
644
|
+
isValid = false;
|
|
645
|
+
fieldErrors.push({
|
|
646
|
+
code: result.error.code,
|
|
647
|
+
message: result.error.message,
|
|
648
|
+
args: result.error.args
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
} else {
|
|
652
|
+
const error = this.validateControlValidator(validator, value);
|
|
653
|
+
if (error) {
|
|
654
|
+
errors[fullPath] = error;
|
|
655
|
+
isValid = false;
|
|
656
|
+
fieldErrors.push({
|
|
657
|
+
code: validator.function,
|
|
658
|
+
args: error
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (control.validators) {
|
|
666
|
+
for (const validator of control.validators) {
|
|
667
|
+
if ("function" in validator) {
|
|
668
|
+
const asyncValidator = getAsyncValidator(validator.function);
|
|
669
|
+
if (asyncValidator && isValid && !fieldErrors.length) {
|
|
670
|
+
const existingController = this.asyncValidators.get(fullPath);
|
|
671
|
+
if (existingController) {
|
|
672
|
+
existingController.abort();
|
|
673
|
+
}
|
|
674
|
+
const fieldState2 = this.runtime.getFieldState(fullPath);
|
|
675
|
+
if (fieldState2) {
|
|
676
|
+
this.runtime.setStatus(fullPath, "PENDING");
|
|
677
|
+
this.emitFieldStateChange(fullPath);
|
|
678
|
+
}
|
|
679
|
+
const controller = new AbortController();
|
|
680
|
+
this.asyncValidators.set(fullPath, controller);
|
|
681
|
+
const ctx = {
|
|
682
|
+
get: (p) => getValueByPath(this.state.values, p),
|
|
683
|
+
signal: controller.signal
|
|
684
|
+
};
|
|
685
|
+
asyncValidator(value, validator.value, ctx).then((result) => {
|
|
686
|
+
if (!controller.signal.aborted && this.asyncValidators.get(fullPath) === controller) {
|
|
687
|
+
if (!result.valid && result.error) {
|
|
688
|
+
const currentErrors = { ...this.state.errors };
|
|
689
|
+
currentErrors[fullPath] = result.error.args || { [result.error.code]: true };
|
|
690
|
+
this.state.errors = currentErrors;
|
|
691
|
+
this.state.valid = false;
|
|
692
|
+
const asyncFieldErrors = [{
|
|
693
|
+
code: result.error.code,
|
|
694
|
+
message: result.error.message,
|
|
695
|
+
args: result.error.args
|
|
696
|
+
}];
|
|
697
|
+
this.runtime.setErrors(fullPath, asyncFieldErrors);
|
|
698
|
+
this.runtime.setStatus(fullPath, "INVALID");
|
|
699
|
+
this.emitErrorsChange(fullPath, asyncFieldErrors);
|
|
700
|
+
this.emitFieldStateChange(fullPath);
|
|
701
|
+
this.emit("statusChanges", { valid: false, errors: currentErrors });
|
|
702
|
+
} else {
|
|
703
|
+
this.runtime.setStatus(fullPath, "VALID");
|
|
704
|
+
this.emitFieldStateChange(fullPath);
|
|
705
|
+
}
|
|
706
|
+
this.asyncValidators.delete(fullPath);
|
|
707
|
+
}
|
|
708
|
+
}).catch((error) => {
|
|
709
|
+
if (!controller.signal.aborted) {
|
|
710
|
+
console.error(`Async validator error for ${fullPath}:`, error);
|
|
711
|
+
this.runtime.setStatus(fullPath, "VALID");
|
|
712
|
+
this.asyncValidators.delete(fullPath);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
if ("validation" in control && control.validation) {
|
|
720
|
+
const error = this.validateBackend(control.validation, fullPath);
|
|
721
|
+
if (error) {
|
|
722
|
+
errors[fullPath] = error;
|
|
723
|
+
isValid = false;
|
|
724
|
+
fieldErrors.push({
|
|
725
|
+
code: "backendValidation",
|
|
726
|
+
args: error
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const fieldState = this.runtime.getFieldState(fullPath);
|
|
731
|
+
if (fieldState) {
|
|
732
|
+
const hadErrors = fieldState.errors.length > 0;
|
|
733
|
+
this.runtime.setErrors(fullPath, fieldErrors);
|
|
734
|
+
this.runtime.setStatus(fullPath, fieldErrors.length > 0 ? "INVALID" : "VALID");
|
|
735
|
+
if (hadErrors !== fieldErrors.length > 0) {
|
|
736
|
+
this.emitErrorsChange(fullPath, fieldErrors);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if ("controls" in control && control.controls) {
|
|
740
|
+
control.controls.forEach((c) => validateControl(c, fullPath));
|
|
741
|
+
}
|
|
742
|
+
if ("control" in control && control.control) {
|
|
743
|
+
validateControl(control.control, fullPath);
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
this.schema.sections.forEach((section) => {
|
|
747
|
+
section.controls.forEach((control) => validateControl(control));
|
|
748
|
+
});
|
|
749
|
+
if (this.schema.controls) {
|
|
750
|
+
this.schema.controls.forEach((control) => validateControl(control));
|
|
751
|
+
}
|
|
752
|
+
this.state.errors = errors;
|
|
753
|
+
this.state.valid = isValid;
|
|
754
|
+
if (!this.batchMode) {
|
|
755
|
+
this.emit("statusChanges", { valid: isValid, errors });
|
|
756
|
+
} else {
|
|
757
|
+
this.pendingEmits.add("statusChanges");
|
|
758
|
+
}
|
|
759
|
+
return isValid;
|
|
760
|
+
}
|
|
761
|
+
validateControlValidator(validator, value) {
|
|
762
|
+
switch (validator.function) {
|
|
763
|
+
case "required":
|
|
764
|
+
if (value == null || value === "" || Array.isArray(value) && value.length === 0) {
|
|
765
|
+
return { required: true };
|
|
766
|
+
}
|
|
767
|
+
break;
|
|
768
|
+
case "min":
|
|
769
|
+
if (typeof validator.value === "number" && value != null && value < validator.value) {
|
|
770
|
+
return { min: { min: validator.value, actual: value } };
|
|
771
|
+
}
|
|
772
|
+
break;
|
|
773
|
+
case "max":
|
|
774
|
+
if (typeof validator.value === "number" && value != null && typeof value === "number" && value > validator.value) {
|
|
775
|
+
return { max: { max: validator.value, actual: value } };
|
|
776
|
+
}
|
|
777
|
+
break;
|
|
778
|
+
case "minLength":
|
|
779
|
+
if (typeof validator.value === "number" && typeof value === "string" && value.length < validator.value) {
|
|
780
|
+
return { minLength: { requiredLength: validator.value, actualLength: value.length } };
|
|
781
|
+
}
|
|
782
|
+
break;
|
|
783
|
+
case "maxLength":
|
|
784
|
+
if (typeof validator.value === "number" && (typeof value === "string" && value.length > validator.value)) {
|
|
785
|
+
return { maxLength: { requiredLength: validator.value, actualLength: value.length } };
|
|
786
|
+
}
|
|
787
|
+
break;
|
|
788
|
+
case "pattern":
|
|
789
|
+
if (typeof validator.value === "string" && typeof value === "string" && !new RegExp(validator.value).test(value)) {
|
|
790
|
+
return { pattern: { requiredPattern: validator.value, actualValue: value } };
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
validateBackend(validation, fieldPath) {
|
|
797
|
+
const values = this.state.values;
|
|
798
|
+
const ok = isBackendGroup(validation) ? evalBackendGroup(values, validation) : evalBackendSimple(values, validation);
|
|
799
|
+
if (!ok) {
|
|
800
|
+
return { backendValidation: { message: "Validation failed", validation } };
|
|
801
|
+
}
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
markTouched(path) {
|
|
805
|
+
this.state.touched[path] = true;
|
|
806
|
+
this.runtime.setTouched(path, true);
|
|
807
|
+
this.emitFieldStateChange(path);
|
|
808
|
+
if (!this.batchMode) {
|
|
809
|
+
this.emit("statusChanges", { valid: this.state.valid, errors: this.state.errors });
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
submit() {
|
|
813
|
+
if (!this.validate()) {
|
|
814
|
+
return null;
|
|
815
|
+
}
|
|
816
|
+
const values = this.values.getAll();
|
|
817
|
+
this.emit("submit", values);
|
|
818
|
+
return values;
|
|
819
|
+
}
|
|
820
|
+
reset(values) {
|
|
821
|
+
this.state.values = values || this.extractInitialValues(this.schema);
|
|
822
|
+
this.state.errors = {};
|
|
823
|
+
this.state.touched = {};
|
|
824
|
+
this.state.dirty = {};
|
|
825
|
+
this.state.valid = false;
|
|
826
|
+
this.emit("valueChanges", this.state.values);
|
|
827
|
+
this.validate();
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
function createEngine(schema, options) {
|
|
831
|
+
return new FeFormEngine(schema, options);
|
|
832
|
+
}
|
|
833
|
+
export {
|
|
834
|
+
FeFormEngine,
|
|
835
|
+
assertFeForm,
|
|
836
|
+
createEngine,
|
|
837
|
+
createRuntime,
|
|
838
|
+
getAsyncValidator,
|
|
839
|
+
getValidator,
|
|
840
|
+
registerAsyncValidator,
|
|
841
|
+
registerValidator
|
|
842
|
+
};
|
|
843
|
+
//# sourceMappingURL=index.js.map
|