@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.
@@ -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.0-dev.20230810172154
5
- Generated: Thu, 10 Aug 2023 17:22:35 GMT
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
 
@@ -6421,6 +6421,384 @@ var require_randexp = __commonJS({
6421
6421
  }
6422
6422
  });
6423
6423
 
6424
+ // node_modules/json-logic-js/logic.js
6425
+ var require_logic = __commonJS({
6426
+ "node_modules/json-logic-js/logic.js"(exports2, module2) {
6427
+ (function(root2, factory) {
6428
+ if (typeof define === "function" && define.amd) {
6429
+ define(factory);
6430
+ } else if (typeof exports2 === "object") {
6431
+ module2.exports = factory();
6432
+ } else {
6433
+ root2.jsonLogic = factory();
6434
+ }
6435
+ })(exports2, function() {
6436
+ "use strict";
6437
+ if (!Array.isArray) {
6438
+ Array.isArray = function(arg) {
6439
+ return Object.prototype.toString.call(arg) === "[object Array]";
6440
+ };
6441
+ }
6442
+ function arrayUnique(array2) {
6443
+ var a = [];
6444
+ for (var i = 0, l = array2.length; i < l; i++) {
6445
+ if (a.indexOf(array2[i]) === -1) {
6446
+ a.push(array2[i]);
6447
+ }
6448
+ }
6449
+ return a;
6450
+ }
6451
+ var jsonLogic2 = {};
6452
+ var operations = {
6453
+ "==": function(a, b) {
6454
+ return a == b;
6455
+ },
6456
+ "===": function(a, b) {
6457
+ return a === b;
6458
+ },
6459
+ "!=": function(a, b) {
6460
+ return a != b;
6461
+ },
6462
+ "!==": function(a, b) {
6463
+ return a !== b;
6464
+ },
6465
+ ">": function(a, b) {
6466
+ return a > b;
6467
+ },
6468
+ ">=": function(a, b) {
6469
+ return a >= b;
6470
+ },
6471
+ "<": function(a, b, c) {
6472
+ return c === void 0 ? a < b : a < b && b < c;
6473
+ },
6474
+ "<=": function(a, b, c) {
6475
+ return c === void 0 ? a <= b : a <= b && b <= c;
6476
+ },
6477
+ "!!": function(a) {
6478
+ return jsonLogic2.truthy(a);
6479
+ },
6480
+ "!": function(a) {
6481
+ return !jsonLogic2.truthy(a);
6482
+ },
6483
+ "%": function(a, b) {
6484
+ return a % b;
6485
+ },
6486
+ "log": function(a) {
6487
+ console.log(a);
6488
+ return a;
6489
+ },
6490
+ "in": function(a, b) {
6491
+ if (!b || typeof b.indexOf === "undefined")
6492
+ return false;
6493
+ return b.indexOf(a) !== -1;
6494
+ },
6495
+ "cat": function() {
6496
+ return Array.prototype.join.call(arguments, "");
6497
+ },
6498
+ "substr": function(source, start, end) {
6499
+ if (end < 0) {
6500
+ var temp = String(source).substr(start);
6501
+ return temp.substr(0, temp.length + end);
6502
+ }
6503
+ return String(source).substr(start, end);
6504
+ },
6505
+ "+": function() {
6506
+ return Array.prototype.reduce.call(arguments, function(a, b) {
6507
+ return parseFloat(a, 10) + parseFloat(b, 10);
6508
+ }, 0);
6509
+ },
6510
+ "*": function() {
6511
+ return Array.prototype.reduce.call(arguments, function(a, b) {
6512
+ return parseFloat(a, 10) * parseFloat(b, 10);
6513
+ });
6514
+ },
6515
+ "-": function(a, b) {
6516
+ if (b === void 0) {
6517
+ return -a;
6518
+ } else {
6519
+ return a - b;
6520
+ }
6521
+ },
6522
+ "/": function(a, b) {
6523
+ return a / b;
6524
+ },
6525
+ "min": function() {
6526
+ return Math.min.apply(this, arguments);
6527
+ },
6528
+ "max": function() {
6529
+ return Math.max.apply(this, arguments);
6530
+ },
6531
+ "merge": function() {
6532
+ return Array.prototype.reduce.call(arguments, function(a, b) {
6533
+ return a.concat(b);
6534
+ }, []);
6535
+ },
6536
+ "var": function(a, b) {
6537
+ var not_found = b === void 0 ? null : b;
6538
+ var data = this;
6539
+ if (typeof a === "undefined" || a === "" || a === null) {
6540
+ return data;
6541
+ }
6542
+ var sub_props = String(a).split(".");
6543
+ for (var i = 0; i < sub_props.length; i++) {
6544
+ if (data === null || data === void 0) {
6545
+ return not_found;
6546
+ }
6547
+ data = data[sub_props[i]];
6548
+ if (data === void 0) {
6549
+ return not_found;
6550
+ }
6551
+ }
6552
+ return data;
6553
+ },
6554
+ "missing": function() {
6555
+ var missing = [];
6556
+ var keys2 = Array.isArray(arguments[0]) ? arguments[0] : arguments;
6557
+ for (var i = 0; i < keys2.length; i++) {
6558
+ var key = keys2[i];
6559
+ var value = jsonLogic2.apply({ "var": key }, this);
6560
+ if (value === null || value === "") {
6561
+ missing.push(key);
6562
+ }
6563
+ }
6564
+ return missing;
6565
+ },
6566
+ "missing_some": function(need_count, options) {
6567
+ var are_missing = jsonLogic2.apply({ "missing": options }, this);
6568
+ if (options.length - are_missing.length >= need_count) {
6569
+ return [];
6570
+ } else {
6571
+ return are_missing;
6572
+ }
6573
+ }
6574
+ };
6575
+ jsonLogic2.is_logic = function(logic) {
6576
+ return typeof logic === "object" && // An object
6577
+ logic !== null && // but not null
6578
+ !Array.isArray(logic) && // and not an array
6579
+ Object.keys(logic).length === 1;
6580
+ };
6581
+ jsonLogic2.truthy = function(value) {
6582
+ if (Array.isArray(value) && value.length === 0) {
6583
+ return false;
6584
+ }
6585
+ return !!value;
6586
+ };
6587
+ jsonLogic2.get_operator = function(logic) {
6588
+ return Object.keys(logic)[0];
6589
+ };
6590
+ jsonLogic2.get_values = function(logic) {
6591
+ return logic[jsonLogic2.get_operator(logic)];
6592
+ };
6593
+ jsonLogic2.apply = function(logic, data) {
6594
+ if (Array.isArray(logic)) {
6595
+ return logic.map(function(l) {
6596
+ return jsonLogic2.apply(l, data);
6597
+ });
6598
+ }
6599
+ if (!jsonLogic2.is_logic(logic)) {
6600
+ return logic;
6601
+ }
6602
+ var op = jsonLogic2.get_operator(logic);
6603
+ var values2 = logic[op];
6604
+ var i;
6605
+ var current;
6606
+ var scopedLogic;
6607
+ var scopedData;
6608
+ var initial;
6609
+ if (!Array.isArray(values2)) {
6610
+ values2 = [values2];
6611
+ }
6612
+ if (op === "if" || op == "?:") {
6613
+ for (i = 0; i < values2.length - 1; i += 2) {
6614
+ if (jsonLogic2.truthy(jsonLogic2.apply(values2[i], data))) {
6615
+ return jsonLogic2.apply(values2[i + 1], data);
6616
+ }
6617
+ }
6618
+ if (values2.length === i + 1) {
6619
+ return jsonLogic2.apply(values2[i], data);
6620
+ }
6621
+ return null;
6622
+ } else if (op === "and") {
6623
+ for (i = 0; i < values2.length; i += 1) {
6624
+ current = jsonLogic2.apply(values2[i], data);
6625
+ if (!jsonLogic2.truthy(current)) {
6626
+ return current;
6627
+ }
6628
+ }
6629
+ return current;
6630
+ } else if (op === "or") {
6631
+ for (i = 0; i < values2.length; i += 1) {
6632
+ current = jsonLogic2.apply(values2[i], data);
6633
+ if (jsonLogic2.truthy(current)) {
6634
+ return current;
6635
+ }
6636
+ }
6637
+ return current;
6638
+ } else if (op === "filter") {
6639
+ scopedData = jsonLogic2.apply(values2[0], data);
6640
+ scopedLogic = values2[1];
6641
+ if (!Array.isArray(scopedData)) {
6642
+ return [];
6643
+ }
6644
+ return scopedData.filter(function(datum) {
6645
+ return jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, datum));
6646
+ });
6647
+ } else if (op === "map") {
6648
+ scopedData = jsonLogic2.apply(values2[0], data);
6649
+ scopedLogic = values2[1];
6650
+ if (!Array.isArray(scopedData)) {
6651
+ return [];
6652
+ }
6653
+ return scopedData.map(function(datum) {
6654
+ return jsonLogic2.apply(scopedLogic, datum);
6655
+ });
6656
+ } else if (op === "reduce") {
6657
+ scopedData = jsonLogic2.apply(values2[0], data);
6658
+ scopedLogic = values2[1];
6659
+ initial = typeof values2[2] !== "undefined" ? values2[2] : null;
6660
+ if (!Array.isArray(scopedData)) {
6661
+ return initial;
6662
+ }
6663
+ return scopedData.reduce(
6664
+ function(accumulator, current2) {
6665
+ return jsonLogic2.apply(
6666
+ scopedLogic,
6667
+ { current: current2, accumulator }
6668
+ );
6669
+ },
6670
+ initial
6671
+ );
6672
+ } else if (op === "all") {
6673
+ scopedData = jsonLogic2.apply(values2[0], data);
6674
+ scopedLogic = values2[1];
6675
+ if (!Array.isArray(scopedData) || !scopedData.length) {
6676
+ return false;
6677
+ }
6678
+ for (i = 0; i < scopedData.length; i += 1) {
6679
+ if (!jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
6680
+ return false;
6681
+ }
6682
+ }
6683
+ return true;
6684
+ } else if (op === "none") {
6685
+ scopedData = jsonLogic2.apply(values2[0], data);
6686
+ scopedLogic = values2[1];
6687
+ if (!Array.isArray(scopedData) || !scopedData.length) {
6688
+ return true;
6689
+ }
6690
+ for (i = 0; i < scopedData.length; i += 1) {
6691
+ if (jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
6692
+ return false;
6693
+ }
6694
+ }
6695
+ return true;
6696
+ } else if (op === "some") {
6697
+ scopedData = jsonLogic2.apply(values2[0], data);
6698
+ scopedLogic = values2[1];
6699
+ if (!Array.isArray(scopedData) || !scopedData.length) {
6700
+ return false;
6701
+ }
6702
+ for (i = 0; i < scopedData.length; i += 1) {
6703
+ if (jsonLogic2.truthy(jsonLogic2.apply(scopedLogic, scopedData[i]))) {
6704
+ return true;
6705
+ }
6706
+ }
6707
+ return false;
6708
+ }
6709
+ values2 = values2.map(function(val) {
6710
+ return jsonLogic2.apply(val, data);
6711
+ });
6712
+ if (operations.hasOwnProperty(op) && typeof operations[op] === "function") {
6713
+ return operations[op].apply(data, values2);
6714
+ } else if (op.indexOf(".") > 0) {
6715
+ var sub_ops = String(op).split(".");
6716
+ var operation = operations;
6717
+ for (i = 0; i < sub_ops.length; i++) {
6718
+ if (!operation.hasOwnProperty(sub_ops[i])) {
6719
+ throw new Error("Unrecognized operation " + op + " (failed at " + sub_ops.slice(0, i + 1).join(".") + ")");
6720
+ }
6721
+ operation = operation[sub_ops[i]];
6722
+ }
6723
+ return operation.apply(data, values2);
6724
+ }
6725
+ throw new Error("Unrecognized operation " + op);
6726
+ };
6727
+ jsonLogic2.uses_data = function(logic) {
6728
+ var collection = [];
6729
+ if (jsonLogic2.is_logic(logic)) {
6730
+ var op = jsonLogic2.get_operator(logic);
6731
+ var values2 = logic[op];
6732
+ if (!Array.isArray(values2)) {
6733
+ values2 = [values2];
6734
+ }
6735
+ if (op === "var") {
6736
+ collection.push(values2[0]);
6737
+ } else {
6738
+ values2.forEach(function(val) {
6739
+ collection.push.apply(collection, jsonLogic2.uses_data(val));
6740
+ });
6741
+ }
6742
+ }
6743
+ return arrayUnique(collection);
6744
+ };
6745
+ jsonLogic2.add_operation = function(name, code) {
6746
+ operations[name] = code;
6747
+ };
6748
+ jsonLogic2.rm_operation = function(name) {
6749
+ delete operations[name];
6750
+ };
6751
+ jsonLogic2.rule_like = function(rule, pattern) {
6752
+ if (pattern === rule) {
6753
+ return true;
6754
+ }
6755
+ if (pattern === "@") {
6756
+ return true;
6757
+ }
6758
+ if (pattern === "number") {
6759
+ return typeof rule === "number";
6760
+ }
6761
+ if (pattern === "string") {
6762
+ return typeof rule === "string";
6763
+ }
6764
+ if (pattern === "array") {
6765
+ return Array.isArray(rule) && !jsonLogic2.is_logic(rule);
6766
+ }
6767
+ if (jsonLogic2.is_logic(pattern)) {
6768
+ if (jsonLogic2.is_logic(rule)) {
6769
+ var pattern_op = jsonLogic2.get_operator(pattern);
6770
+ var rule_op = jsonLogic2.get_operator(rule);
6771
+ if (pattern_op === "@" || pattern_op === rule_op) {
6772
+ return jsonLogic2.rule_like(
6773
+ jsonLogic2.get_values(rule, false),
6774
+ jsonLogic2.get_values(pattern, false)
6775
+ );
6776
+ }
6777
+ }
6778
+ return false;
6779
+ }
6780
+ if (Array.isArray(pattern)) {
6781
+ if (Array.isArray(rule)) {
6782
+ if (pattern.length !== rule.length) {
6783
+ return false;
6784
+ }
6785
+ for (var i = 0; i < pattern.length; i += 1) {
6786
+ if (!jsonLogic2.rule_like(rule[i], pattern[i])) {
6787
+ return false;
6788
+ }
6789
+ }
6790
+ return true;
6791
+ } else {
6792
+ return false;
6793
+ }
6794
+ }
6795
+ return false;
6796
+ };
6797
+ return jsonLogic2;
6798
+ });
6799
+ }
6800
+ });
6801
+
6424
6802
  // node_modules/lodash/_baseInRange.js
