@remoteoss/json-schema-form 0.4.3-beta.0 → 0.4.4-dev.20230829101351
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.cjs +500 -62
- package/dist/index.js +500 -62
- package/dist/standalone.js +878 -62
- package/package.json +2 -1
- package/src/tests/jsonLogic.test.js +814 -0
- package/src/tests/jsonLogicFixtures.js +1489 -0
package/dist/standalone.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.4.
|
|
5
|
-
Generated:
|
|
4
|
+
NPM Package: @remoteoss/json-schema-form@0.4.4-dev.20230829101351
|
|
5
|
+
Generated: Tue, 29 Aug 2023 10:14:13 GMT
|
|
6
6
|
|
|
7
7
|
MIT License
|
|
8
8
|
|
|
@@ -5648,6 +5648,384 @@ var require_isEmpty = __commonJS({
|
|
|
5648
5648
|
}
|
|
5649
5649
|
});
|
|
5650
5650
|
|
|
5651
|
+
// node_modules/json-logic-js/logic.js
|
|
5652
|
+
var require_logic = __commonJS({
|
|
5653
|
+
"node_modules/json-logic-js/logic.js"(exports2, module2) {
|
|
5654
|
+
(function(root2, factory) {
|
|
5655
|
+
if (typeof define === "function" && define.amd) {
|
|
5656
|
+
define(factory);
|
|
5657
|
+
} else if (typeof exports2 === "object") {
|
|
5658
|
+
module2.exports = factory();
|
|
5659
|
+
} else {
|
|
5660
|
+
root2.jsonLogic = factory();
|
|
5661
|
+
}
|
|
5662
|
+
})(exports2, function() {
|
|
5663
|
+
"use strict";
|
|
5664
|
+
if (!Array.isArray) {
|
|
5665
|
+
Array.isArray = function(arg) {
|
|
5666
|
+
return Object.prototype.toString.call(arg) === "[object Array]";
|
|
5667
|
+
};
|
|
5668
|
+
}
|
|
5669
|
+
function arrayUnique(array2) {
|
|
5670
|
+
var a = [];
|
|
5671
|
+
for (var i = 0, l = array2.length; i < l; i++) {
|
|
5672
|
+
if (a.indexOf(array2[i]) === -1) {
|
|
5673
|
+
a.push(array2[i]);
|
|
5674
|
+
}
|
|
5675
|
+
}
|
|
5676
|
+
return a;
|
|
5677
|
+
}
|
|
5678
|
+
var jsonLogic2 = {};
|
|
5679
|
+
var operations = {
|
|
5680
|
+
"==": function(a, b) {
|
|
5681
|
+
return a == b;
|
|
5682
|
+
},
|
|
5683
|
+
"===": function(a, b) {
|
|
5684
|
+
return a === b;
|
|
5685
|
+
},
|
|
5686
|
+
"!=": function(a, b) {
|
|
5687
|
+
return a != b;
|
|
5688
|
+
},
|
|
5689
|
+
"!==": function(a, b) {
|
|
5690
|
+
return a !== b;
|
|
5691
|
+
},
|
|
5692
|
+
">": function(a, b) {
|
|
5693
|
+
return a > b;
|
|
5694
|
+
},
|
|
5695
|
+
">=": function(a, b) {
|
|
5696
|
+
return a >= b;
|
|
5697
|
+
},
|
|
5698
|
+
"<": function(a, b, c) {
|
|
5699
|
+
return c === void 0 ? a < b : a < b && b < c;
|
|
5700
|
+
},
|
|
5701
|
+
"<=": function(a, b, c) {
|
|
5702
|
+
return c === void 0 ? a <= b : a <= b && b <= c;
|
|
5703
|
+
},
|
|
5704
|
+
"!!": function(a) {
|
|
5705
|
+
return jsonLogic2.truthy(a);
|
|
5706
|
+
},
|
|
5707
|
+
"!": function(a) {
|
|
5708
|
+
return !jsonLogic2.truthy(a);
|
|
5709
|
+
},
|
|
5710
|
+
"%": function(a, b) {
|
|
5711
|
+
return a % b;
|
|
5712
|
+
},
|
|
5713
|
+
"log": function(a) {
|
|
5714
|
+
console.log(a);
|
|
5715
|
+
return a;
|
|
5716
|
+
},
|
|
5717
|
+
"in": function(a, b) {
|
|
5718
|
+
if (!b || typeof b.indexOf === "undefined")
|
|
5719
|
+
return false;
|
|
5720
|
+
return b.indexOf(a) !== -1;
|
|
5721
|
+
},
|
|
5722
|
+
"cat": function() {
|
|
5723
|
+
return Array.prototype.join.call(arguments, "");
|
|
5724
|
+
},
|
|
5725
|
+
"substr": function(source, start, end) {
|
|
5726
|
+
if (end < 0) {
|
|
5727
|
+
var temp = String(source).substr(start);
|
|
5728
|
+
return temp.substr(0, temp.length + end);
|
|
5729
|
+
}
|
|
5730
|
+
return String(source).substr(start, end);
|
|
5731
|
+
},
|
|
5732
|
+
"+": function() {
|
|
5733
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
5734
|
+
return parseFloat(a, 10) + parseFloat(b, 10);
|
|
5735
|
+
}, 0);
|
|
5736
|
+
},
|
|
5737
|
+
"*": function() {
|
|
5738
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
5739
|
+
return parseFloat(a, 10) * parseFloat(b, 10);
|
|
5740
|
+
});
|
|
5741
|
+
},
|
|
5742
|
+
"-": function(a, b) {
|
|
5743
|
+
if (b === void 0) {
|
|
5744
|
+
return -a;
|
|
5745
|
+
} else {
|
|
5746
|
+
return a - b;
|
|
5747
|
+
}
|
|
5748
|
+
},
|
|
5749
|
+
"/": function(a, b) {
|
|
5750
|
+
return a / b;
|
|
5751
|
+
},
|
|
5752
|
+
"min": function() {
|
|
5753
|
+
return Math.min.apply(this, arguments);
|
|
5754
|
+
},
|
|
5755
|
+
"max": function() {
|
|
5756
|
+
return Math.max.apply(this, arguments);
|
|
5757
|
+
},
|
|
5758
|
+
"merge": function() {
|
|
5759
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
5760
|
+
return a.concat(b);
|
|
5761
|
+
}, []);
|
|
5762
|
+
},
|
|
5763
|
+
"var": function(a, b) {
|
|
5764
|
+
var not_found = b === void 0 ? null : b;
|
|
5765
|
+
var data = this;
|
|
5766
|
+
if (typeof a === "undefined" || a === "" || a === null) {
|
|
5767
|
+
return data;
|
|
5768
|
+
}
|
|
5769
|
+
var sub_props = String(a).split(".");
|
|
5770
|
+
for (var i = 0; i < sub_props.length; i++) {
|
|
5771
|
+
if (data === null || data === void 0) {
|
|
5772
|
+
return not_found;
|
|
5773
|
+
}
|
|
5774
|
+
data = data[sub_props[i]];
|
|
5775
|
+
if (data === void 0) {
|
|
5776
|
+
return not_found;
|
|
5777
|
+
}
|
|
5778
|
+
}
|
|
5779
|
+
return data;
|
|
5780
|
+
},
|
|
5781
|
+
"missing": function() {
|
|
5782
|
+
var missing = [];
|
|
5783
|
+
var keys2 = Array.isArray(arguments[0]) ? arguments[0] : arguments;
|
|
5784
|
+
for (var i = 0; i < keys2.length; i++) {
|
|
5785
|
+
var key = keys2[i];
|
|
5786
|
+
var value = jsonLogic2.apply({ "var": key }, this);
|
|
5787
|
+
if (value === null || value === "") {
|
|
5788
|
+
missing.push(key);
|
|
5789
|
+
}
|
|
5790
|
+
}
|
|
5791
|
+
return missing;
|
|
5792
|
+
},
|
|
5793
|
+
"missing_some": function(need_count, options) {
|
|
5794
|
+
var are_missing = jsonLogic2.apply({ "missing": options }, this);
|
|
5795
|
+
if (options.length - are_missing.length >= need_count) {
|
|
5796
|
+
return [];
|
|
5797
|
+
} else {
|
|
5798
|
+
return are_missing;
|
|
5799
|
+
}
|
|
5800
|
+
}
|
|
5801
|
+
};
|
|
5802
|
+
jsonLogic2.is_logic = function(logic) {
|
|
5803
|
+
return typeof logic === "object" && // An object
|
|
5804
|
+
logic !== null && // but not null
|
|
5805
|
+
!Array.isArray(logic) && // and not an array
|
|
5806
|
+
Object.keys(logic).length === 1;
|
|
5807
|
+
};
|
|
5808
|
+
jsonLogic2.truthy = function(value) {
|
|
5809
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
5810
|
+
return false;
|
|
5811
|
+
}
|
|
5812
|
+
return !!value;
|
|
5813
|
+
};
|
|
5814
|
+
jsonLogic2.get_operator = function(logic) {
|
|
5815
|
+
return Object.keys(logic)[0];
|
|
5816
|
+
};
|
|
5817
|
+
jsonLogic2.get_values = function(logic) {
|
|
5818
|
+
return logic[jsonLogic2.get_operator(logic)];
|
|
5819
|
+
};
|
|
5820
|
+
jsonLogic2.apply = function(logic, data) {
|
|
5821
|
+
if (Array.isArray(logic)) {
|
|
5822
|
+
return logic.map(function(l) {
|
|
5823
|
+
return jsonLogic2.apply(l, data);
|
|
5824
|
+
});
|
|
5825
|
+
}
|
|
5826
|
+
if (!jsonLogic2.is_logic(logic)) {
|
|
5827
|
+
return logic;
|
|
5828
|
+
}
|
|
5829
|
+
var op = jsonLogic2.get_operator(logic);
|
|
5830
|
+
var values2 = logic[op];
|
|
5831
|
+
var i;
|
|
5832
|
+
var current;
|
|
5833
|
+
var scopedLogic;
|
|
5834
|
+
var scopedData;
|
|
5835
|
+
var initial;
|
|
5836
|
+
if (!Array.isArray(values2)) {
|
|
5837
|
+
values2 = [values2];
|
|
5838
|
+
}
|
|
5839
|
+
if (op === "if" || op == "?:") {
|
|
5840
|
+
for (i = 0; i < values2.length - 1; i += 2) {
|
|
5841
|
+
if (jsonLogic2.truthy(jsonLogic2.apply(values2[i], data))) {
|
|
5842
|
+
return jsonLogic2.apply(values2[i + 1], data);
|
|
5843
|
+
}
|
|
5844
|
+
}
|
|
5845
|
+
if (values2.length === i + 1) {
|
|
5846
|
+
return jsonLogic2.apply(values2[i], data);
|
|
5847
|
+
}
|
|
5848
|
+
return null;
|
|
5849
|
+
} else if (op === "and") {
|
|
5850
|
+
for (i = 0; i < values2.length; i += 1) {
|
|
5851
|
+
current = jsonLogic2.apply(values2[i], data);
|
|
5852
|
+
if (!jsonLogic2.truthy(current)) {
|
|
5853
|
+
return current;
|
|
5854
|
+
}
|
|
5855
|
+
}
|
|
5856
|
+
return current;
|
|
5857
|
+
} else if (op === "or") {
|
|
5858
|
+
for (i = 0; i < values2.length; i += 1) {
|
|
5859
|
+
current = jsonLogic2.apply(values2[i], data);
|
|
5860
|
+
if (jsonLogic2.truthy(current)) {
|
|
5861
|
+
return current;
|
|
5862
|
+
}
|
|
5863
|
+
}
|
|
5864
|
+
return current;
|
|
5865
|
+
} else if (op === "filter") {
|
|
5866
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5867
|
+
scopedLogic = values2[1];
|
|
5868
|
+
if (!Array.isArray(scopedData)) {
|
|
5869
|
+
return [];
|
|
5870
|
+
}
|
|
5871
|
+
return scopedData.filter(function(datum) {
|
|
5872
|
+
return jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, datum));
|
|
5873
|
+
});
|
|
5874
|
+
} else if (op === "map") {
|
|
5875
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5876
|
+
scopedLogic = values2[1];
|
|
5877
|
+
if (!Array.isArray(scopedData)) {
|
|
5878
|
+
return [];
|
|
5879
|
+
}
|
|
5880
|
+
return scopedData.map(function(datum) {
|
|
5881
|
+
return jsonLogic2.apply(scopedLogic, datum);
|
|
5882
|
+
});
|
|
5883
|
+
} else if (op === "reduce") {
|
|
5884
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5885
|
+
scopedLogic = values2[1];
|
|
5886
|
+
initial = typeof values2[2] !== "undefined" ? values2[2] : null;
|
|
5887
|
+
if (!Array.isArray(scopedData)) {
|
|
5888
|
+
return initial;
|
|
5889
|
+
}
|
|
5890
|
+
return scopedData.reduce(
|
|
5891
|
+
function(accumulator, current2) {
|
|
5892
|
+
return jsonLogic2.apply(
|
|
5893
|
+
scopedLogic,
|
|
5894
|
+
{ current: current2, accumulator }
|
|
5895
|
+
);
|
|
5896
|
+
},
|
|
5897
|
+
initial
|
|
5898
|
+
);
|
|
5899
|
+
} else if (op === "all") {
|
|
5900
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5901
|
+
scopedLogic = values2[1];
|
|
5902
|
+
if (!Array.isArray(scopedData) || !scopedData.length) {
|
|
5903
|
+
return false;
|
|
5904
|
+
}
|
|
5905
|
+
for (i = 0; i < scopedData.length; i += 1) {
|
|
5906
|
+
if (!jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
|
|
5907
|
+
return false;
|
|
5908
|
+
}
|
|
5909
|
+
}
|
|
5910
|
+
return true;
|
|
5911
|
+
} else if (op === "none") {
|
|
5912
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5913
|
+
scopedLogic = values2[1];
|
|
5914
|
+
if (!Array.isArray(scopedData) || !scopedData.length) {
|
|
5915
|
+
return true;
|
|
5916
|
+
}
|
|
5917
|
+
for (i = 0; i < scopedData.length; i += 1) {
|
|
5918
|
+
if (jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
|
|
5919
|
+
return false;
|
|
5920
|
+
}
|
|
5921
|
+
}
|
|
5922
|
+
return true;
|
|
5923
|
+
} else if (op === "some") {
|
|
5924
|
+
scopedData = jsonLogic2.apply(values2[0], data);
|
|
5925
|
+
scopedLogic = values2[1];
|
|
5926
|
+
if (!Array.isArray(scopedData) || !scopedData.length) {
|
|
5927
|
+
return false;
|
|
5928
|
+
}
|
|
5929
|
+
for (i = 0; i < scopedData.length; i += 1) {
|
|
5930
|
+
if (jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
|
|
5931
|
+
return true;
|
|
5932
|
+
}
|
|
5933
|
+
}
|
|
5934
|
+
return false;
|
|
5935
|
+
}
|
|
5936
|
+
values2 = values2.map(function(val) {
|
|
5937
|
+
return jsonLogic2.apply(val, data);
|
|
5938
|
+
});
|
|
5939
|
+
if (operations.hasOwnProperty(op) && typeof operations[op] === "function") {
|
|
5940
|
+
return operations[op].apply(data, values2);
|
|
5941
|
+
} else if (op.indexOf(".") > 0) {
|
|
5942
|
+
var sub_ops = String(op).split(".");
|
|
5943
|
+
var operation = operations;
|
|
5944
|
+
for (i = 0; i < sub_ops.length; i++) {
|
|
5945
|
+
if (!operation.hasOwnProperty(sub_ops[i])) {
|
|
5946
|
+
throw new Error("Unrecognized operation " + op + " (failed at " + sub_ops.slice(0, i + 1).join(".") + ")");
|
|
5947
|
+
}
|
|
5948
|
+
operation = operation[sub_ops[i]];
|
|
5949
|
+
}
|
|
5950
|
+
return operation.apply(data, values2);
|
|
5951
|
+
}
|
|
5952
|
+
throw new Error("Unrecognized operation " + op);
|
|
5953
|
+
};
|
|
5954
|
+
jsonLogic2.uses_data = function(logic) {
|
|
5955
|
+
var collection = [];
|
|
5956
|
+
if (jsonLogic2.is_logic(logic)) {
|
|
5957
|
+
var op = jsonLogic2.get_operator(logic);
|
|
5958
|
+
var values2 = logic[op];
|
|
5959
|
+
if (!Array.isArray(values2)) {
|
|
5960
|
+
values2 = [values2];
|
|
5961
|
+
}
|
|
5962
|
+
if (op === "var") {
|
|
5963
|
+
collection.push(values2[0]);
|
|
5964
|
+
} else {
|
|
5965
|
+
values2.forEach(function(val) {
|
|
5966
|
+
collection.push.apply(collection, jsonLogic2.uses_data(val));
|
|
5967
|
+
});
|
|
5968
|
+
}
|
|
5969
|
+
}
|
|
5970
|
+
return arrayUnique(collection);
|
|
5971
|
+
};
|
|
5972
|
+
jsonLogic2.add_operation = function(name, code) {
|
|
5973
|
+
operations[name] = code;
|
|
5974
|
+
};
|
|
5975
|
+
jsonLogic2.rm_operation = function(name) {
|
|
5976
|
+
delete operations[name];
|
|
5977
|
+
};
|
|
5978
|
+
jsonLogic2.rule_like = function(rule, pattern) {
|
|
5979
|
+
if (pattern === rule) {
|
|
5980
|
+
return true;
|
|
5981
|
+
}
|
|
5982
|
+
if (pattern === "@") {
|
|
5983
|
+
return true;
|
|
5984
|
+
}
|
|
5985
|
+
if (pattern === "number") {
|
|
5986
|
+
return typeof rule === "number";
|
|
5987
|
+
}
|
|
5988
|
+
if (pattern === "string") {
|
|
5989
|
+
return typeof rule === "string";
|
|
5990
|
+
}
|
|
5991
|
+
if (pattern === "array") {
|
|
5992
|
+
return Array.isArray(rule) && !jsonLogic2.is_logic(rule);
|
|
5993
|
+
}
|
|
5994
|
+
if (jsonLogic2.is_logic(pattern)) {
|
|
5995
|
+
if (jsonLogic2.is_logic(rule)) {
|
|
5996
|
+
var pattern_op = jsonLogic2.get_operator(pattern);
|
|
5997
|
+
var rule_op = jsonLogic2.get_operator(rule);
|
|
5998
|
+
if (pattern_op === "@" || pattern_op === rule_op) {
|
|
5999
|
+
return jsonLogic2.rule_like(
|
|
6000
|
+
jsonLogic2.get_values(rule, false),
|
|
6001
|
+
jsonLogic2.get_values(pattern, false)
|
|
6002
|
+
);
|
|
6003
|
+
}
|
|
6004
|
+
}
|
|
6005
|
+
return false;
|
|
6006
|
+
}
|
|
6007
|
+
if (Array.isArray(pattern)) {
|
|
6008
|
+
if (Array.isArray(rule)) {
|
|
6009
|
+
if (pattern.length !== rule.length) {
|
|
6010
|
+
return false;
|
|
6011
|
+
}
|
|
6012
|
+
for (var i = 0; i < pattern.length; i += 1) {
|
|
6013
|
+
if (!jsonLogic2.rule_like(rule[i], pattern[i])) {
|
|
6014
|
+
return false;
|
|
6015
|
+
}
|
|
6016
|
+
}
|
|
6017
|
+
return true;
|
|
6018
|
+
} else {
|
|
6019
|
+
return false;
|
|
6020
|
+
}
|
|
6021
|
+
}
|
|
6022
|
+
return false;
|
|
6023
|
+
};
|
|
6024
|
+
return jsonLogic2;
|
|
6025
|
+
});
|
|
6026
|
+
}
|
|
6027
|
+
});
|
|
6028
|
+
|
|
5651
6029
|
// node_modules/lodash/_createFlow.js
|
|
5652
6030
|
var require_createFlow = __commonJS({
|
|
5653
6031
|
"node_modules/lodash/_createFlow.js"(exports2, module2) {
|
|
@@ -11044,8 +11422,8 @@ function hasProperty(object2, propertyName) {
|
|
|
11044
11422
|
}
|
|
11045
11423
|
|
|
11046
11424
|
// src/checkIfConditionMatches.js
|
|
11047
|
-
function checkIfConditionMatches(node, formValues, formFields) {
|
|
11048
|
-
return Object.keys(node.if.properties).every((name) => {
|
|
11425
|
+
function checkIfConditionMatches(node, formValues, formFields, validations) {
|
|
11426
|
+
return Object.keys(node.if.properties ?? {}).every((name) => {
|
|
11049
11427
|
const currentProperty = node.if.properties[name];
|
|
11050
11428
|
const value = formValues[name];
|
|
11051
11429
|
const hasEmptyValue = typeof value === "undefined" || // NOTE: This is a "Remote API" dependency, as empty fields are sent as "null".
|
|
@@ -11071,7 +11449,8 @@ function checkIfConditionMatches(node, formValues, formFields) {
|
|
|
11071
11449
|
return checkIfConditionMatches(
|
|
11072
11450
|
{ if: currentProperty },
|
|
11073
11451
|
formValues[name],
|
|
11074
|
-
getField(name, formFields).fields
|
|
11452
|
+
getField(name, formFields).fields,
|
|
11453
|
+
validations
|
|
11075
11454
|
);
|
|
11076
11455
|
}
|
|
11077
11456
|
const field = getField(name, formFields);
|
|
@@ -11087,6 +11466,23 @@ function checkIfConditionMatches(node, formValues, formFields) {
|
|
|
11087
11466
|
);
|
|
11088
11467
|
});
|
|
11089
11468
|
}
|
|
11469
|
+
function checkIfMatchesValidationsAndComputedValues(node, formValues, validations, parentID) {
|
|
11470
|
+
const validationsMatch = Object.entries(node.if.validations ?? {}).every(([name, property2]) => {
|
|
11471
|
+
const currentValue = validations.getScope(parentID).evaluateValidationRuleInCondition(name, formValues);
|
|
11472
|
+
if (Object.hasOwn(property2, "const") && currentValue === property2.const)
|
|
11473
|
+
return true;
|
|
11474
|
+
return false;
|
|
11475
|
+
});
|
|
11476
|
+
const computedValuesMatch = Object.entries(node.if.computedValues ?? {}).every(
|
|
11477
|
+
([name, property2]) => {
|
|
11478
|
+
const currentValue = validations.getScope(parentID).evaluateComputedValueRuleInCondition(name, formValues);
|
|
11479
|
+
if (Object.hasOwn(property2, "const") && currentValue === property2.const)
|
|
11480
|
+
return true;
|
|
11481
|
+
return false;
|
|
11482
|
+
}
|
|
11483
|
+
);
|
|
11484
|
+
return computedValuesMatch && validationsMatch;
|
|
11485
|
+
}
|
|
11090
11486
|
|
|
11091
11487
|
// src/internals/helpers.js
|
|
11092
11488
|
var import_merge = __toESM(require_merge2());
|
|
@@ -11347,6 +11743,9 @@ function _composeFieldCustomClosure(defaultComposeFn) {
|
|
|
11347
11743
|
};
|
|
11348
11744
|
}
|
|
11349
11745
|
|
|
11746
|
+
// src/jsonLogic.js
|
|
11747
|
+
var import_json_logic_js = __toESM(require_logic());
|
|
11748
|
+
|
|
11350
11749
|
// src/yupSchema.js
|
|
11351
11750
|
var import_flow = __toESM(require_flow());
|
|
11352
11751
|
var import_noop = __toESM(require_noop());
|
|
@@ -11473,7 +11872,7 @@ var getYupSchema = ({ inputType, ...field }) => {
|
|
|
11473
11872
|
}
|
|
11474
11873
|
return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
|
|
11475
11874
|
};
|
|
11476
|
-
function buildYupSchema(field, config) {
|
|
11875
|
+
function buildYupSchema(field, config, validations) {
|
|
11477
11876
|
const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
|
|
11478
11877
|
const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
|
|
11479
11878
|
let baseSchema;
|
|
@@ -11546,6 +11945,13 @@ function buildYupSchema(field, config) {
|
|
|
11546
11945
|
}) : true
|
|
11547
11946
|
);
|
|
11548
11947
|
}
|
|
11948
|
+
function withConst(yupSchema) {
|
|
11949
|
+
return yupSchema.test(
|
|
11950
|
+
"isConst",
|
|
11951
|
+
errorMessage.const ?? errorMessageFromConfig.const ?? `The only accepted value is ${propertyFields.const}.`,
|
|
11952
|
+
(value) => propertyFields.required === false && value === void 0 || value === null || value === propertyFields.const
|
|
11953
|
+
);
|
|
11954
|
+
}
|
|
11549
11955
|
function withBaseSchema() {
|
|
11550
11956
|
const customErrorMsg = errorMessage.type || errorMessageFromConfig.type;
|
|
11551
11957
|
if (customErrorMsg) {
|
|
@@ -11566,7 +11972,8 @@ function buildYupSchema(field, config) {
|
|
|
11566
11972
|
...fieldSetfield,
|
|
11567
11973
|
inputType: fieldSetfield.type
|
|
11568
11974
|
},
|
|
11569
|
-
config
|
|
11975
|
+
{ ...config, parentID: field.name },
|
|
11976
|
+
validations
|
|
11570
11977
|
)();
|
|
11571
11978
|
}
|
|
11572
11979
|
});
|
|
@@ -11577,7 +11984,11 @@ function buildYupSchema(field, config) {
|
|
|
11577
11984
|
propertyFields.nthFieldGroup.fields().reduce(
|
|
11578
11985
|
(schema, groupArrayField) => ({
|
|
11579
11986
|
...schema,
|
|
11580
|
-
[groupArrayField.name]: buildYupSchema(
|
|
11987
|
+
[groupArrayField.name]: buildYupSchema(
|
|
11988
|
+
groupArrayField,
|
|
11989
|
+
{ ...config, parentID: `${propertyFields.nthFieldGroup.name}[]` },
|
|
11990
|
+
validations
|
|
11991
|
+
)()
|
|
11581
11992
|
}),
|
|
11582
11993
|
{}
|
|
11583
11994
|
)
|
|
@@ -11613,6 +12024,14 @@ function buildYupSchema(field, config) {
|
|
|
11613
12024
|
if (propertyFields.accept) {
|
|
11614
12025
|
validators.push(withFileFormat);
|
|
11615
12026
|
}
|
|
12027
|
+
if (propertyFields.const) {
|
|
12028
|
+
validators.push(withConst);
|
|
12029
|
+
}
|
|
12030
|
+
if (propertyFields.requiredValidations) {
|
|
12031
|
+
propertyFields.requiredValidations.forEach(
|
|
12032
|
+
(id) => validators.push(yupSchemaWithCustomJSONLogic({ field, id, validations, config }))
|
|
12033
|
+
);
|
|
12034
|
+
}
|
|
11616
12035
|
return (0, import_flow.default)(validators);
|
|
11617
12036
|
}
|
|
11618
12037
|
function getNoSortEdges(fields = []) {
|
|
@@ -11623,26 +12042,322 @@ function getNoSortEdges(fields = []) {
|
|
|
11623
12042
|
return list;
|
|
11624
12043
|
}, []);
|
|
11625
12044
|
}
|
|
11626
|
-
function getSchema(fields = [], config) {
|
|
12045
|
+
function getSchema(fields = [], config, validations) {
|
|
11627
12046
|
const newSchema = {};
|
|
11628
12047
|
fields.forEach((field) => {
|
|
11629
12048
|
if (field.schema) {
|
|
11630
12049
|
if (field.name) {
|
|
11631
12050
|
if (field.inputType === supportedTypes.FIELDSET) {
|
|
11632
|
-
const fieldsetSchema = buildYupSchema(
|
|
12051
|
+
const fieldsetSchema = buildYupSchema(
|
|
12052
|
+
field,
|
|
12053
|
+
{ ...config, parentID: field.name },
|
|
12054
|
+
validations
|
|
12055
|
+
)();
|
|
11633
12056
|
newSchema[field.name] = fieldsetSchema;
|
|
11634
12057
|
} else {
|
|
11635
12058
|
newSchema[field.name] = field.schema;
|
|
11636
12059
|
}
|
|
11637
12060
|
} else {
|
|
11638
|
-
Object.assign(newSchema, getSchema(field.fields, config));
|
|
12061
|
+
Object.assign(newSchema, getSchema(field.fields, config, validations));
|
|
11639
12062
|
}
|
|
11640
12063
|
}
|
|
11641
12064
|
});
|
|
11642
12065
|
return newSchema;
|
|
11643
12066
|
}
|
|
11644
|
-
function buildCompleteYupSchema(fields, config) {
|
|
11645
|
-
return ObjectSchema().shape(getSchema(fields, config), getNoSortEdges(fields));
|
|
12067
|
+
function buildCompleteYupSchema(fields, config, validations) {
|
|
12068
|
+
return ObjectSchema().shape(getSchema(fields, config, validations), getNoSortEdges(fields));
|
|
12069
|
+
}
|
|
12070
|
+
|
|
12071
|
+
// src/jsonLogic.js
|
|
12072
|
+
function createValidationChecker(schema) {
|
|
12073
|
+
const scopes = /* @__PURE__ */ new Map();
|
|
12074
|
+
function createScopes(jsonSchema, key = "root") {
|
|
12075
|
+
const sampleEmptyObject = buildSampleEmptyObject(schema);
|
|
12076
|
+
scopes.set(key, createValidationsScope(jsonSchema));
|
|
12077
|
+
Object.entries(jsonSchema?.properties ?? {}).filter(([, property2]) => property2.type === "object" || property2.type === "array").forEach(([key2, property2]) => {
|
|
12078
|
+
if (property2.type === "array") {
|
|
12079
|
+
createScopes(property2.items, `${key2}[]`);
|
|
12080
|
+
}
|
|
12081
|
+
createScopes(property2, key2);
|
|
12082
|
+
});
|
|
12083
|
+
validateInlineRules(jsonSchema, sampleEmptyObject);
|
|
12084
|
+
}
|
|
12085
|
+
createScopes(schema);
|
|
12086
|
+
return {
|
|
12087
|
+
scopes,
|
|
12088
|
+
getScope(name = "root") {
|
|
12089
|
+
return scopes.get(name);
|
|
12090
|
+
}
|
|
12091
|
+
};
|
|
12092
|
+
}
|
|
12093
|
+
function createValidationsScope(schema) {
|
|
12094
|
+
const validationMap = /* @__PURE__ */ new Map();
|
|
12095
|
+
const computedValuesMap = /* @__PURE__ */ new Map();
|
|
12096
|
+
const logic = schema?.["x-jsf-logic"] ?? {
|
|
12097
|
+
validations: {},
|
|
12098
|
+
computedValues: {}
|
|
12099
|
+
};
|
|
12100
|
+
const validations = Object.entries(logic.validations ?? {});
|
|
12101
|
+
const computedValues = Object.entries(logic.computedValues ?? {});
|
|
12102
|
+
const sampleEmptyObject = buildSampleEmptyObject(schema);
|
|
12103
|
+
validations.forEach(([id, validation]) => {
|
|
12104
|
+
if (!validation.rule) {
|
|
12105
|
+
throw Error(`Missing rule for validation with id of: "${id}".`);
|
|
12106
|
+
}
|
|
12107
|
+
checkRuleIntegrity(validation.rule, id, sampleEmptyObject);
|
|
12108
|
+
validationMap.set(id, validation);
|
|
12109
|
+
});
|
|
12110
|
+
computedValues.forEach(([id, computedValue]) => {
|
|
12111
|
+
if (!computedValue.rule) {
|
|
12112
|
+
throw Error(`Missing rule for computedValue with id of: "${id}".`);
|
|
12113
|
+
}
|
|
12114
|
+
checkRuleIntegrity(computedValue.rule, id, sampleEmptyObject);
|
|
12115
|
+
computedValuesMap.set(id, computedValue);
|
|
12116
|
+
});
|
|
12117
|
+
function evaluateValidation(rule, values2) {
|
|
12118
|
+
return import_json_logic_js.default.apply(rule, clean(values2));
|
|
12119
|
+
}
|
|
12120
|
+
return {
|
|
12121
|
+
validationMap,
|
|
12122
|
+
computedValuesMap,
|
|
12123
|
+
evaluateValidation,
|
|
12124
|
+
evaluateValidationRuleInCondition(id, values2) {
|
|
12125
|
+
const validation = validationMap.get(id);
|
|
12126
|
+
if (validation === void 0)
|
|
12127
|
+
throw Error(`"${id}" validation in if condition doesn't exist.`);
|
|
12128
|
+
return evaluateValidation(validation.rule, values2);
|
|
12129
|
+
},
|
|
12130
|
+
evaluateComputedValueRuleForField(id, values2, fieldName) {
|
|
12131
|
+
const validation = computedValuesMap.get(id);
|
|
12132
|
+
if (validation === void 0)
|
|
12133
|
+
throw Error(`"${id}" computedValue in field "${fieldName}" doesn't exist.`);
|
|
12134
|
+
return evaluateValidation(validation.rule, values2);
|
|
12135
|
+
},
|
|
12136
|
+
evaluateComputedValueRuleInCondition(id, values2) {
|
|
12137
|
+
const validation = computedValuesMap.get(id);
|
|
12138
|
+
if (validation === void 0)
|
|
12139
|
+
throw Error(`"${id}" computedValue in if condition doesn't exist.`);
|
|
12140
|
+
return evaluateValidation(validation.rule, values2);
|
|
12141
|
+
}
|
|
12142
|
+
};
|
|
12143
|
+
}
|
|
12144
|
+
function clean(values2 = {}) {
|
|
12145
|
+
return Object.entries(values2).reduce((prev, [key, value]) => {
|
|
12146
|
+
return { ...prev, [key]: value === void 0 ? null : value };
|
|
12147
|
+
}, {});
|
|
12148
|
+
}
|
|
12149
|
+
function yupSchemaWithCustomJSONLogic({ field, validations, config, id }) {
|
|
12150
|
+
const { parentID = "root" } = config;
|
|
12151
|
+
const validation = validations.getScope(parentID).validationMap.get(id);
|
|
12152
|
+
if (validation === void 0) {
|
|
12153
|
+
throw Error(`Validation "${id}" required for "${field.name}" doesn't exist.`);
|
|
12154
|
+
}
|
|
12155
|
+
return (yupSchema) => yupSchema.test(
|
|
12156
|
+
`${field.name}-validation-${id}`,
|
|
12157
|
+
validation?.errorMessage ?? "This field is invalid.",
|
|
12158
|
+
(value, { parent }) => {
|
|
12159
|
+
if (value === void 0 && !field.required)
|
|
12160
|
+
return true;
|
|
12161
|
+
return import_json_logic_js.default.apply(validation.rule, parent);
|
|
12162
|
+
}
|
|
12163
|
+
);
|
|
12164
|
+
}
|
|
12165
|
+
function replaceHandlebarsTemplates({
|
|
12166
|
+
value: toReplace,
|
|
12167
|
+
validations,
|
|
12168
|
+
formValues,
|
|
12169
|
+
parentID,
|
|
12170
|
+
name: fieldName
|
|
12171
|
+
}) {
|
|
12172
|
+
if (typeof toReplace === "string") {
|
|
12173
|
+
return toReplace.replace(/\{\{([^{}]+)\}\}/g, (match, key) => {
|
|
12174
|
+
return validations.getScope(parentID).evaluateComputedValueRuleForField(key.trim(), formValues, fieldName);
|
|
12175
|
+
});
|
|
12176
|
+
} else if (typeof toReplace === "object") {
|
|
12177
|
+
const { value, ...rules } = toReplace;
|
|
12178
|
+
if (Object.keys(rules).length > 1 && !value)
|
|
12179
|
+
throw Error("Cannot define multiple rules without a template string with key `value`.");
|
|
12180
|
+
const computedTemplateValue = Object.entries(rules).reduce((prev, [key, rule]) => {
|
|
12181
|
+
const computedValue = validations.getScope(parentID).evaluateValidation(rule, formValues);
|
|
12182
|
+
return prev.replaceAll(`{{${key}}}`, computedValue);
|
|
12183
|
+
}, value);
|
|
12184
|
+
return computedTemplateValue.replace(/\{\{([^{}]+)\}\}/g, (match, key) => {
|
|
12185
|
+
return validations.getScope(parentID).evaluateComputedValueRuleForField(key.trim(), formValues, fieldName);
|
|
12186
|
+
});
|
|
12187
|
+
}
|
|
12188
|
+
}
|
|
12189
|
+
function calculateComputedAttributes(fieldParams, { parentID = "root" } = {}) {
|
|
12190
|
+
return ({ validations, isRequired, config, formValues }) => {
|
|
12191
|
+
const { name, computedAttributes } = fieldParams;
|
|
12192
|
+
const attributes = Object.fromEntries(
|
|
12193
|
+
Object.entries(computedAttributes).map(handleComputedAttribute(validations, formValues, parentID, name)).filter(([, value]) => value !== null)
|
|
12194
|
+
);
|
|
12195
|
+
return {
|
|
12196
|
+
...attributes,
|
|
12197
|
+
schema: buildYupSchema(
|
|
12198
|
+
{ ...fieldParams, ...attributes, required: isRequired },
|
|
12199
|
+
config,
|
|
12200
|
+
validations
|
|
12201
|
+
)
|
|
12202
|
+
};
|
|
12203
|
+
};
|
|
12204
|
+
}
|
|
12205
|
+
function handleComputedAttribute(validations, formValues, parentID, name) {
|
|
12206
|
+
return ([key, value]) => {
|
|
12207
|
+
if (key === "description")
|
|
12208
|
+
return [key, replaceHandlebarsTemplates({ value, validations, formValues, parentID, name })];
|
|
12209
|
+
if (key === "title") {
|
|
12210
|
+
return [
|
|
12211
|
+
"label",
|
|
12212
|
+
replaceHandlebarsTemplates({ value, validations, formValues, parentID, name })
|
|
12213
|
+
];
|
|
12214
|
+
}
|
|
12215
|
+
if (key === "const")
|
|
12216
|
+
return [
|
|
12217
|
+
key,
|
|
12218
|
+
validations.getScope(parentID).evaluateComputedValueRuleForField(value, formValues, name)
|
|
12219
|
+
];
|
|
12220
|
+
if (key === "x-jsf-errorMessage") {
|
|
12221
|
+
return [
|
|
12222
|
+
"errorMessage",
|
|
12223
|
+
handleNestedObjectForComputedValues(value, formValues, parentID, validations, name)
|
|
12224
|
+
];
|
|
12225
|
+
}
|
|
12226
|
+
if (typeof value === "string") {
|
|
12227
|
+
return [
|
|
12228
|
+
key,
|
|
12229
|
+
validations.getScope(parentID).evaluateComputedValueRuleForField(value, formValues, name)
|
|
12230
|
+
];
|
|
12231
|
+
}
|
|
12232
|
+
if (key === "x-jsf-presentation" && value.statement) {
|
|
12233
|
+
return [
|
|
12234
|
+
"statement",
|
|
12235
|
+
handleNestedObjectForComputedValues(
|
|
12236
|
+
value.statement,
|
|
12237
|
+
formValues,
|
|
12238
|
+
parentID,
|
|
12239
|
+
validations,
|
|
12240
|
+
name
|
|
12241
|
+
)
|
|
12242
|
+
];
|
|
12243
|
+
}
|
|
12244
|
+
if (typeof value === "object" && value.rule) {
|
|
12245
|
+
return [key, validations.getScope(parentID).evaluateValidation(value.rule, formValues)];
|
|
12246
|
+
}
|
|
12247
|
+
};
|
|
12248
|
+
}
|
|
12249
|
+
function handleNestedObjectForComputedValues(values2, formValues, parentID, validations, name) {
|
|
12250
|
+
return Object.fromEntries(
|
|
12251
|
+
Object.entries(values2).map(([key, value]) => {
|
|
12252
|
+
return [key, replaceHandlebarsTemplates({ value, validations, formValues, parentID, name })];
|
|
12253
|
+
})
|
|
12254
|
+
);
|
|
12255
|
+
}
|
|
12256
|
+
function processJSONLogicNode({
|
|
12257
|
+
node,
|
|
12258
|
+
formFields,
|
|
12259
|
+
formValues,
|
|
12260
|
+
accRequired,
|
|
12261
|
+
parentID,
|
|
12262
|
+
validations
|
|
12263
|
+
}) {
|
|
12264
|
+
const requiredFields = new Set(accRequired);
|
|
12265
|
+
if (node.allOf) {
|
|
12266
|
+
node.allOf.map(
|
|
12267
|
+
(allOfNode) => processJSONLogicNode({ node: allOfNode, formValues, formFields, validations, parentID })
|
|
12268
|
+
).forEach(({ required: allOfItemRequired }) => {
|
|
12269
|
+
allOfItemRequired.forEach(requiredFields.add, requiredFields);
|
|
12270
|
+
});
|
|
12271
|
+
}
|
|
12272
|
+
if (node.if) {
|
|
12273
|
+
const matchesPropertyCondition = checkIfConditionMatches(
|
|
12274
|
+
node,
|
|
12275
|
+
formValues,
|
|
12276
|
+
formFields,
|
|
12277
|
+
validations
|
|
12278
|
+
);
|
|
12279
|
+
const matchesValidationsAndComputedValues = checkIfMatchesValidationsAndComputedValues(
|
|
12280
|
+
node,
|
|
12281
|
+
formValues,
|
|
12282
|
+
validations,
|
|
12283
|
+
parentID
|
|
12284
|
+
);
|
|
12285
|
+
const isConditionMatch = matchesPropertyCondition && matchesValidationsAndComputedValues;
|
|
12286
|
+
if (isConditionMatch && node.then) {
|
|
12287
|
+
const { required: branchRequired } = processNode({
|
|
12288
|
+
node: node.then,
|
|
12289
|
+
formValues,
|
|
12290
|
+
formFields,
|
|
12291
|
+
accRequired,
|
|
12292
|
+
validations
|
|
12293
|
+
});
|
|
12294
|
+
branchRequired.forEach((field) => requiredFields.add(field));
|
|
12295
|
+
}
|
|
12296
|
+
if (!isConditionMatch && node.else) {
|
|
12297
|
+
const { required: branchRequired } = processNode({
|
|
12298
|
+
node: node.else,
|
|
12299
|
+
formValues,
|
|
12300
|
+
formFields,
|
|
12301
|
+
accRequired: requiredFields,
|
|
12302
|
+
validations
|
|
12303
|
+
});
|
|
12304
|
+
branchRequired.forEach((field) => requiredFields.add(field));
|
|
12305
|
+
}
|
|
12306
|
+
}
|
|
12307
|
+
return { required: requiredFields };
|
|
12308
|
+
}
|
|
12309
|
+
function buildSampleEmptyObject(schema = {}) {
|
|
12310
|
+
const sample = {};
|
|
12311
|
+
if (typeof schema !== "object" || !schema.properties) {
|
|
12312
|
+
return schema;
|
|
12313
|
+
}
|
|
12314
|
+
for (const key in schema.properties) {
|
|
12315
|
+
if (schema.properties[key].type === "object") {
|
|
12316
|
+
sample[key] = buildSampleEmptyObject(schema.properties[key]);
|
|
12317
|
+
} else if (schema.properties[key].type === "array") {
|
|
12318
|
+
const itemSchema = schema.properties[key].items;
|
|
12319
|
+
sample[key] = buildSampleEmptyObject(itemSchema);
|
|
12320
|
+
} else {
|
|
12321
|
+
sample[key] = true;
|
|
12322
|
+
}
|
|
12323
|
+
}
|
|
12324
|
+
return sample;
|
|
12325
|
+
}
|
|
12326
|
+
function validateInlineRules(jsonSchema, sampleEmptyObject) {
|
|
12327
|
+
const properties = (jsonSchema?.properties || jsonSchema?.items?.properties) ?? {};
|
|
12328
|
+
Object.entries(properties).filter(([, property2]) => property2["x-jsf-logic-computedAttrs"] !== void 0).forEach(([fieldName, property2]) => {
|
|
12329
|
+
Object.entries(property2["x-jsf-logic-computedAttrs"]).filter(([, value]) => typeof value === "object").forEach(([key, item]) => {
|
|
12330
|
+
Object.values(item).forEach((rule) => {
|
|
12331
|
+
checkRuleIntegrity(
|
|
12332
|
+
rule,
|
|
12333
|
+
fieldName,
|
|
12334
|
+
sampleEmptyObject,
|
|
12335
|
+
(item2) => `"${item2.var}" in inline rule in property "${fieldName}.x-jsf-logic-computedAttrs.${key}" does not exist as a JSON schema property.`
|
|
12336
|
+
);
|
|
12337
|
+
});
|
|
12338
|
+
});
|
|
12339
|
+
});
|
|
12340
|
+
}
|
|
12341
|
+
function checkRuleIntegrity(rule, id, data, errorMessage = (item) => `"${item.var}" in rule "${id}" does not exist as a JSON schema property.`) {
|
|
12342
|
+
Object.values(rule ?? {}).map((subRule) => {
|
|
12343
|
+
if (!Array.isArray(subRule) && subRule !== null && subRule !== void 0)
|
|
12344
|
+
return;
|
|
12345
|
+
subRule.map((item) => {
|
|
12346
|
+
const isVar = item !== null && typeof item === "object" && Object.hasOwn(item, "var");
|
|
12347
|
+
if (isVar) {
|
|
12348
|
+
const exists = import_json_logic_js.default.apply({ var: removeIndicesFromPath(item.var) }, data);
|
|
12349
|
+
if (exists === null) {
|
|
12350
|
+
throw Error(errorMessage(item));
|
|
12351
|
+
}
|
|
12352
|
+
} else {
|
|
12353
|
+
checkRuleIntegrity(item, id, data);
|
|
12354
|
+
}
|
|
12355
|
+
});
|
|
12356
|
+
});
|
|
12357
|
+
}
|
|
12358
|
+
function removeIndicesFromPath(path) {
|
|
12359
|
+
const intermediatePath = path.replace(/\.\d+\./g, ".");
|
|
12360
|
+
return intermediatePath.replace(/\.\d+$/, "");
|
|
11646
12361
|
}
|
|
11647
12362
|
|
|
11648
12363
|
// src/helpers.js
|
|
@@ -11652,8 +12367,8 @@ function hasType(type, typeName) {
|
|
|
11652
12367
|
function getField(fieldName, fields) {
|
|
11653
12368
|
return fields.find(({ name }) => name === fieldName);
|
|
11654
12369
|
}
|
|
11655
|
-
function validateFieldSchema(field, value) {
|
|
11656
|
-
const validator = buildYupSchema(field);
|
|
12370
|
+
function validateFieldSchema(field, value, validations) {
|
|
12371
|
+
const validator = buildYupSchema(field, {}, validations);
|
|
11657
12372
|
return validator().isValidSync(value);
|
|
11658
12373
|
}
|
|
11659
12374
|
function compareFormValueWithSchemaValue(formValue, schemaValue) {
|
|
@@ -11718,7 +12433,7 @@ function getPrefillValues(fields, initialValues = {}) {
|
|
|
11718
12433
|
});
|
|
11719
12434
|
return initialValues;
|
|
11720
12435
|
}
|
|
11721
|
-
function updateField(field, requiredFields, node, formValues) {
|
|
12436
|
+
function updateField(field, requiredFields, node, formValues, validations, config) {
|
|
11722
12437
|
if (!field) {
|
|
11723
12438
|
return;
|
|
11724
12439
|
}
|
|
@@ -11740,8 +12455,25 @@ function updateField(field, requiredFields, node, formValues) {
|
|
|
11740
12455
|
}
|
|
11741
12456
|
}
|
|
11742
12457
|
});
|
|
12458
|
+
if (field.getComputedAttributes) {
|
|
12459
|
+
const computedFieldValues = field.getComputedAttributes({
|
|
12460
|
+
field,
|
|
12461
|
+
isRequired: fieldIsRequired,
|
|
12462
|
+
node,
|
|
12463
|
+
formValues,
|
|
12464
|
+
config,
|
|
12465
|
+
validations
|
|
12466
|
+
});
|
|
12467
|
+
updateValues(computedFieldValues);
|
|
12468
|
+
}
|
|
11743
12469
|
if (field.calculateConditionalProperties) {
|
|
11744
|
-
const newFieldValues = field.calculateConditionalProperties(
|
|
12470
|
+
const newFieldValues = field.calculateConditionalProperties(
|
|
12471
|
+
fieldIsRequired,
|
|
12472
|
+
node,
|
|
12473
|
+
validations,
|
|
12474
|
+
config,
|
|
12475
|
+
formValues
|
|
12476
|
+
);
|
|
11745
12477
|
updateValues(newFieldValues);
|
|
11746
12478
|
}
|
|
11747
12479
|
if (field.calculateCustomValidationProperties) {
|
|
@@ -11753,33 +12485,46 @@ function updateField(field, requiredFields, node, formValues) {
|
|
|
11753
12485
|
updateValues(newFieldValues);
|
|
11754
12486
|
}
|
|
11755
12487
|
}
|
|
11756
|
-
function processNode(
|
|
12488
|
+
function processNode({
|
|
12489
|
+
node,
|
|
12490
|
+
formValues,
|
|
12491
|
+
formFields,
|
|
12492
|
+
accRequired = /* @__PURE__ */ new Set(),
|
|
12493
|
+
parentID = "root",
|
|
12494
|
+
validations
|
|
12495
|
+
}) {
|
|
11757
12496
|
const requiredFields = new Set(accRequired);
|
|
11758
12497
|
Object.keys(node.properties ?? []).forEach((fieldName) => {
|
|
11759
12498
|
const field = getField(fieldName, formFields);
|
|
11760
|
-
updateField(field, requiredFields, node, formValues);
|
|
12499
|
+
updateField(field, requiredFields, node, formValues, validations, { parentID });
|
|
11761
12500
|
});
|
|
11762
12501
|
node.required?.forEach((fieldName) => {
|
|
11763
12502
|
requiredFields.add(fieldName);
|
|
11764
|
-
updateField(getField(fieldName, formFields), requiredFields, node, formValues
|
|
12503
|
+
updateField(getField(fieldName, formFields), requiredFields, node, formValues, validations, {
|
|
12504
|
+
parentID
|
|
12505
|
+
});
|
|
11765
12506
|
});
|
|
11766
12507
|
if (node.if) {
|
|
11767
|
-
const matchesCondition = checkIfConditionMatches(node, formValues, formFields);
|
|
12508
|
+
const matchesCondition = checkIfConditionMatches(node, formValues, formFields, validations);
|
|
11768
12509
|
if (matchesCondition && node.then) {
|
|
11769
|
-
const { required: branchRequired } = processNode(
|
|
11770
|
-
node.then,
|
|
12510
|
+
const { required: branchRequired } = processNode({
|
|
12511
|
+
node: node.then,
|
|
11771
12512
|
formValues,
|
|
11772
12513
|
formFields,
|
|
11773
|
-
requiredFields
|
|
11774
|
-
|
|
12514
|
+
accRequired: requiredFields,
|
|
12515
|
+
parentID,
|
|
12516
|
+
validations
|
|
12517
|
+
});
|
|
11775
12518
|
branchRequired.forEach((field) => requiredFields.add(field));
|
|
11776
12519
|
} else if (node.else) {
|
|
11777
|
-
const { required: branchRequired } = processNode(
|
|
11778
|
-
node.else,
|
|
12520
|
+
const { required: branchRequired } = processNode({
|
|
12521
|
+
node: node.else,
|
|
11779
12522
|
formValues,
|
|
11780
12523
|
formFields,
|
|
11781
|
-
requiredFields
|
|
11782
|
-
|
|
12524
|
+
accRequired: requiredFields,
|
|
12525
|
+
parentID,
|
|
12526
|
+
validations
|
|
12527
|
+
});
|
|
11783
12528
|
branchRequired.forEach((field) => requiredFields.add(field));
|
|
11784
12529
|
}
|
|
11785
12530
|
}
|
|
@@ -11791,12 +12536,21 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
|
|
|
11791
12536
|
node.anyOf.forEach(({ required: required2 = [] }) => {
|
|
11792
12537
|
required2.forEach((fieldName) => {
|
|
11793
12538
|
const field = getField(fieldName, formFields);
|
|
11794
|
-
updateField(field, requiredFields, node, formValues);
|
|
12539
|
+
updateField(field, requiredFields, node, formValues, validations, { parentID });
|
|
11795
12540
|
});
|
|
11796
12541
|
});
|
|
11797
12542
|
}
|
|
11798
12543
|
if (node.allOf) {
|
|
11799
|
-
node.allOf.map(
|
|
12544
|
+
node.allOf.map(
|
|
12545
|
+
(allOfNode) => processNode({
|
|
12546
|
+
node: allOfNode,
|
|
12547
|
+
formValues,
|
|
12548
|
+
formFields,
|
|
12549
|
+
accRequired: requiredFields,
|
|
12550
|
+
parentID,
|
|
12551
|
+
validations
|
|
12552
|
+
})
|
|
12553
|
+
).forEach(({ required: allOfItemRequired }) => {
|
|
11800
12554
|
allOfItemRequired.forEach(requiredFields.add, requiredFields);
|
|
11801
12555
|
});
|
|
11802
12556
|
}
|
|
@@ -11804,10 +12558,27 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
|
|
|
11804
12558
|
Object.entries(node.properties).forEach(([name, nestedNode]) => {
|
|
11805
12559
|
const inputType = getInputType(nestedNode);
|
|
11806
12560
|
if (inputType === supportedTypes.FIELDSET) {
|
|
11807
|
-
processNode(
|
|
12561
|
+
processNode({
|
|
12562
|
+
node: nestedNode,
|
|
12563
|
+
formValues: formValues[name] || {},
|
|
12564
|
+
formFields: getField(name, formFields).fields,
|
|
12565
|
+
validations,
|
|
12566
|
+
parentID: name
|
|
12567
|
+
});
|
|
11808
12568
|
}
|
|
11809
12569
|
});
|
|
11810
12570
|
}
|
|
12571
|
+
if (node["x-jsf-logic"]) {
|
|
12572
|
+
const { required: requiredFromLogic } = processJSONLogicNode({
|
|
12573
|
+
node: node["x-jsf-logic"],
|
|
12574
|
+
formValues,
|
|
12575
|
+
formFields,
|
|
12576
|
+
accRequired: requiredFields,
|
|
12577
|
+
parentID,
|
|
12578
|
+
validations
|
|
12579
|
+
});
|
|
12580
|
+
requiredFromLogic.forEach((field) => requiredFields.add(field));
|
|
12581
|
+
}
|
|
11811
12582
|
return {
|
|
11812
12583
|
required: requiredFields
|
|
11813
12584
|
};
|
|
@@ -11822,11 +12593,11 @@ function clearValuesIfNotVisible(fields, formValues) {
|
|
|
11822
12593
|
}
|
|
11823
12594
|
});
|
|
11824
12595
|
}
|
|
11825
|
-
function updateFieldsProperties(fields, formValues, jsonSchema) {
|
|
12596
|
+
function updateFieldsProperties(fields, formValues, jsonSchema, validations) {
|
|
11826
12597
|
if (!jsonSchema?.properties) {
|
|
11827
12598
|
return;
|
|
11828
12599
|
}
|
|
11829
|
-
processNode(jsonSchema, formValues, fields);
|
|
12600
|
+
processNode({ node: jsonSchema, formValues, formFields: fields, validations });
|
|
11830
12601
|
clearValuesIfNotVisible(fields, formValues);
|
|
11831
12602
|
}
|
|
11832
12603
|
var notNullOption = (opt) => opt.const !== null;
|
|
@@ -11869,11 +12640,20 @@ function extractParametersFromNode(schemaNode) {
|
|
|
11869
12640
|
}
|
|
11870
12641
|
const presentation = pickXKey(schemaNode, "presentation") ?? {};
|
|
11871
12642
|
const errorMessage = pickXKey(schemaNode, "errorMessage") ?? {};
|
|
12643
|
+
const requiredValidations = schemaNode["x-jsf-logic-validations"];
|
|
12644
|
+
const computedAttributes = schemaNode["x-jsf-logic-computedAttrs"];
|
|
12645
|
+
const decoratedComputedAttributes = {
|
|
12646
|
+
...computedAttributes ?? {},
|
|
12647
|
+
...computedAttributes?.const && computedAttributes?.default ? { value: computedAttributes.const } : {}
|
|
12648
|
+
};
|
|
11872
12649
|
const node = (0, import_omit.default)(schemaNode, ["x-jsf-presentation", "presentation"]);
|
|
11873
12650
|
const description = presentation?.description || node.description;
|
|
11874
12651
|
const statementDescription = containsHTML(presentation.statement?.description) ? wrapWithSpan(presentation.statement.description, { class: "jsf-statement" }) : presentation.statement?.description;
|
|
11875
12652
|
return (0, import_omitBy.default)(
|
|
11876
12653
|
{
|
|
12654
|
+
const: node.const,
|
|
12655
|
+
// This is a "forced value" when both const and default are present.
|
|
12656
|
+
...node.const && node.default ? { value: node.const } : {},
|
|
11877
12657
|
label: node.title,
|
|
11878
12658
|
readOnly: node.readOnly,
|
|
11879
12659
|
...node.deprecated && {
|
|
@@ -11908,6 +12688,8 @@ function extractParametersFromNode(schemaNode) {
|
|
|
11908
12688
|
},
|
|
11909
12689
|
// Handle [name].presentation
|
|
11910
12690
|
...presentation,
|
|
12691
|
+
requiredValidations,
|
|
12692
|
+
computedAttributes: decoratedComputedAttributes,
|
|
11911
12693
|
description: containsHTML(description) ? wrapWithSpan(description, {
|
|
11912
12694
|
class: "jsf-description"
|
|
11913
12695
|
}) : description,
|
|
@@ -11944,9 +12726,9 @@ function yupToFormErrors(yupError) {
|
|
|
11944
12726
|
}
|
|
11945
12727
|
return errors;
|
|
11946
12728
|
}
|
|
11947
|
-
var handleValuesChange = (fields, jsonSchema, config) => (values2) => {
|
|
11948
|
-
updateFieldsProperties(fields, values2, jsonSchema);
|
|
11949
|
-
const lazySchema = lazy(() => buildCompleteYupSchema(fields, config));
|
|
12729
|
+
var handleValuesChange = (fields, jsonSchema, config, validations) => (values2) => {
|
|
12730
|
+
updateFieldsProperties(fields, values2, jsonSchema, validations);
|
|
12731
|
+
const lazySchema = lazy(() => buildCompleteYupSchema(fields, config, validations));
|
|
11950
12732
|
let errors;
|
|
11951
12733
|
try {
|
|
11952
12734
|
lazySchema.validateSync(values2, {
|
|
@@ -12000,8 +12782,8 @@ function rebuildFieldset(fields, property2) {
|
|
|
12000
12782
|
required: isFieldRequired(property2, field)
|
|
12001
12783
|
}));
|
|
12002
12784
|
}
|
|
12003
|
-
function calculateConditionalProperties(fieldParams, customProperties) {
|
|
12004
|
-
return (isRequired, conditionBranch) => {
|
|
12785
|
+
function calculateConditionalProperties(fieldParams, customProperties, validations, config) {
|
|
12786
|
+
return (isRequired, conditionBranch, __, _, formValues) => {
|
|
12005
12787
|
const conditionalProperty = conditionBranch?.properties?.[fieldParams.name];
|
|
12006
12788
|
if (conditionalProperty) {
|
|
12007
12789
|
const presentation = pickXKey(conditionalProperty, "presentation") ?? {};
|
|
@@ -12015,17 +12797,31 @@ function calculateConditionalProperties(fieldParams, customProperties) {
|
|
|
12015
12797
|
fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
|
|
12016
12798
|
newFieldParams.fields = fieldSetFields;
|
|
12017
12799
|
}
|
|
12800
|
+
const { computedAttributes, ...restNewFieldParams } = newFieldParams;
|
|
12801
|
+
const calculatedComputedAttributes = computedAttributes ? calculateComputedAttributes(newFieldParams, config)({ validations, formValues }) : {};
|
|
12802
|
+
const requiredValidations = [
|
|
12803
|
+
...fieldParams.requiredValidations ?? [],
|
|
12804
|
+
...restNewFieldParams.requiredValidations ?? []
|
|
12805
|
+
];
|
|
12018
12806
|
const base = {
|
|
12019
12807
|
isVisible: true,
|
|
12020
12808
|
required: isRequired,
|
|
12021
12809
|
...presentation?.inputType && { type: presentation.inputType },
|
|
12022
|
-
|
|
12023
|
-
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12810
|
+
...calculatedComputedAttributes,
|
|
12811
|
+
...calculatedComputedAttributes.value ? { value: calculatedComputedAttributes.value } : { value: void 0 },
|
|
12812
|
+
schema: buildYupSchema(
|
|
12813
|
+
{
|
|
12814
|
+
...fieldParams,
|
|
12815
|
+
...restNewFieldParams,
|
|
12816
|
+
...calculatedComputedAttributes,
|
|
12817
|
+
requiredValidations,
|
|
12818
|
+
// If there are inner fields (case of fieldset) they need to be updated based on the condition
|
|
12819
|
+
fields: fieldSetFields,
|
|
12820
|
+
required: isRequired
|
|
12821
|
+
},
|
|
12822
|
+
config,
|
|
12823
|
+
validations
|
|
12824
|
+
)
|
|
12029
12825
|
};
|
|
12030
12826
|
return (0, import_omit2.default)((0, import_merge2.default)(base, presentation, newFieldParams), ["inputType"]);
|
|
12031
12827
|
}
|
|
@@ -12119,14 +12915,19 @@ function sortByOrderOrPosition(a, b, order) {
|
|
|
12119
12915
|
function removeInvalidAttributes(fields) {
|
|
12120
12916
|
return (0, import_omit3.default)(fields, ["items", "maxFileSize", "isDynamic"]);
|
|
12121
12917
|
}
|
|
12122
|
-
function buildFieldParameters(name, fieldProperties, required2 = [], config = {}) {
|
|
12918
|
+
function buildFieldParameters(name, fieldProperties, required2 = [], config = {}, validations) {
|
|
12123
12919
|
const { position } = pickXKey(fieldProperties, "presentation") ?? {};
|
|
12124
12920
|
let fields;
|
|
12125
12921
|
const inputType = getInputType(fieldProperties, config.strictInputType, name);
|
|
12126
12922
|
if (inputType === supportedTypes.FIELDSET) {
|
|
12127
|
-
fields = getFieldsFromJSONSchema(
|
|
12128
|
-
|
|
12129
|
-
|
|
12923
|
+
fields = getFieldsFromJSONSchema(
|
|
12924
|
+
fieldProperties,
|
|
12925
|
+
{
|
|
12926
|
+
customProperties: (0, import_get4.default)(config, `customProperties.${name}`, {}),
|
|
12927
|
+
parentID: name
|
|
12928
|
+
},
|
|
12929
|
+
validations
|
|
12930
|
+
);
|
|
12130
12931
|
}
|
|
12131
12932
|
const result = {
|
|
12132
12933
|
name,
|
|
@@ -12141,9 +12942,9 @@ function buildFieldParameters(name, fieldProperties, required2 = [], config = {}
|
|
|
12141
12942
|
};
|
|
12142
12943
|
return (0, import_omitBy2.default)(result, import_isNil3.default);
|
|
12143
12944
|
}
|
|
12144
|
-
function convertJSONSchemaPropertiesToFieldParameters({ properties, required: required2, "x-jsf-order": order }, config = {}) {
|
|
12945
|
+
function convertJSONSchemaPropertiesToFieldParameters({ properties, required: required2, "x-jsf-order": order }, config = {}, validations) {
|
|
12145
12946
|
const sortFields2 = (a, b) => sortByOrderOrPosition(a, b, order);
|
|
12146
|
-
return Object.entries(properties).filter(([, value]) => typeof value === "object").map(([key, value]) => buildFieldParameters(key, value, required2, config)).sort(sortFields2).map(({ position, ...fieldParams }) => fieldParams);
|
|
12947
|
+
return Object.entries(properties).filter(([, value]) => typeof value === "object").map(([key, value]) => buildFieldParameters(key, value, required2, config, validations)).sort(sortFields2).map(({ position, ...fieldParams }) => fieldParams);
|
|
12147
12948
|
}
|
|
12148
12949
|
function applyFieldsDependencies(fieldsParameters, node) {
|
|
12149
12950
|
if (node?.then) {
|
|
@@ -12165,6 +12966,9 @@ function applyFieldsDependencies(fieldsParameters, node) {
|
|
|
12165
12966
|
applyFieldsDependencies(fieldsParameters, condition);
|
|
12166
12967
|
});
|
|
12167
12968
|
}
|
|
12969
|
+
if (node?.["x-jsf-logic"]) {
|
|
12970
|
+
applyFieldsDependencies(fieldsParameters, node["x-jsf-logic"]);
|
|
12971
|
+
}
|
|
12168
12972
|
}
|
|
12169
12973
|
function getCustomPropertiesForField(fieldParams, config) {
|
|
12170
12974
|
return config?.customProperties?.[fieldParams.name];
|
|
@@ -12176,15 +12980,16 @@ function getComposeFunctionForField(fieldParams, hasCustomizations) {
|
|
|
12176
12980
|
}
|
|
12177
12981
|
return composeFn;
|
|
12178
12982
|
}
|
|
12179
|
-
function buildField(fieldParams, config, scopedJsonSchema) {
|
|
12983
|
+
function buildField(fieldParams, config, scopedJsonSchema, validations) {
|
|
12180
12984
|
const customProperties = getCustomPropertiesForField(fieldParams, config);
|
|
12181
12985
|
const composeFn = getComposeFunctionForField(fieldParams, !!customProperties);
|
|
12182
|
-
const yupSchema = buildYupSchema(fieldParams, config);
|
|
12183
|
-
const calculateConditionalFieldsClosure = fieldParams.isDynamic && calculateConditionalProperties(fieldParams, customProperties);
|
|
12986
|
+
const yupSchema = buildYupSchema(fieldParams, config, validations);
|
|
12987
|
+
const calculateConditionalFieldsClosure = fieldParams.isDynamic && calculateConditionalProperties(fieldParams, customProperties, validations, config);
|
|
12184
12988
|
const calculateCustomValidationPropertiesClosure = calculateCustomValidationProperties(
|
|
12185
12989
|
fieldParams,
|
|
12186
12990
|
customProperties
|
|
12187
12991
|
);
|
|
12992
|
+
const getComputedAttributes = Object.keys(fieldParams.computedAttributes).length > 0 && calculateComputedAttributes(fieldParams, config);
|
|
12188
12993
|
const hasCustomValidations = !!customProperties && (0, import_size.default)((0, import_pick2.default)(customProperties, SUPPORTED_CUSTOM_VALIDATION_FIELD_PARAMS)) > 0;
|
|
12189
12994
|
const finalFieldParams = {
|
|
12190
12995
|
// invalid attribute cleanup
|
|
@@ -12197,6 +13002,7 @@ function buildField(fieldParams, config, scopedJsonSchema) {
|
|
|
12197
13002
|
...hasCustomValidations && {
|
|
12198
13003
|
calculateCustomValidationProperties: calculateCustomValidationPropertiesClosure
|
|
12199
13004
|
},
|
|
13005
|
+
...getComputedAttributes && { getComputedAttributes },
|
|
12200
13006
|
// field customization properties
|
|
12201
13007
|
...customProperties && { fieldCustomization: customProperties },
|
|
12202
13008
|
// base schema
|
|
@@ -12205,11 +13011,15 @@ function buildField(fieldParams, config, scopedJsonSchema) {
|
|
|
12205
13011
|
};
|
|
12206
13012
|
return composeFn(finalFieldParams);
|
|
12207
13013
|
}
|
|
12208
|
-
function getFieldsFromJSONSchema(scopedJsonSchema, config) {
|
|
13014
|
+
function getFieldsFromJSONSchema(scopedJsonSchema, config, validations) {
|
|
12209
13015
|
if (!scopedJsonSchema) {
|
|
12210
13016
|
return [];
|
|
12211
13017
|
}
|
|
12212
|
-
const fieldParamsList = convertJSONSchemaPropertiesToFieldParameters(
|
|
13018
|
+
const fieldParamsList = convertJSONSchemaPropertiesToFieldParameters(
|
|
13019
|
+
scopedJsonSchema,
|
|
13020
|
+
config,
|
|
13021
|
+
validations
|
|
13022
|
+
);
|
|
12213
13023
|
applyFieldsDependencies(fieldParamsList, scopedJsonSchema);
|
|
12214
13024
|
const fields = [];
|
|
12215
13025
|
fieldParamsList.forEach((fieldParams) => {
|
|
@@ -12229,11 +13039,11 @@ function getFieldsFromJSONSchema(scopedJsonSchema, config) {
|
|
|
12229
13039
|
fields: () => groupArrayFields,
|
|
12230
13040
|
addFieldText: fieldParams.addFieldText
|
|
12231
13041
|
};
|
|
12232
|
-
buildField(fieldParams, config, scopedJsonSchema).forEach((groupField) => {
|
|
13042
|
+
buildField(fieldParams, config, scopedJsonSchema, validations).forEach((groupField) => {
|
|
12233
13043
|
fields.push(groupField);
|
|
12234
13044
|
});
|
|
12235
13045
|
} else {
|
|
12236
|
-
fields.push(buildField(fieldParams, config, scopedJsonSchema));
|
|
13046
|
+
fields.push(buildField(fieldParams, config, scopedJsonSchema, validations));
|
|
12237
13047
|
}
|
|
12238
13048
|
});
|
|
12239
13049
|
return fields;
|
|
@@ -12244,9 +13054,15 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
|
|
|
12244
13054
|
...customConfig
|
|
12245
13055
|
};
|
|
12246
13056
|
try {
|
|
12247
|
-
const
|
|
12248
|
-
const
|
|
12249
|
-
|
|
13057
|
+
const validations = createValidationChecker(jsonSchema);
|
|
13058
|
+
const fields = getFieldsFromJSONSchema(jsonSchema, config, validations);
|
|
13059
|
+
const handleValidation = handleValuesChange(fields, jsonSchema, config, validations);
|
|
13060
|
+
updateFieldsProperties(
|
|
13061
|
+
fields,
|
|
13062
|
+
getPrefillValues(fields, config.initialValues),
|
|
13063
|
+
jsonSchema,
|
|
13064
|
+
validations
|
|
13065
|
+
);
|
|
12250
13066
|
return {
|
|
12251
13067
|
fields,
|
|
12252
13068
|
handleValidation,
|