@remoteoss/json-schema-form 0.5.0-dev.20230810172154 → 0.5.1-dev.20230913083845
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/CHANGELOG.md +19 -0
- package/dist/index.cjs +379 -37
- package/dist/index.js +379 -37
- package/dist/standalone.js +757 -37
- package/package.json +2 -1
- package/src/tests/const.test.js +98 -0
- package/src/tests/createHeadlessForm.test.js +34 -12
- package/src/tests/helpers.custom.js +0 -1
- package/src/tests/helpers.js +5 -5
- package/src/tests/jsonLogic.fixtures.js +441 -0
- package/src/tests/jsonLogic.test.js +348 -0
- package/src/tests/testUtils.js +11 -0
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
3
|
Copyright (c) 2023 Remote Technology, Inc.
|
|
4
|
-
NPM Package: @remoteoss/json-schema-form@0.5.
|
|
5
|
-
Generated:
|
|
4
|
+
NPM Package: @remoteoss/json-schema-form@0.5.1-dev.20230913083845
|
|
5
|
+
Generated: Wed, 13 Sep 2023 08:39:03 GMT
|
|
6
6
|
|
|
7
7
|
MIT License
|
|
8
8
|
|
|
@@ -55,6 +55,13 @@ function convertDiskSizeFromTo(from, to) {
|
|
|
55
55
|
return value * Math.pow(1024, units.indexOf(from.toLowerCase())) / Math.pow(1024, units.indexOf(to.toLowerCase()));
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
|
+
function containsHTML(str = "") {
|
|
59
|
+
return /<[a-z][\s\S]*>/i.test(str);
|
|
60
|
+
}
|
|
61
|
+
function wrapWithSpan(html, properties = {}) {
|
|
62
|
+
const attributes = Object.entries(properties).reduce((acc, [key, value]) => `${acc}${key}="${value}" `, "").trim();
|
|
63
|
+
return `<span ${attributes}>${html}</span>`;
|
|
64
|
+
}
|
|
58
65
|
function hasProperty(object2, propertyName) {
|
|
59
66
|
return Object.prototype.hasOwnProperty.call(object2, propertyName);
|
|
60
67
|
}
|
|
@@ -368,6 +375,252 @@ import flow from "lodash/flow";
|
|
|
368
375
|
import noop from "lodash/noop";
|
|
369
376
|
import { randexp } from "randexp";
|
|
370
377
|
import { string, number, boolean, object, array } from "yup";
|
|
378
|
+
|
|
379
|
+
// src/jsonLogic.js
|
|
380
|
+
import jsonLogic from "json-logic-js";
|
|
381
|
+
function createValidationChecker(schema) {
|
|
382
|
+
const scopes = /* @__PURE__ */ new Map();
|
|
383
|
+
function createScopes(jsonSchema, key = "root") {
|
|
384
|
+
const sampleEmptyObject = buildSampleEmptyObject(schema);
|
|
385
|
+
scopes.set(key, createValidationsScope(jsonSchema));
|
|
386
|
+
Object.entries(jsonSchema?.properties ?? {}).filter(([, property]) => property.type === "object" || property.type === "array").forEach(([key2, property]) => {
|
|
387
|
+
if (property.type === "array") {
|
|
388
|
+
createScopes(property.items, `${key2}[]`);
|
|
389
|
+
} else {
|
|
390
|
+
createScopes(property, key2);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
validateInlineRules(jsonSchema, sampleEmptyObject);
|
|
394
|
+
}
|
|
395
|
+
createScopes(schema);
|
|
396
|
+
return {
|
|
397
|
+
scopes,
|
|
398
|
+
getScope(name = "root") {
|
|
399
|
+
return scopes.get(name);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function createValidationsScope(schema) {
|
|
404
|
+
const validationMap = /* @__PURE__ */ new Map();
|
|
405
|
+
const computedValuesMap = /* @__PURE__ */ new Map();
|
|
406
|
+
const logic = schema?.["x-jsf-logic"] ?? {
|
|
407
|
+
validations: {},
|
|
408
|
+
computedValues: {}
|
|
409
|
+
};
|
|
410
|
+
const validations = Object.entries(logic.validations ?? {});
|
|
411
|
+
const computedValues = Object.entries(logic.computedValues ?? {});
|
|
412
|
+
const sampleEmptyObject = buildSampleEmptyObject(schema);
|
|
413
|
+
validations.forEach(([id, validation]) => {
|
|
414
|
+
if (!validation.rule) {
|
|
415
|
+
throw Error(`[json-schema-form] json-logic error: Validation "${id}" has missing rule.`);
|
|
416
|
+
}
|
|
417
|
+
checkRuleIntegrity(validation.rule, id, sampleEmptyObject);
|
|
418
|
+
validationMap.set(id, validation);
|
|
419
|
+
});
|
|
420
|
+
computedValues.forEach(([id, computedValue]) => {
|
|
421
|
+
if (!computedValue.rule) {
|
|
422
|
+
throw Error(`[json-schema-form] json-logic error: Computed value "${id}" has missing rule.`);
|
|
423
|
+
}
|
|
424
|
+
checkRuleIntegrity(computedValue.rule, id, sampleEmptyObject);
|
|
425
|
+
computedValuesMap.set(id, computedValue);
|
|
426
|
+
});
|
|
427
|
+
function validate(rule, values) {
|
|
428
|
+
return jsonLogic.apply(rule, replaceUndefinedValuesWithNulls(values));
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
validationMap,
|
|
432
|
+
computedValuesMap,
|
|
433
|
+
validate,
|
|
434
|
+
applyValidationRuleInCondition(id, values) {
|
|
435
|
+
const validation = validationMap.get(id);
|
|
436
|
+
return validate(validation.rule, values);
|
|
437
|
+
},
|
|
438
|
+
applyComputedValueInField(id, values, fieldName) {
|
|
439
|
+
const validation = computedValuesMap.get(id);
|
|
440
|
+
if (validation === void 0) {
|
|
441
|
+
throw Error(
|
|
442
|
+
`[json-schema-form] json-logic error: Computed value "${id}" doesn't exist in field "${fieldName}".`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
return validate(validation.rule, values);
|
|
446
|
+
},
|
|
447
|
+
applyComputedValueRuleInCondition(id, values) {
|
|
448
|
+
const validation = computedValuesMap.get(id);
|
|
449
|
+
return validate(validation.rule, values);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
function replaceUndefinedValuesWithNulls(values = {}) {
|
|
454
|
+
return Object.entries(values).reduce((prev, [key, value]) => {
|
|
455
|
+
return { ...prev, [key]: value === void 0 ? null : value };
|
|
456
|
+
}, {});
|
|
457
|
+
}
|
|
458
|
+
function yupSchemaWithCustomJSONLogic({ field, logic, config, id }) {
|
|
459
|
+
const { parentID = "root" } = config;
|
|
460
|
+
const validation = logic.getScope(parentID).validationMap.get(id);
|
|
461
|
+
if (validation === void 0) {
|
|
462
|
+
throw Error(
|
|
463
|
+
`[json-schema-form] json-logic error: "${field.name}" required validation "${id}" doesn't exist.`
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
return (yupSchema) => yupSchema.test(
|
|
467
|
+
`${field.name}-validation-${id}`,
|
|
468
|
+
validation?.errorMessage ?? "This field is invalid.",
|
|
469
|
+
(value, { parent }) => {
|
|
470
|
+
if (value === void 0 && !field.required)
|
|
471
|
+
return true;
|
|
472
|
+
return jsonLogic.apply(validation.rule, parent);
|
|
473
|
+
}
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
var HANDLEBARS_REGEX = /\{\{([^{}]+)\}\}/g;
|
|
477
|
+
function replaceHandlebarsTemplates({
|
|
478
|
+
value: toReplace,
|
|
479
|
+
logic,
|
|
480
|
+
formValues,
|
|
481
|
+
parentID,
|
|
482
|
+
name: fieldName
|
|
483
|
+
}) {
|
|
484
|
+
if (typeof toReplace === "string") {
|
|
485
|
+
return toReplace.replace(HANDLEBARS_REGEX, (match, key) => {
|
|
486
|
+
return logic.getScope(parentID).applyComputedValueInField(key.trim(), formValues, fieldName);
|
|
487
|
+
});
|
|
488
|
+
} else if (typeof toReplace === "object") {
|
|
489
|
+
const { value, ...rules } = toReplace;
|
|
490
|
+
if (Object.keys(rules).length > 1 && !value) {
|
|
491
|
+
throw Error("Cannot define multiple rules without a template string with key `value`.");
|
|
492
|
+
}
|
|
493
|
+
const computedTemplateValue = Object.entries(rules).reduce((prev, [key, rule]) => {
|
|
494
|
+
const computedValue = logic.getScope(parentID).evaluateValidation(rule, formValues);
|
|
495
|
+
return prev.replaceAll(`{{${key}}}`, computedValue);
|
|
496
|
+
}, value);
|
|
497
|
+
return computedTemplateValue.replace(/\{\{([^{}]+)\}\}/g, (match, key) => {
|
|
498
|
+
return logic.getScope(parentID).applyComputedValueInField(key.trim(), formValues, fieldName);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
return toReplace;
|
|
502
|
+
}
|
|
503
|
+
function calculateComputedAttributes(fieldParams, { parentID = "root" } = {}) {
|
|
504
|
+
return ({ logic, isRequired, config, formValues }) => {
|
|
505
|
+
const { name, computedAttributes } = fieldParams;
|
|
506
|
+
const attributes = Object.fromEntries(
|
|
507
|
+
Object.entries(computedAttributes).map(handleComputedAttribute(logic, formValues, parentID, name)).filter(([, value]) => value !== null)
|
|
508
|
+
);
|
|
509
|
+
return {
|
|
510
|
+
...attributes,
|
|
511
|
+
schema: buildYupSchema(
|
|
512
|
+
{ ...fieldParams, ...attributes, required: isRequired },
|
|
513
|
+
config,
|
|
514
|
+
logic
|
|
515
|
+
)
|
|
516
|
+
};
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
function handleComputedAttribute(logic, formValues, parentID, name) {
|
|
520
|
+
return ([key, value]) => {
|
|
521
|
+
switch (key) {
|
|
522
|
+
case "description":
|
|
523
|
+
return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
|
|
524
|
+
case "title":
|
|
525
|
+
return ["label", replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
|
|
526
|
+
case "x-jsf-errorMessage":
|
|
527
|
+
return [
|
|
528
|
+
"errorMessage",
|
|
529
|
+
handleNestedObjectForComputedValues(value, formValues, parentID, logic, name)
|
|
530
|
+
];
|
|
531
|
+
case "x-jsf-presentation": {
|
|
532
|
+
if (value.statement) {
|
|
533
|
+
return [
|
|
534
|
+
"statement",
|
|
535
|
+
handleNestedObjectForComputedValues(value.statement, formValues, parentID, logic, name)
|
|
536
|
+
];
|
|
537
|
+
}
|
|
538
|
+
return [
|
|
539
|
+
key,
|
|
540
|
+
handleNestedObjectForComputedValues(value.statement, formValues, parentID, logic, name)
|
|
541
|
+
];
|
|
542
|
+
}
|
|
543
|
+
case "const":
|
|
544
|
+
default:
|
|
545
|
+
return [key, logic.getScope(parentID).applyComputedValueInField(value, formValues, name)];
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
function handleNestedObjectForComputedValues(values, formValues, parentID, logic, name) {
|
|
550
|
+
return Object.fromEntries(
|
|
551
|
+
Object.entries(values).map(([key, value]) => {
|
|
552
|
+
return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
|
|
553
|
+
})
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
function buildSampleEmptyObject(schema = {}) {
|
|
557
|
+
const sample = {};
|
|
558
|
+
if (typeof schema !== "object" || !schema.properties) {
|
|
559
|
+
return schema;
|
|
560
|
+
}
|
|
561
|
+
for (const key in schema.properties) {
|
|
562
|
+
if (schema.properties[key].type === "object") {
|
|
563
|
+
sample[key] = buildSampleEmptyObject(schema.properties[key]);
|
|
564
|
+
} else if (schema.properties[key].type === "array") {
|
|
565
|
+
const itemSchema = schema.properties[key].items;
|
|
566
|
+
sample[key] = buildSampleEmptyObject(itemSchema);
|
|
567
|
+
} else {
|
|
568
|
+
sample[key] = true;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return sample;
|
|
572
|
+
}
|
|
573
|
+
function validateInlineRules(jsonSchema, sampleEmptyObject) {
|
|
574
|
+
const properties = (jsonSchema?.properties || jsonSchema?.items?.properties) ?? {};
|
|
575
|
+
Object.entries(properties).filter(([, property]) => property["x-jsf-logic-computedAttrs"] !== void 0).forEach(([fieldName, property]) => {
|
|
576
|
+
Object.entries(property["x-jsf-logic-computedAttrs"]).filter(([, value]) => typeof value === "object").forEach(([key, item]) => {
|
|
577
|
+
Object.values(item).forEach((rule) => {
|
|
578
|
+
checkRuleIntegrity(
|
|
579
|
+
rule,
|
|
580
|
+
fieldName,
|
|
581
|
+
sampleEmptyObject,
|
|
582
|
+
(item2) => `[json-schema-form] json-logic error: fieldName "${item2.var}" doesn't exist in field "${fieldName}.x-jsf-logic-computedAttrs.${key}".`
|
|
583
|
+
);
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
function checkRuleIntegrity(rule, id, data, errorMessage = (item) => `[json-schema-form] json-logic error: rule "${id}" has no variable "${item.var}".`) {
|
|
589
|
+
Object.entries(rule ?? {}).map(([operator, subRule]) => {
|
|
590
|
+
if (!Array.isArray(subRule) && subRule !== null && subRule !== void 0)
|
|
591
|
+
return;
|
|
592
|
+
throwIfUnknownOperator(operator, subRule, id);
|
|
593
|
+
subRule.map((item) => {
|
|
594
|
+
const isVar = item !== null && typeof item === "object" && Object.hasOwn(item, "var");
|
|
595
|
+
if (isVar) {
|
|
596
|
+
const exists = jsonLogic.apply({ var: removeIndicesFromPath(item.var) }, data);
|
|
597
|
+
if (exists === null) {
|
|
598
|
+
throw Error(errorMessage(item));
|
|
599
|
+
}
|
|
600
|
+
} else {
|
|
601
|
+
checkRuleIntegrity(item, id, data);
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
function throwIfUnknownOperator(operator, subRule, id) {
|
|
607
|
+
try {
|
|
608
|
+
jsonLogic.apply({ [operator]: subRule });
|
|
609
|
+
} catch (e) {
|
|
610
|
+
if (e.message === `Unrecognized operation ${operator}`) {
|
|
611
|
+
throw Error(
|
|
612
|
+
`[json-schema-form] json-logic error: in "${id}" rule there is an unknown operator "${operator}".`
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
var regexToGetIndices = /\.\d+\./g;
|
|
618
|
+
function removeIndicesFromPath(path) {
|
|
619
|
+
const intermediatePath = path.replace(regexToGetIndices, ".");
|
|
620
|
+
return intermediatePath.replace(/\.\d+$/, "");
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// src/yupSchema.js
|
|
371
624
|
var DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
|
372
625
|
var baseString = string().trim();
|
|
373
626
|
var todayDateHint = (/* @__PURE__ */ new Date()).toISOString().substring(0, 10);
|
|
@@ -490,7 +743,7 @@ var getYupSchema = ({ inputType, ...field }) => {
|
|
|
490
743
|
}
|
|
491
744
|
return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
|
|
492
745
|
};
|
|
493
|
-
function buildYupSchema(field, config) {
|
|
746
|
+
function buildYupSchema(field, config, logic) {
|
|
494
747
|
const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
|
|
495
748
|
const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
|
|
496
749
|
let baseSchema;
|
|
@@ -563,6 +816,13 @@ function buildYupSchema(field, config) {
|
|
|
563
816
|
}) : true
|
|
564
817
|
);
|
|
565
818
|
}
|
|
819
|
+
function withConst(yupSchema) {
|
|
820
|
+
return yupSchema.test(
|
|
821
|
+
"isConst",
|
|
822
|
+
errorMessage.const ?? errorMessageFromConfig.const ?? `The only accepted value is ${propertyFields.const}.`,
|
|
823
|
+
(value) => propertyFields.required === false && value === void 0 || value === null || value === propertyFields.const
|
|
824
|
+
);
|
|
825
|
+
}
|
|
566
826
|
function withBaseSchema() {
|
|
567
827
|
const customErrorMsg = errorMessage.type || errorMessageFromConfig.type;
|
|
568
828
|
if (customErrorMsg) {
|
|
@@ -630,6 +890,14 @@ function buildYupSchema(field, config) {
|
|
|
630
890
|
if (propertyFields.accept) {
|
|
631
891
|
validators.push(withFileFormat);
|
|
632
892
|
}
|
|
893
|
+
if (propertyFields.const) {
|
|
894
|
+
validators.push(withConst);
|
|
895
|
+
}
|
|
896
|
+
if (propertyFields.requiredValidations) {
|
|
897
|
+
propertyFields.requiredValidations.forEach(
|
|
898
|
+
(id) => validators.push(yupSchemaWithCustomJSONLogic({ field, id, logic, config }))
|
|
899
|
+
);
|
|
900
|
+
}
|
|
633
901
|
return flow(validators);
|
|
634
902
|
}
|
|
635
903
|
function getNoSortEdges(fields = []) {
|
|
@@ -669,8 +937,8 @@ function hasType(type, typeName) {
|
|
|
669
937
|
function getField(fieldName, fields) {
|
|
670
938
|
return fields.find(({ name }) => name === fieldName);
|
|
671
939
|
}
|
|
672
|
-
function validateFieldSchema(field, value) {
|
|
673
|
-
const validator = buildYupSchema(field);
|
|
940
|
+
function validateFieldSchema(field, value, logic) {
|
|
941
|
+
const validator = buildYupSchema(field, {}, logic);
|
|
674
942
|
return validator().isValidSync(value);
|
|
675
943
|
}
|
|
676
944
|
function compareFormValueWithSchemaValue(formValue, schemaValue) {
|
|
@@ -706,7 +974,16 @@ function getPrefillSubFieldValues(field, defaultValues, parentFieldKeyPath) {
|
|
|
706
974
|
initialValue[field.name] = subFieldValues;
|
|
707
975
|
}
|
|
708
976
|
} else {
|
|
709
|
-
|
|
977
|
+
if (typeof initialValue !== "object") {
|
|
978
|
+
console.warn(
|
|
979
|
+
`Field "${parentFieldKeyPath}"'s value is "${initialValue}", but should be type object.`
|
|
980
|
+
);
|
|
981
|
+
initialValue = getPrefillValues([field], {
|
|
982
|
+
// TODO nested fieldsets are not handled
|
|
983
|
+
});
|
|
984
|
+
} else {
|
|
985
|
+
initialValue = getPrefillValues([field], initialValue);
|
|
986
|
+
}
|
|
710
987
|
}
|
|
711
988
|
return initialValue;
|
|
712
989
|
}
|
|
@@ -735,7 +1012,7 @@ function getPrefillValues(fields, initialValues = {}) {
|
|
|
735
1012
|
});
|
|
736
1013
|
return initialValues;
|
|
737
1014
|
}
|
|
738
|
-
function updateField(field, requiredFields, node, formValues) {
|
|
1015
|
+
function updateField(field, requiredFields, node, formValues, logic, config) {
|
|
739
1016
|
if (!field) {
|
|
740
1017
|
return;
|
|
741
1018
|
}
|
|
@@ -757,6 +1034,17 @@ function updateField(field, requiredFields, node, formValues) {
|
|
|
757
1034
|
}
|
|
758
1035
|
}
|
|
759
1036
|
});
|
|
1037
|
+
if (field.getComputedAttributes) {
|
|
1038
|
+
const computedFieldValues = field.getComputedAttributes({
|
|
1039
|
+
field,
|
|
1040
|
+
isRequired: fieldIsRequired,
|
|
1041
|
+
node,
|
|
1042
|
+
formValues,
|
|
1043
|
+
config,
|
|
1044
|
+
logic
|
|
1045
|
+
});
|
|
1046
|
+
updateValues(computedFieldValues);
|
|
1047
|
+
}
|
|
760
1048
|
if (field.calculateConditionalProperties) {
|
|
761
1049
|
const newFieldValues = field.calculateConditionalProperties(fieldIsRequired, node);
|
|
762
1050
|
updateValues(newFieldValues);
|
|
@@ -770,33 +1058,46 @@ function updateField(field, requiredFields, node, formValues) {
|
|
|
770
1058
|
updateValues(newFieldValues);
|
|
771
1059
|
}
|
|
772
1060
|
}
|
|
773
|
-
function processNode(
|
|
1061
|
+
function processNode({
|
|
1062
|
+
node,
|
|
1063
|
+
formValues,
|
|
1064
|
+
formFields,
|
|
1065
|
+
accRequired = /* @__PURE__ */ new Set(),
|
|
1066
|
+
parentID = "root",
|
|
1067
|
+
logic
|
|
1068
|
+
}) {
|
|
774
1069
|
const requiredFields = new Set(accRequired);
|
|
775
1070
|
Object.keys(node.properties ?? []).forEach((fieldName) => {
|
|
776
1071
|
const field = getField(fieldName, formFields);
|
|
777
|
-
updateField(field, requiredFields, node, formValues);
|
|
1072
|
+
updateField(field, requiredFields, node, formValues, logic, { parentID });
|
|
778
1073
|
});
|
|
779
1074
|
node.required?.forEach((fieldName) => {
|
|
780
1075
|
requiredFields.add(fieldName);
|
|
781
|
-
updateField(getField(fieldName, formFields), requiredFields, node, formValues
|
|
1076
|
+
updateField(getField(fieldName, formFields), requiredFields, node, formValues, logic, {
|
|
1077
|
+
parentID
|
|
1078
|
+
});
|
|
782
1079
|
});
|
|
783
1080
|
if (node.if) {
|
|
784
|
-
const matchesCondition = checkIfConditionMatches(node, formValues, formFields);
|
|
1081
|
+
const matchesCondition = checkIfConditionMatches(node, formValues, formFields, logic);
|
|
785
1082
|
if (matchesCondition && node.then) {
|
|
786
|
-
const { required: branchRequired } = processNode(
|
|
787
|
-
node.then,
|
|
1083
|
+
const { required: branchRequired } = processNode({
|
|
1084
|
+
node: node.then,
|
|
788
1085
|
formValues,
|
|
789
1086
|
formFields,
|
|
790
|
-
requiredFields
|
|
791
|
-
|
|
1087
|
+
accRequired: requiredFields,
|
|
1088
|
+
parentID,
|
|
1089
|
+
logic
|
|
1090
|
+
});
|
|
792
1091
|
branchRequired.forEach((field) => requiredFields.add(field));
|
|
793
1092
|
} else if (node.else) {
|
|
794
|
-
const { required: branchRequired } = processNode(
|
|
795
|
-
node.else,
|
|
1093
|
+
const { required: branchRequired } = processNode({
|
|
1094
|
+
node: node.else,
|
|
796
1095
|
formValues,
|
|
797
1096
|
formFields,
|
|
798
|
-
requiredFields
|
|
799
|
-
|
|
1097
|
+
accRequired: requiredFields,
|
|
1098
|
+
parentID,
|
|
1099
|
+
logic
|
|
1100
|
+
});
|
|
800
1101
|
branchRequired.forEach((field) => requiredFields.add(field));
|
|
801
1102
|
}
|
|
802
1103
|
}
|
|
@@ -808,12 +1109,21 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
|
|
|
808
1109
|
node.anyOf.forEach(({ required = [] }) => {
|
|
809
1110
|
required.forEach((fieldName) => {
|
|
810
1111
|
const field = getField(fieldName, formFields);
|
|
811
|
-
updateField(field, requiredFields, node, formValues);
|
|
1112
|
+
updateField(field, requiredFields, node, formValues, logic, { parentID });
|
|
812
1113
|
});
|
|
813
1114
|
});
|
|
814
1115
|
}
|
|
815
1116
|
if (node.allOf) {
|
|
816
|
-
node.allOf.map(
|
|
1117
|
+
node.allOf.map(
|
|
1118
|
+
(allOfNode) => processNode({
|
|
1119
|
+
node: allOfNode,
|
|
1120
|
+
formValues,
|
|
1121
|
+
formFields,
|
|
1122
|
+
accRequired: requiredFields,
|
|
1123
|
+
parentID,
|
|
1124
|
+
logic
|
|
1125
|
+
})
|
|
1126
|
+
).forEach(({ required: allOfItemRequired }) => {
|
|
817
1127
|
allOfItemRequired.forEach(requiredFields.add, requiredFields);
|
|
818
1128
|
});
|
|
819
1129
|
}
|
|
@@ -821,7 +1131,13 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
|
|
|
821
1131
|
Object.entries(node.properties).forEach(([name, nestedNode]) => {
|
|
822
1132
|
const inputType = getInputType(nestedNode);
|
|
823
1133
|
if (inputType === supportedTypes.FIELDSET) {
|
|
824
|
-
processNode(
|
|
1134
|
+
processNode({
|
|
1135
|
+
node: nestedNode,
|
|
1136
|
+
formValues: formValues[name] || {},
|
|
1137
|
+
formFields: getField(name, formFields).fields,
|
|
1138
|
+
parentID: name,
|
|
1139
|
+
logic
|
|
1140
|
+
});
|
|
825
1141
|
}
|
|
826
1142
|
});
|
|
827
1143
|
}
|
|
@@ -839,11 +1155,11 @@ function clearValuesIfNotVisible(fields, formValues) {
|
|
|
839
1155
|
}
|
|
840
1156
|
});
|
|
841
1157
|
}
|
|
842
|
-
function updateFieldsProperties(fields, formValues, jsonSchema) {
|
|
1158
|
+
function updateFieldsProperties(fields, formValues, jsonSchema, logic) {
|
|
843
1159
|
if (!jsonSchema?.properties) {
|
|
844
1160
|
return;
|
|
845
1161
|
}
|
|
846
|
-
processNode(jsonSchema, formValues, fields);
|
|
1162
|
+
processNode({ node: jsonSchema, formValues, formFields: fields, logic });
|
|
847
1163
|
clearValuesIfNotVisible(fields, formValues);
|
|
848
1164
|
}
|
|
849
1165
|
var notNullOption = (opt) => opt.const !== null;
|
|
@@ -886,11 +1202,16 @@ function extractParametersFromNode(schemaNode) {
|
|
|
886
1202
|
}
|
|
887
1203
|
const presentation = pickXKey(schemaNode, "presentation") ?? {};
|
|
888
1204
|
const errorMessage = pickXKey(schemaNode, "errorMessage") ?? {};
|
|
1205
|
+
const requiredValidations = schemaNode["x-jsf-logic-validations"];
|
|
1206
|
+
const computedAttributes = schemaNode["x-jsf-logic-computedAttrs"];
|
|
1207
|
+
const decoratedComputedAttributes = getDecoratedComputedAttributes(computedAttributes);
|
|
889
1208
|
const node = omit(schemaNode, ["x-jsf-presentation", "presentation"]);
|
|
890
1209
|
const description = presentation?.description || node.description;
|
|
891
|
-
const statementDescription = presentation.statement?.description;
|
|
1210
|
+
const statementDescription = containsHTML(presentation.statement?.description) ? wrapWithSpan(presentation.statement.description, { class: "jsf-statement" }) : presentation.statement?.description;
|
|
892
1211
|
return omitBy(
|
|
893
1212
|
{
|
|
1213
|
+
const: node.const,
|
|
1214
|
+
...node.const && node.default ? { value: node.const } : {},
|
|
894
1215
|
label: node.title,
|
|
895
1216
|
readOnly: node.readOnly,
|
|
896
1217
|
...node.deprecated && {
|
|
@@ -925,8 +1246,12 @@ function extractParametersFromNode(schemaNode) {
|
|
|
925
1246
|
},
|
|
926
1247
|
// Handle [name].presentation
|
|
927
1248
|
...presentation,
|
|
928
|
-
|
|
929
|
-
|
|
1249
|
+
requiredValidations,
|
|
1250
|
+
computedAttributes: decoratedComputedAttributes,
|
|
1251
|
+
description: containsHTML(description) ? wrapWithSpan(description, {
|
|
1252
|
+
class: "jsf-description"
|
|
1253
|
+
}) : description,
|
|
1254
|
+
extra: containsHTML(presentation.extra) ? wrapWithSpan(presentation.extra, { class: "jsf-extra" }) : presentation.extra,
|
|
930
1255
|
statement: presentation.statement && {
|
|
931
1256
|
...presentation.statement,
|
|
932
1257
|
description: statementDescription
|
|
@@ -959,8 +1284,8 @@ function yupToFormErrors(yupError) {
|
|
|
959
1284
|
}
|
|
960
1285
|
return errors;
|
|
961
1286
|
}
|
|
962
|
-
var handleValuesChange = (fields, jsonSchema, config) => (values) => {
|
|
963
|
-
updateFieldsProperties(fields, values, jsonSchema);
|
|
1287
|
+
var handleValuesChange = (fields, jsonSchema, config, logic) => (values) => {
|
|
1288
|
+
updateFieldsProperties(fields, values, jsonSchema, logic);
|
|
964
1289
|
const lazySchema = lazy(() => buildCompleteYupSchema(fields, config));
|
|
965
1290
|
let errors;
|
|
966
1291
|
try {
|
|
@@ -979,6 +1304,12 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
|
|
|
979
1304
|
formErrors: yupToFormErrors(errors)
|
|
980
1305
|
};
|
|
981
1306
|
};
|
|
1307
|
+
function getDecoratedComputedAttributes(computedAttributes) {
|
|
1308
|
+
return {
|
|
1309
|
+
...computedAttributes ?? {},
|
|
1310
|
+
...computedAttributes?.const && computedAttributes?.default ? { value: computedAttributes.const } : {}
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
982
1313
|
|
|
983
1314
|
// src/calculateConditionalProperties.js
|
|
984
1315
|
function isFieldRequired(node, field) {
|
|
@@ -1180,6 +1511,9 @@ function applyFieldsDependencies(fieldsParameters, node) {
|
|
|
1180
1511
|
applyFieldsDependencies(fieldsParameters, condition);
|
|
1181
1512
|
});
|
|
1182
1513
|
}
|
|
1514
|
+
if (node?.["x-jsf-logic"]) {
|
|
1515
|
+
applyFieldsDependencies(fieldsParameters, node["x-jsf-logic"]);
|
|
1516
|
+
}
|
|
1183
1517
|
}
|
|
1184
1518
|
function getCustomPropertiesForField(fieldParams, config) {
|
|
1185
1519
|
return config?.customProperties?.[fieldParams.name];
|
|
@@ -1191,15 +1525,16 @@ function getComposeFunctionForField(fieldParams, hasCustomizations) {
|
|
|
1191
1525
|
}
|
|
1192
1526
|
return composeFn;
|
|
1193
1527
|
}
|
|
1194
|
-
function buildField(fieldParams, config, scopedJsonSchema) {
|
|
1528
|
+
function buildField(fieldParams, config, scopedJsonSchema, logic) {
|
|
1195
1529
|
const customProperties = getCustomPropertiesForField(fieldParams, config);
|
|
1196
1530
|
const composeFn = getComposeFunctionForField(fieldParams, !!customProperties);
|
|
1197
|
-
const yupSchema = buildYupSchema(fieldParams, config);
|
|
1531
|
+
const yupSchema = buildYupSchema(fieldParams, config, logic);
|
|
1198
1532
|
const calculateConditionalFieldsClosure = fieldParams.isDynamic && calculateConditionalProperties(fieldParams, customProperties);
|
|
1199
1533
|
const calculateCustomValidationPropertiesClosure = calculateCustomValidationProperties(
|
|
1200
1534
|
fieldParams,
|
|
1201
1535
|
customProperties
|
|
1202
1536
|
);
|
|
1537
|
+
const getComputedAttributes = Object.keys(fieldParams.computedAttributes).length > 0 && calculateComputedAttributes(fieldParams, config);
|
|
1203
1538
|
const hasCustomValidations = !!customProperties && size(pick2(customProperties, SUPPORTED_CUSTOM_VALIDATION_FIELD_PARAMS)) > 0;
|
|
1204
1539
|
const finalFieldParams = {
|
|
1205
1540
|
// invalid attribute cleanup
|
|
@@ -1212,6 +1547,7 @@ function buildField(fieldParams, config, scopedJsonSchema) {
|
|
|
1212
1547
|
...hasCustomValidations && {
|
|
1213
1548
|
calculateCustomValidationProperties: calculateCustomValidationPropertiesClosure
|
|
1214
1549
|
},
|
|
1550
|
+
...getComputedAttributes && { getComputedAttributes },
|
|
1215
1551
|
// field customization properties
|
|
1216
1552
|
...customProperties && { fieldCustomization: customProperties },
|
|
1217
1553
|
// base schema
|
|
@@ -1220,7 +1556,7 @@ function buildField(fieldParams, config, scopedJsonSchema) {
|
|
|
1220
1556
|
};
|
|
1221
1557
|
return composeFn(finalFieldParams);
|
|
1222
1558
|
}
|
|
1223
|
-
function getFieldsFromJSONSchema(scopedJsonSchema, config) {
|
|
1559
|
+
function getFieldsFromJSONSchema(scopedJsonSchema, config, logic) {
|
|
1224
1560
|
if (!scopedJsonSchema) {
|
|
1225
1561
|
return [];
|
|
1226
1562
|
}
|
|
@@ -1244,11 +1580,11 @@ function getFieldsFromJSONSchema(scopedJsonSchema, config) {
|
|
|
1244
1580
|
fields: () => groupArrayFields,
|
|
1245
1581
|
addFieldText: fieldParams.addFieldText
|
|
1246
1582
|
};
|
|
1247
|
-
buildField(fieldParams, config, scopedJsonSchema).forEach((groupField) => {
|
|
1583
|
+
buildField(fieldParams, config, scopedJsonSchema, logic).forEach((groupField) => {
|
|
1248
1584
|
fields.push(groupField);
|
|
1249
1585
|
});
|
|
1250
1586
|
} else {
|
|
1251
|
-
fields.push(buildField(fieldParams, config, scopedJsonSchema));
|
|
1587
|
+
fields.push(buildField(fieldParams, config, scopedJsonSchema, logic));
|
|
1252
1588
|
}
|
|
1253
1589
|
});
|
|
1254
1590
|
return fields;
|
|
@@ -1259,9 +1595,15 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
|
|
|
1259
1595
|
...customConfig
|
|
1260
1596
|
};
|
|
1261
1597
|
try {
|
|
1262
|
-
const
|
|
1263
|
-
const
|
|
1264
|
-
|
|
1598
|
+
const logic = createValidationChecker(jsonSchema);
|
|
1599
|
+
const fields = getFieldsFromJSONSchema(jsonSchema, config, logic);
|
|
1600
|
+
const handleValidation = handleValuesChange(fields, jsonSchema, config, logic);
|
|
1601
|
+
updateFieldsProperties(
|
|
1602
|
+
fields,
|
|
1603
|
+
getPrefillValues(fields, config.initialValues),
|
|
1604
|
+
jsonSchema,
|
|
1605
|
+
logic
|
|
1606
|
+
);
|
|
1265
1607
|
return {
|
|
1266
1608
|
fields,
|
|
1267
1609
|
handleValidation,
|