6425
6803
  var require_baseInRange = __commonJS({
6426
6804
  "node_modules/lodash/_baseInRange.js"(exports2, module2) {
@@ -11032,6 +11410,13 @@ function convertDiskSizeFromTo(from2, to) {
11032
11410
  return value * Math.pow(1024, units.indexOf(from2.toLowerCase())) / Math.pow(1024, units.indexOf(to.toLowerCase()));
11033
11411
  };
11034
11412
  }
11413
+ function containsHTML(str = "") {
11414
+ return /<[a-z][\s\S]*>/i.test(str);
11415
+ }
11416
+ function wrapWithSpan(html, properties = {}) {
11417
+ const attributes = Object.entries(properties).reduce((acc, [key, value]) => `${acc}${key}="${value}" `, "").trim();
11418
+ return `<span ${attributes}>${html}</span>`;
11419
+ }
11035
11420
  function hasProperty(object2, propertyName) {
11036
11421
  return Object.prototype.hasOwnProperty.call(object2, propertyName);
11037
11422
  }
@@ -11344,6 +11729,252 @@ function _composeFieldCustomClosure(defaultComposeFn) {
11344
11729
  var import_flow = __toESM(require_flow());
11345
11730
  var import_noop = __toESM(require_noop());
11346
11731
  var import_randexp = __toESM(require_randexp());
11732
+
11733
+ // src/jsonLogic.js
11734
+ var import_json_logic_js = __toESM(require_logic());
11735
+ function createValidationChecker(schema) {
11736
+ const scopes = /* @__PURE__ */ new Map();
11737
+ function createScopes(jsonSchema, key = "root") {
11738
+ const sampleEmptyObject = buildSampleEmptyObject(schema);
11739
+ scopes.set(key, createValidationsScope(jsonSchema));
11740
+ Object.entries(jsonSchema?.properties ?? {}).filter(([, property2]) => property2.type === "object" || property2.type === "array").forEach(([key2, property2]) => {
11741
+ if (property2.type === "array") {
11742
+ createScopes(property2.items, `${key2}[]`);
11743
+ } else {
11744
+ createScopes(property2, key2);
11745
+ }
11746
+ });
11747
+ validateInlineRules(jsonSchema, sampleEmptyObject);
11748
+ }
11749
+ createScopes(schema);
11750
+ return {
11751
+ scopes,
11752
+ getScope(name = "root") {
11753
+ return scopes.get(name);
11754
+ }
11755
+ };
11756
+ }
11757
+ function createValidationsScope(schema) {
11758
+ const validationMap = /* @__PURE__ */ new Map();
11759
+ const computedValuesMap = /* @__PURE__ */ new Map();
11760
+ const logic = schema?.["x-jsf-logic"] ?? {
11761
+ validations: {},
11762
+ computedValues: {}
11763
+ };
11764
+ const validations = Object.entries(logic.validations ?? {});
11765
+ const computedValues = Object.entries(logic.computedValues ?? {});
11766
+ const sampleEmptyObject = buildSampleEmptyObject(schema);
11767
+ validations.forEach(([id, validation]) => {
11768
+ if (!validation.rule) {
11769
+ throw Error(`[json-schema-form] json-logic error: Validation "${id}" has missing rule.`);
11770
+ }
11771
+ checkRuleIntegrity(validation.rule, id, sampleEmptyObject);
11772
+ validationMap.set(id, validation);
11773
+ });
11774
+ computedValues.forEach(([id, computedValue]) => {
11775
+ if (!computedValue.rule) {
11776
+ throw Error(`[json-schema-form] json-logic error: Computed value "${id}" has missing rule.`);
11777
+ }
11778
+ checkRuleIntegrity(computedValue.rule, id, sampleEmptyObject);
11779
+ computedValuesMap.set(id, computedValue);
11780
+ });
11781
+ function validate2(rule, values2) {
11782
+ return import_json_logic_js.default.apply(rule, replaceUndefinedValuesWithNulls(values2));
11783
+ }
11784
+ return {
11785
+ validationMap,
11786
+ computedValuesMap,
11787
+ validate: validate2,
11788
+ applyValidationRuleInCondition(id, values2) {
11789
+ const validation = validationMap.get(id);
11790
+ return validate2(validation.rule, values2);
11791
+ },
11792
+ applyComputedValueInField(id, values2, fieldName) {
11793
+ const validation = computedValuesMap.get(id);
11794
+ if (validation === void 0) {
11795
+ throw Error(
11796
+ `[json-schema-form] json-logic error: Computed value "${id}" doesn't exist in field "${fieldName}".`
11797
+ );
11798
+ }
11799
+ return validate2(validation.rule, values2);
11800
+ },
11801
+ applyComputedValueRuleInCondition(id, values2) {
11802
+ const validation = computedValuesMap.get(id);
11803
+ return validate2(validation.rule, values2);
11804
+ }
11805
+ };
11806
+ }
11807
+ function replaceUndefinedValuesWithNulls(values2 = {}) {
11808
+ return Object.entries(values2).reduce((prev, [key, value]) => {
11809
+ return { ...prev, [key]: value === void 0 ? null : value };
11810
+ }, {});
11811
+ }
11812
+ function yupSchemaWithCustomJSONLogic({ field, logic, config, id }) {
11813
+ const { parentID = "root" } = config;
11814
+ const validation = logic.getScope(parentID).validationMap.get(id);
11815
+ if (validation === void 0) {
11816
+ throw Error(
11817
+ `[json-schema-form] json-logic error: "${field.name}" required validation "${id}" doesn't exist.`
11818
+ );
11819
+ }
11820
+ return (yupSchema) => yupSchema.test(
11821
+ `${field.name}-validation-${id}`,
11822
+ validation?.errorMessage ?? "This field is invalid.",
11823
+ (value, { parent }) => {
11824
+ if (value === void 0 && !field.required)
11825
+ return true;
11826
+ return import_json_logic_js.default.apply(validation.rule, parent);
11827
+ }
11828
+ );
11829
+ }
11830
+ var HANDLEBARS_REGEX = /\{\{([^{}]+)\}\}/g;
11831
+ function replaceHandlebarsTemplates({
11832
+ value: toReplace,
11833
+ logic,
11834
+ formValues,
11835
+ parentID,
11836
+ name: fieldName
11837
+ }) {
11838
+ if (typeof toReplace === "string") {
11839
+ return toReplace.replace(HANDLEBARS_REGEX, (match, key) => {
11840
+ return logic.getScope(parentID).applyComputedValueInField(key.trim(), formValues, fieldName);
11841
+ });
11842
+ } else if (typeof toReplace === "object") {
11843
+ const { value, ...rules } = toReplace;
11844
+ if (Object.keys(rules).length > 1 && !value) {
11845
+ throw Error("Cannot define multiple rules without a template string with key `value`.");
11846
+ }
11847
+ const computedTemplateValue = Object.entries(rules).reduce((prev, [key, rule]) => {
11848
+ const computedValue = logic.getScope(parentID).evaluateValidation(rule, formValues);
11849
+ return prev.replaceAll(`{{${key}}}`, computedValue);
11850
+ }, value);
11851
+ return computedTemplateValue.replace(/\{\{([^{}]+)\}\}/g, (match, key) => {
11852
+ return logic.getScope(parentID).applyComputedValueInField(key.trim(), formValues, fieldName);
11853
+ });
11854
+ }
11855
+ return toReplace;
11856
+ }
11857
+ function calculateComputedAttributes(fieldParams, { parentID = "root" } = {}) {
11858
+ return ({ logic, isRequired, config, formValues }) => {
11859
+ const { name, computedAttributes } = fieldParams;
11860
+ const attributes = Object.fromEntries(
11861
+ Object.entries(computedAttributes).map(handleComputedAttribute(logic, formValues, parentID, name)).filter(([, value]) => value !== null)
11862
+ );
11863
+ return {
11864
+ ...attributes,
11865
+ schema: buildYupSchema(
11866
+ { ...fieldParams, ...attributes, required: isRequired },
11867
+ config,
11868
+ logic
11869
+ )
11870
+ };
11871
+ };
11872
+ }
11873
+ function handleComputedAttribute(logic, formValues, parentID, name) {
11874
+ return ([key, value]) => {
11875
+ switch (key) {
11876
+ case "description":
11877
+ return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
11878
+ case "title":
11879
+ return ["label", replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
11880
+ case "x-jsf-errorMessage":
11881
+ return [
11882
+ "errorMessage",
11883
+ handleNestedObjectForComputedValues(value, formValues, parentID, logic, name)
11884
+ ];
11885
+ case "x-jsf-presentation": {
11886
+ if (value.statement) {
11887
+ return [
11888
+ "statement",
11889
+ handleNestedObjectForComputedValues(value.statement, formValues, parentID, logic, name)
11890
+ ];
11891
+ }
11892
+ return [
11893
+ key,
11894
+ handleNestedObjectForComputedValues(value.statement, formValues, parentID, logic, name)
11895
+ ];
11896
+ }
11897
+ case "const":
11898
+ default:
11899
+ return [key, logic.getScope(parentID).applyComputedValueInField(value, formValues, name)];
11900
+ }
11901
+ };
11902
+ }
11903
+ function handleNestedObjectForComputedValues(values2, formValues, parentID, logic, name) {
11904
+ return Object.fromEntries(
11905
+ Object.entries(values2).map(([key, value]) => {
11906
+ return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })];
11907
+ })
11908
+ );
11909
+ }
11910
+ function buildSampleEmptyObject(schema = {}) {
11911
+ const sample = {};
11912
+ if (typeof schema !== "object" || !schema.properties) {
11913
+ return schema;
11914
+ }
11915
+ for (const key in schema.properties) {
11916
+ if (schema.properties[key].type === "object") {
11917
+ sample[key] = buildSampleEmptyObject(schema.properties[key]);
11918
+ } else if (schema.properties[key].type === "array") {
11919
+ const itemSchema = schema.properties[key].items;
11920
+ sample[key] = buildSampleEmptyObject(itemSchema);
11921
+ } else {
11922
+ sample[key] = true;
11923
+ }
11924
+ }
11925
+ return sample;
11926
+ }
11927
+ function validateInlineRules(jsonSchema, sampleEmptyObject) {
11928
+ const properties = (jsonSchema?.properties || jsonSchema?.items?.properties) ?? {};
11929
+ Object.entries(properties).filter(([, property2]) => property2["x-jsf-logic-computedAttrs"] !== void 0).forEach(([fieldName, property2]) => {
11930
+ Object.entries(property2["x-jsf-logic-computedAttrs"]).filter(([, value]) => typeof value === "object").forEach(([key, item]) => {
11931
+ Object.values(item).forEach((rule) => {
11932
+ checkRuleIntegrity(
11933
+ rule,
11934
+ fieldName,
11935
+ sampleEmptyObject,
11936
+ (item2) => `[json-schema-form] json-logic error: fieldName "${item2.var}" doesn't exist in field "${fieldName}.x-jsf-logic-computedAttrs.${key}".`
11937
+ );
11938
+ });
11939
+ });
11940
+ });
11941
+ }
11942
+ function checkRuleIntegrity(rule, id, data, errorMessage = (item) => `[json-schema-form] json-logic error: rule "${id}" has no variable "${item.var}".`) {
11943
+ Object.entries(rule ?? {}).map(([operator, subRule]) => {
11944
+ if (!Array.isArray(subRule) && subRule !== null && subRule !== void 0)
11945
+ return;
11946
+ throwIfUnknownOperator(operator, subRule, id);
11947
+ subRule.map((item) => {
11948
+ const isVar = item !== null && typeof item === "object" && Object.hasOwn(item, "var");
11949
+ if (isVar) {
11950
+ const exists = import_json_logic_js.default.apply({ var: removeIndicesFromPath(item.var) }, data);
11951
+ if (exists === null) {
11952
+ throw Error(errorMessage(item));
11953
+ }
11954
+ } else {
11955
+ checkRuleIntegrity(item, id, data);
11956
+ }
11957
+ });
11958
+ });
11959
+ }
11960
+ function throwIfUnknownOperator(operator, subRule, id) {
11961
+ try {
11962
+ import_json_logic_js.default.apply({ [operator]: subRule });
11963
+ } catch (e) {
11964
+ if (e.message === `Unrecognized operation ${operator}`) {
11965
+ throw Error(
11966
+ `[json-schema-form] json-logic error: in "${id}" rule there is an unknown operator "${operator}".`
11967
+ );
11968
+ }
11969
+ }
11970
+ }
11971
+ var regexToGetIndices = /\.\d+\./g;
11972
+ function removeIndicesFromPath(path) {
11973
+ const intermediatePath = path.replace(regexToGetIndices, ".");
11974
+ return intermediatePath.replace(/\.\d+$/, "");
11975
+ }
11976
+
11977
+ // src/yupSchema.js
11347
11978
  var DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
11348
11979
  var baseString = StringSchema().trim();
11349
11980
  var todayDateHint = (/* @__PURE__ */ new Date()).toISOString().substring(0, 10);
@@ -11466,7 +12097,7 @@ var getYupSchema = ({ inputType, ...field }) => {
11466
12097
  }
11467
12098
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
11468
12099
  };
11469
- function buildYupSchema(field, config) {
12100
+ function buildYupSchema(field, config, logic) {
11470
12101
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
11471
12102
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
11472
12103
  let baseSchema;
@@ -11539,6 +12170,13 @@ function buildYupSchema(field, config) {
11539
12170
  }) : true
11540
12171
  );
11541
12172
  }
12173
+ function withConst(yupSchema) {
12174
+ return yupSchema.test(
12175
+ "isConst",
12176
+ errorMessage.const ?? errorMessageFromConfig.const ?? `The only accepted value is ${propertyFields.const}.`,
12177
+ (value) => propertyFields.required === false && value === void 0 || value === null || value === propertyFields.const
12178
+ );
12179
+ }
11542
12180
  function withBaseSchema() {
11543
12181
  const customErrorMsg = errorMessage.type || errorMessageFromConfig.type;
11544
12182
  if (customErrorMsg) {
@@ -11606,6 +12244,14 @@ function buildYupSchema(field, config) {
11606
12244
  if (propertyFields.accept) {
11607
12245
  validators.push(withFileFormat);
11608
12246
  }
12247
+ if (propertyFields.const) {
12248
+ validators.push(withConst);
12249
+ }
12250
+ if (propertyFields.requiredValidations) {
12251
+ propertyFields.requiredValidations.forEach(
12252
+ (id) => validators.push(yupSchemaWithCustomJSONLogic({ field, id, logic, config }))
12253
+ );
12254
+ }
11609
12255
  return (0, import_flow.default)(validators);
11610
12256
  }
11611
12257
  function getNoSortEdges(fields = []) {
@@ -11645,8 +12291,8 @@ function hasType(type, typeName) {
11645
12291
  function getField(fieldName, fields) {
11646
12292
  return fields.find(({ name }) => name === fieldName);
11647
12293
  }
11648
- function validateFieldSchema(field, value) {
11649
- const validator = buildYupSchema(field);
12294
+ function validateFieldSchema(field, value, logic) {
12295
+ const validator = buildYupSchema(field, {}, logic);
11650
12296
  return validator().isValidSync(value);
11651
12297
  }
11652
12298
  function compareFormValueWithSchemaValue(formValue, schemaValue) {
@@ -11682,7 +12328,16 @@ function getPrefillSubFieldValues(field, defaultValues, parentFieldKeyPath) {
11682
12328
  initialValue[field.name] = subFieldValues;
11683
12329
  }
11684
12330
  } else {
11685
- initialValue = getPrefillValues([field], initialValue);
12331
+ if (typeof initialValue !== "object") {
12332
+ console.warn(
12333
+ `Field "${parentFieldKeyPath}"'s value is "${initialValue}", but should be type object.`
12334
+ );
12335
+ initialValue = getPrefillValues([field], {
12336
+ // TODO nested fieldsets are not handled
12337
+ });
12338
+ } else {
12339
+ initialValue = getPrefillValues([field], initialValue);
12340
+ }
11686
12341
  }
11687
12342
  return initialValue;
11688
12343
  }
@@ -11711,7 +12366,7 @@ function getPrefillValues(fields, initialValues = {}) {
11711
12366
  });
11712
12367
  return initialValues;
11713
12368
  }
11714
- function updateField(field, requiredFields, node, formValues) {
12369
+ function updateField(field, requiredFields, node, formValues, logic, config) {
11715
12370
  if (!field) {
11716
12371
  return;
11717
12372
  }
@@ -11733,6 +12388,17 @@ function updateField(field, requiredFields, node, formValues) {
11733
12388
  }
11734
12389
  }
11735
12390
  });
12391
+ if (field.getComputedAttributes) {
12392
+ const computedFieldValues = field.getComputedAttributes({
12393
+ field,
12394
+ isRequired: fieldIsRequired,
12395
+ node,
12396
+ formValues,
12397
+ config,
12398
+ logic
12399
+ });
12400
+ updateValues(computedFieldValues);
12401
+ }
11736
12402
  if (field.calculateConditionalProperties) {
11737
12403
  const newFieldValues = field.calculateConditionalProperties(fieldIsRequired, node);
11738
12404
  updateValues(newFieldValues);
@@ -11746,33 +12412,46 @@ function updateField(field, requiredFields, node, formValues) {
11746
12412
  updateValues(newFieldValues);
11747
12413
  }
11748
12414
  }
11749
- function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */ new Set()) {
12415
+ function processNode({
12416
+ node,
12417
+ formValues,
12418
+ formFields,
12419
+ accRequired = /* @__PURE__ */ new Set(),
12420
+ parentID = "root",
12421
+ logic
12422
+ }) {
11750
12423
  const requiredFields = new Set(accRequired);
11751
12424
  Object.keys(node.properties ?? []).forEach((fieldName) => {
11752
12425
  const field = getField(fieldName, formFields);
11753
- updateField(field, requiredFields, node, formValues);
12426
+ updateField(field, requiredFields, node, formValues, logic, { parentID });
11754
12427
  });
11755
12428
  node.required?.forEach((fieldName) => {
11756
12429
  requiredFields.add(fieldName);
11757
- updateField(getField(fieldName, formFields), requiredFields, node, formValues);
12430
+ updateField(getField(fieldName, formFields), requiredFields, node, formValues, logic, {
12431
+ parentID
12432
+ });
11758
12433
  });
11759
12434
  if (node.if) {
11760
- const matchesCondition = checkIfConditionMatches(node, formValues, formFields);
12435
+ const matchesCondition = checkIfConditionMatches(node, formValues, formFields, logic);
11761
12436
  if (matchesCondition && node.then) {
11762
- const { required: branchRequired } = processNode(
11763
- node.then,
12437
+ const { required: branchRequired } = processNode({
12438
+ node: node.then,
11764
12439
  formValues,
11765
12440
  formFields,
11766
- requiredFields
11767
- );
12441
+ accRequired: requiredFields,
12442
+ parentID,
12443
+ logic
12444
+ });
11768
12445
  branchRequired.forEach((field) => requiredFields.add(field));
11769
12446
  } else if (node.else) {
11770
- const { required: branchRequired } = processNode(
11771
- node.else,
12447
+ const { required: branchRequired } = processNode({
12448
+ node: node.else,
11772
12449
  formValues,
11773
12450
  formFields,
11774
- requiredFields
11775
- );
12451
+ accRequired: requiredFields,
12452
+ parentID,
12453
+ logic
12454
+ });
11776
12455
  branchRequired.forEach((field) => requiredFields.add(field));
11777
12456
  }
11778
12457
  }
@@ -11784,12 +12463,21 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
11784
12463
  node.anyOf.forEach(({ required: required2 = [] }) => {
11785
12464
  required2.forEach((fieldName) => {
11786
12465
  const field = getField(fieldName, formFields);
11787
- updateField(field, requiredFields, node, formValues);
12466
+ updateField(field, requiredFields, node, formValues, logic, { parentID });
11788
12467
  });
11789
12468
  });
11790
12469
  }
11791
12470
  if (node.allOf) {
11792
- node.allOf.map((allOfNode) => processNode(allOfNode, formValues, formFields, requiredFields)).forEach(({ required: allOfItemRequired }) => {
12471
+ node.allOf.map(
12472
+ (allOfNode) => processNode({
12473
+ node: allOfNode,
12474
+ formValues,
12475
+ formFields,
12476
+ accRequired: requiredFields,
12477
+ parentID,
12478
+ logic
12479
+ })
12480
+ ).forEach(({ required: allOfItemRequired }) => {
11793
12481
  allOfItemRequired.forEach(requiredFields.add, requiredFields);
11794
12482
  });
11795
12483
  }
@@ -11797,7 +12485,13 @@ function processNode(node, formValues, formFields, accRequired = /* @__PURE__ */
11797
12485
  Object.entries(node.properties).forEach(([name, nestedNode]) => {
11798
12486
  const inputType = getInputType(nestedNode);
11799
12487
  if (inputType === supportedTypes.FIELDSET) {
11800
- processNode(nestedNode, formValues[name] || {}, getField(name, formFields).fields);
12488
+ processNode({
12489
+ node: nestedNode,
12490
+ formValues: formValues[name] || {},
12491
+ formFields: getField(name, formFields).fields,
12492
+ parentID: name,
12493
+ logic
12494
+ });
11801
12495
  }
11802
12496
  });
11803
12497
  }
@@ -11815,11 +12509,11 @@ function clearValuesIfNotVisible(fields, formValues) {
11815
12509
  }
11816
12510
  });
11817
12511
  }
11818
- function updateFieldsProperties(fields, formValues, jsonSchema) {
12512
+ function updateFieldsProperties(fields, formValues, jsonSchema, logic) {
11819
12513
  if (!jsonSchema?.properties) {
11820
12514
  return;
11821
12515
  }
11822
- processNode(jsonSchema, formValues, fields);
12516
+ processNode({ node: jsonSchema, formValues, formFields: fields, logic });
11823
12517
  clearValuesIfNotVisible(fields, formValues);
11824
12518
  }
11825
12519
  var notNullOption = (opt) => opt.const !== null;
@@ -11862,11 +12556,16 @@ function extractParametersFromNode(schemaNode) {
11862
12556
  }
11863
12557
  const presentation = pickXKey(schemaNode, "presentation") ?? {};
11864
12558
  const errorMessage = pickXKey(schemaNode, "errorMessage") ?? {};
12559
+ const requiredValidations = schemaNode["x-jsf-logic-validations"];
12560
+ const computedAttributes = schemaNode["x-jsf-logic-computedAttrs"];
12561
+ const decoratedComputedAttributes = getDecoratedComputedAttributes(computedAttributes);
11865
12562
  const node = (0, import_omit.default)(schemaNode, ["x-jsf-presentation", "presentation"]);
11866
12563
  const description = presentation?.description || node.description;
11867
- const statementDescription = presentation.statement?.description;
12564
+ const statementDescription = containsHTML(presentation.statement?.description) ? wrapWithSpan(presentation.statement.description, { class: "jsf-statement" }) : presentation.statement?.description;
11868
12565
  return (0, import_omitBy.default)(
11869
12566
  {
12567
+ const: node.const,
12568
+ ...node.const && node.default ? { value: node.const } : {},
11870
12569
  label: node.title,
11871
12570
  readOnly: node.readOnly,
11872
12571
  ...node.deprecated && {
@@ -11901,8 +12600,12 @@ function extractParametersFromNode(schemaNode) {
11901
12600
  },
11902
12601
  // Handle [name].presentation
11903
12602
  ...presentation,
11904
- description,
11905
- extra: presentation.extra,
12603
+ requiredValidations,
12604
+ computedAttributes: decoratedComputedAttributes,
12605
+ description: containsHTML(description) ? wrapWithSpan(description, {
12606
+ class: "jsf-description"
12607
+ }) : description,
12608
+ extra: containsHTML(presentation.extra) ? wrapWithSpan(presentation.extra, { class: "jsf-extra" }) : presentation.extra,
11906
12609
  statement: presentation.statement && {
11907
12610
  ...presentation.statement,
11908
12611
  description: statementDescription
@@ -11935,8 +12638,8 @@ function yupToFormErrors(yupError) {
11935
12638
  }
11936
12639
  return errors;
11937
12640
  }
11938
- var handleValuesChange = (fields, jsonSchema, config) => (values2) => {
11939
- updateFieldsProperties(fields, values2, jsonSchema);
12641
+ var handleValuesChange = (fields, jsonSchema, config, logic) => (values2) => {
12642
+ updateFieldsProperties(fields, values2, jsonSchema, logic);
11940
12643
  const lazySchema = lazy(() => buildCompleteYupSchema(fields, config));
11941
12644
  let errors;
11942
12645
  try {
@@ -11955,6 +12658,12 @@ var handleValuesChange = (fields, jsonSchema, config) => (values2) => {
11955
12658
  formErrors: yupToFormErrors(errors)
11956
12659
  };
11957
12660
  };
12661
+ function getDecoratedComputedAttributes(computedAttributes) {
12662
+ return {
12663
+ ...computedAttributes ?? {},
12664
+ ...computedAttributes?.const && computedAttributes?.default ? { value: computedAttributes.const } : {}
12665
+ };
12666
+ }
11958
12667
 
11959
12668
  // src/calculateConditionalProperties.js
11960
12669
  function isFieldRequired(node, field) {
@@ -12156,6 +12865,9 @@ function applyFieldsDependencies(fieldsParameters, node) {
12156
12865
  applyFieldsDependencies(fieldsParameters, condition);
12157
12866
  });
12158
12867
  }
12868
+ if (node?.["x-jsf-logic"]) {
12869
+ applyFieldsDependencies(fieldsParameters, node["x-jsf-logic"]);
12870
+ }
12159
12871
  }
12160
12872
  function getCustomPropertiesForField(fieldParams, config) {
12161
12873
  return config?.customProperties?.[fieldParams.name];
@@ -12167,15 +12879,16 @@ function getComposeFunctionForField(fieldParams, hasCustomizations) {
12167
12879
  }
12168
12880
  return composeFn;
12169
12881
  }
12170
- function buildField(fieldParams, config, scopedJsonSchema) {
12882
+ function buildField(fieldParams, config, scopedJsonSchema, logic) {
12171
12883
  const customProperties = getCustomPropertiesForField(fieldParams, config);
12172
12884
  const composeFn = getComposeFunctionForField(fieldParams, !!customProperties);
12173
- const yupSchema = buildYupSchema(fieldParams, config);
12885
+ const yupSchema = buildYupSchema(fieldParams, config, logic);
12174
12886
  const calculateConditionalFieldsClosure = fieldParams.isDynamic && calculateConditionalProperties(fieldParams, customProperties);
12175
12887
  const calculateCustomValidationPropertiesClosure = calculateCustomValidationProperties(
12176
12888
  fieldParams,
12177
12889
  customProperties
12178
12890
  );
12891
+ const getComputedAttributes = Object.keys(fieldParams.computedAttributes).length > 0 && calculateComputedAttributes(fieldParams, config);
12179
12892
  const hasCustomValidations = !!customProperties && (0, import_size.default)((0, import_pick2.default)(customProperties, SUPPORTED_CUSTOM_VALIDATION_FIELD_PARAMS)) > 0;
12180
12893
  const finalFieldParams = {
12181
12894
  // invalid attribute cleanup
@@ -12188,6 +12901,7 @@ function buildField(fieldParams, config, scopedJsonSchema) {
12188
12901
  ...hasCustomValidations && {
12189
12902
  calculateCustomValidationProperties: calculateCustomValidationPropertiesClosure
12190
12903
  },
12904
+ ...getComputedAttributes && { getComputedAttributes },
12191
12905
  // field customization properties
12192
12906
  ...customProperties && { fieldCustomization: customProperties },
12193
12907
  // base schema
@@ -12196,7 +12910,7 @@ function buildField(fieldParams, config, scopedJsonSchema) {
12196
12910
  };
12197
12911
  return composeFn(finalFieldParams);
12198
12912
  }
12199
- function getFieldsFromJSONSchema(scopedJsonSchema, config) {
12913
+ function getFieldsFromJSONSchema(scopedJsonSchema, config, logic) {
12200
12914
  if (!scopedJsonSchema) {
12201
12915
  return [];
12202
12916
  }
@@ -12220,11 +12934,11 @@ function getFieldsFromJSONSchema(scopedJsonSchema, config) {
12220
12934
  fields: () => groupArrayFields,
12221
12935
  addFieldText: fieldParams.addFieldText
12222
12936
  };
12223
- buildField(fieldParams, config, scopedJsonSchema).forEach((groupField) => {
12937
+ buildField(fieldParams, config, scopedJsonSchema, logic).forEach((groupField) => {
12224
12938
  fields.push(groupField);
12225
12939
  });
12226
12940
  } else {
12227
- fields.push(buildField(fieldParams, config, scopedJsonSchema));
12941
+ fields.push(buildField(fieldParams, config, scopedJsonSchema, logic));
12228
12942
  }
12229
12943
  });
12230
12944
  return fields;
@@ -12235,9 +12949,15 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
12235
12949
  ...customConfig
12236
12950
  };
12237
12951
  try {
12238
- const fields = getFieldsFromJSONSchema(jsonSchema, config);
12239
- const handleValidation = handleValuesChange(fields, jsonSchema, config);
12240
- updateFieldsProperties(fields, getPrefillValues(fields, config.initialValues), jsonSchema);
12952
+ const logic = createValidationChecker(jsonSchema);
12953
+ const fields = getFieldsFromJSONSchema(jsonSchema, config, logic);
12954
+ const handleValidation = handleValuesChange(fields, jsonSchema, config, logic);
12955
+ updateFieldsProperties(
12956
+ fields,
12957
+ getPrefillValues(fields, config.initialValues),
12958
+ jsonSchema,
12959
+ logic
12960
+ );
12241
12961
  return {
12242
12962
  fields,
12243
12963
  handleValidation,