@wix/form-public 0.46.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1727,6 +1727,267 @@ var init_errors = __esm({
1727
1727
  }
1728
1728
  });
1729
1729
 
1730
+ // ../form-validator/dist/esm/lib/error-derivation/types.js
1731
+ var DateFieldErrorSuffix, DateTimeFieldErrorSuffix;
1732
+ var init_types = __esm({
1733
+ "../form-validator/dist/esm/lib/error-derivation/types.js"() {
1734
+ (function(DateFieldErrorSuffix2) {
1735
+ DateFieldErrorSuffix2["YEAR_MONTH"] = "year-month";
1736
+ DateFieldErrorSuffix2["YEAR_DAY"] = "year-day";
1737
+ DateFieldErrorSuffix2["MONTH_DAY"] = "month-day";
1738
+ DateFieldErrorSuffix2["YEAR"] = "year";
1739
+ DateFieldErrorSuffix2["MONTH"] = "month";
1740
+ DateFieldErrorSuffix2["DAY"] = "day";
1741
+ })(DateFieldErrorSuffix || (DateFieldErrorSuffix = {}));
1742
+ (function(DateTimeFieldErrorSuffix2) {
1743
+ DateTimeFieldErrorSuffix2["YEAR_MONTH_TIME"] = "year-month-time";
1744
+ DateTimeFieldErrorSuffix2["YEAR_DAY_TIME"] = "year-day-time";
1745
+ DateTimeFieldErrorSuffix2["MONTH_DAY_TIME"] = "month-day-time";
1746
+ DateTimeFieldErrorSuffix2["YEAR_TIME"] = "year-time";
1747
+ DateTimeFieldErrorSuffix2["MONTH_TIME"] = "month-time";
1748
+ DateTimeFieldErrorSuffix2["DAY_TIME"] = "day-time";
1749
+ DateTimeFieldErrorSuffix2["TIME"] = "time";
1750
+ })(DateTimeFieldErrorSuffix || (DateTimeFieldErrorSuffix = {}));
1751
+ }
1752
+ });
1753
+
1754
+ // ../form-validator/dist/esm/lib/error-derivation/helpers.js
1755
+ function getDecimalPlacesCount(multipleOf) {
1756
+ return String(multipleOf).split(".")?.[1]?.length || 0;
1757
+ }
1758
+ function deserializeDate(value) {
1759
+ if (!value) {
1760
+ return {};
1761
+ }
1762
+ const parts = value.split("-");
1763
+ if (parts.length === 3) {
1764
+ const [year, month, day] = parts;
1765
+ return {
1766
+ YEAR: year || void 0,
1767
+ MONTH: month || void 0,
1768
+ DAY: day || void 0
1769
+ };
1770
+ }
1771
+ return {};
1772
+ }
1773
+ function parseDateTimeValue(value) {
1774
+ if (!value) {
1775
+ return ["", ""];
1776
+ }
1777
+ const separator = value.indexOf("T") > -1 ? "T" : " ";
1778
+ const [date, time] = value.split(separator);
1779
+ return [date || "", time || ""];
1780
+ }
1781
+ function isTimeComplete(time) {
1782
+ if (!time) {
1783
+ return false;
1784
+ }
1785
+ const [hours, minutes] = time.split(":");
1786
+ return !!hours && !!minutes;
1787
+ }
1788
+ var init_helpers = __esm({
1789
+ "../form-validator/dist/esm/lib/error-derivation/helpers.js"() {
1790
+ }
1791
+ });
1792
+
1793
+ // ../form-validator/dist/esm/lib/error-derivation/error-derivers.js
1794
+ function deriveError(validation, error, options) {
1795
+ if (!error) {
1796
+ return error;
1797
+ }
1798
+ return derivePatternError(error) ?? deriveCharacterLengthError(validation, error) ?? deriveItemsCountError(validation, error) ?? deriveNumericValueError(validation, error) ?? deriveDateFieldError(validation, error, options?.value) ?? deriveDateTimeFieldError(validation, error, options?.value) ?? error;
1799
+ }
1800
+ function derivePatternError(error) {
1801
+ if (!error || error.errorType !== ErrorType.PATTERN_ERROR) {
1802
+ return void 0;
1803
+ }
1804
+ return {
1805
+ ...error,
1806
+ errorType: ErrorType.INVALID_VALUE_FOR_PATTERN_ERROR
1807
+ };
1808
+ }
1809
+ function deriveCharacterLengthError(validation, error) {
1810
+ if (!error) {
1811
+ return void 0;
1812
+ }
1813
+ const isMinMaxLengthError = error.errorType === ErrorType.MIN_LENGTH_ERROR || error.errorType === ErrorType.MAX_LENGTH_ERROR;
1814
+ if (!isMinMaxLengthError) {
1815
+ return void 0;
1816
+ }
1817
+ const minLength = validation?.string?.minLength;
1818
+ const maxLength = validation?.string?.maxLength;
1819
+ const hasMinMax = minLength && maxLength;
1820
+ if (hasMinMax && minLength === maxLength) {
1821
+ return {
1822
+ ...error,
1823
+ errorType: ErrorType.EXACT_CHARACTER_LENGTH_ERROR,
1824
+ errorMessage: `must have exactly ${minLength} characters`
1825
+ };
1826
+ }
1827
+ if (hasMinMax && minLength !== maxLength) {
1828
+ return {
1829
+ ...error,
1830
+ errorType: ErrorType.CHARACTER_LENGTH_RANGE_ERROR,
1831
+ errorMessage: `must have between ${minLength} and ${maxLength} characters`,
1832
+ params: {
1833
+ minLimit: minLength,
1834
+ maxLimit: maxLength
1835
+ }
1836
+ };
1837
+ }
1838
+ return void 0;
1839
+ }
1840
+ function deriveItemsCountError(validation, error) {
1841
+ if (!error) {
1842
+ return void 0;
1843
+ }
1844
+ const isMinMaxItemsError = error.errorType === ErrorType.MIN_ITEMS_ERROR || error.errorType === ErrorType.MAX_ITEMS_ERROR;
1845
+ if (!isMinMaxItemsError) {
1846
+ return void 0;
1847
+ }
1848
+ const minItems = validation?.array?.minItems;
1849
+ const maxItems = validation?.array?.maxItems;
1850
+ const hasMinMax = minItems && maxItems;
1851
+ if (hasMinMax && minItems === maxItems) {
1852
+ return {
1853
+ ...error,
1854
+ errorType: ErrorType.EXACT_ITEMS_NUMBER_ERROR,
1855
+ errorMessage: `must choose ${minItems} options`
1856
+ };
1857
+ }
1858
+ return void 0;
1859
+ }
1860
+ function deriveNumericValueError(validation, error) {
1861
+ if (!error) {
1862
+ return void 0;
1863
+ }
1864
+ const isMinMaxValueError = error.errorType === ErrorType.MIN_VALUE_ERROR || error.errorType === ErrorType.MAX_VALUE_ERROR;
1865
+ const isMultipleOfValueError = error.errorType === ErrorType.MULTIPLE_OF_VALUE_ERROR;
1866
+ if (isMinMaxValueError) {
1867
+ const { minPrice, maxPrice } = validation?.predefined?.paymentOptions?.products?.[0]?.dynamicPriceOptions ?? {};
1868
+ const minimum = validation?.number?.minimum ?? minPrice;
1869
+ const maximum = validation?.number?.maximum ?? maxPrice;
1870
+ const hasMinMax = minimum !== void 0 && maximum !== void 0;
1871
+ if (hasMinMax && minimum !== maximum) {
1872
+ return {
1873
+ ...error,
1874
+ errorType: ErrorType.VALUE_RANGE_ERROR,
1875
+ errorMessage: `must be from ${minimum} to ${maximum}`,
1876
+ params: {
1877
+ minLimit: minimum,
1878
+ maxLimit: maximum
1879
+ }
1880
+ };
1881
+ }
1882
+ }
1883
+ if (isMultipleOfValueError) {
1884
+ const multipleOf = validation?.number?.multipleOf;
1885
+ if (multipleOf) {
1886
+ const decimalPlaces = getDecimalPlacesCount(multipleOf);
1887
+ return {
1888
+ ...error,
1889
+ errorType: ErrorType.DECIMAL_POINT_ERROR,
1890
+ errorMessage: `must have ${decimalPlaces} number(s) after the decimal point`,
1891
+ params: {
1892
+ number: decimalPlaces
1893
+ }
1894
+ };
1895
+ }
1896
+ }
1897
+ return void 0;
1898
+ }
1899
+ function deriveDateFieldError(_validation, error, value) {
1900
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1901
+ if (!error || !isFormatError) {
1902
+ return void 0;
1903
+ }
1904
+ const { YEAR: _YEAR, MONTH, DAY } = deserializeDate(value);
1905
+ const YEAR = _YEAR?.length === 4;
1906
+ if (!YEAR && !MONTH && !DAY) {
1907
+ return void 0;
1908
+ }
1909
+ const withErrorSuffix = (suffix) => ({
1910
+ ...error,
1911
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1912
+ params: {
1913
+ suffix
1914
+ }
1915
+ });
1916
+ if (!YEAR && !MONTH) {
1917
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_MONTH);
1918
+ } else if (!YEAR && !DAY) {
1919
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_DAY);
1920
+ } else if (!MONTH && !DAY) {
1921
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH_DAY);
1922
+ } else if (!YEAR) {
1923
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR);
1924
+ } else if (!MONTH) {
1925
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH);
1926
+ } else if (!DAY) {
1927
+ return withErrorSuffix(DateFieldErrorSuffix.DAY);
1928
+ } else {
1929
+ return void 0;
1930
+ }
1931
+ }
1932
+ function deriveDateTimeFieldError(validation, error, value) {
1933
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1934
+ if (!error || !isFormatError) {
1935
+ return void 0;
1936
+ }
1937
+ const [date, time] = parseDateTimeValue(value);
1938
+ const dateValidationError = deriveDateFieldError(validation, error, date);
1939
+ const { YEAR, MONTH, DAY } = deserializeDate(date);
1940
+ const timeComplete = isTimeComplete(time);
1941
+ if (!timeComplete && YEAR?.length !== 4 && !MONTH && !DAY) {
1942
+ return void 0;
1943
+ }
1944
+ if (!timeComplete) {
1945
+ const withErrorSuffix = (suffix) => ({
1946
+ ...error,
1947
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1948
+ params: {
1949
+ suffix
1950
+ }
1951
+ });
1952
+ const isDateFieldError = (validationError) => {
1953
+ return validationError?.errorType === ErrorType.INCOMPLETE_DATE_ERROR;
1954
+ };
1955
+ if (isDateFieldError(dateValidationError)) {
1956
+ switch (dateValidationError.params.suffix) {
1957
+ case DateFieldErrorSuffix.YEAR_MONTH:
1958
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_MONTH_TIME);
1959
+ case DateFieldErrorSuffix.YEAR_DAY:
1960
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_DAY_TIME);
1961
+ case DateFieldErrorSuffix.MONTH_DAY:
1962
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_DAY_TIME);
1963
+ case DateFieldErrorSuffix.YEAR:
1964
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_TIME);
1965
+ case DateFieldErrorSuffix.MONTH:
1966
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_TIME);
1967
+ case DateFieldErrorSuffix.DAY:
1968
+ return withErrorSuffix(DateTimeFieldErrorSuffix.DAY_TIME);
1969
+ }
1970
+ }
1971
+ return withErrorSuffix(DateTimeFieldErrorSuffix.TIME);
1972
+ }
1973
+ return dateValidationError;
1974
+ }
1975
+ var init_error_derivers = __esm({
1976
+ "../form-validator/dist/esm/lib/error-derivation/error-derivers.js"() {
1977
+ init_errors();
1978
+ init_types();
1979
+ init_helpers();
1980
+ }
1981
+ });
1982
+
1983
+ // ../form-validator/dist/esm/lib/error-derivation/index.js
1984
+ var init_error_derivation = __esm({
1985
+ "../form-validator/dist/esm/lib/error-derivation/index.js"() {
1986
+ init_error_derivers();
1987
+ init_types();
1988
+ }
1989
+ });
1990
+
1730
1991
  // ../../node_modules/ajv/dist/compile/codegen/code.js
1731
1992
  var require_code = __commonJS({
1732
1993
  "../../node_modules/ajv/dist/compile/codegen/code.js"(exports) {
@@ -13323,6 +13584,7 @@ var init_form_validator = __esm({
13323
13584
  "../form-validator/dist/esm/lib/submission-validation/form-validator.js"() {
13324
13585
  init_errors();
13325
13586
  init_types_impl();
13587
+ init_error_derivation();
13326
13588
  init_create_ajv_schema_validator();
13327
13589
  init_form_schema_mapper();
13328
13590
  init_property_schema_mapper();
@@ -13423,10 +13685,11 @@ var init_form_validator = __esm({
13423
13685
  throw new Error(`Missing precompiled schema for form ${formId}`);
13424
13686
  }
13425
13687
  validate2(fieldSubmission);
13426
- return makeValidationErrors(validate2.errors);
13688
+ const baseErrors = makeValidationErrors(validate2.errors);
13689
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13427
13690
  }
13428
13691
  validateSubmissionWithFieldMask(ajv, formId, fieldSubmission, fieldIdMasks) {
13429
- return fieldIdMasks.reduce((acc, mask) => {
13692
+ const baseErrors = fieldIdMasks.reduce((acc, mask) => {
13430
13693
  const validate2 = ajv.getSchema(`${formId}#/properties/${mask}`);
13431
13694
  if (!validate2) {
13432
13695
  acc.push({
@@ -13442,6 +13705,82 @@ var init_form_validator = __esm({
13442
13705
  }
13443
13706
  return acc;
13444
13707
  }, []);
13708
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13709
+ }
13710
+ deriveErrors(formId, errors, submission) {
13711
+ return errors.map((error) => {
13712
+ const validation = this.getFieldValidation(formId, error.errorPath);
13713
+ const value = this.getFieldValue(submission, error.errorPath);
13714
+ return deriveError(validation, error, { value });
13715
+ }).filter((error) => error !== void 0);
13716
+ }
13717
+ getFieldValidation(formId, fieldPath) {
13718
+ const form = this.formSchemas.find((f) => f.id === formId);
13719
+ if (!form) {
13720
+ return void 0;
13721
+ }
13722
+ const pathParts = fieldPath.split("/");
13723
+ const rootFieldId = pathParts[0];
13724
+ const platformisedField = [
13725
+ ...form.formFields ?? [],
13726
+ ...form.deletedFormFields ?? []
13727
+ ].find((f) => {
13728
+ if (f.id === rootFieldId) {
13729
+ return true;
13730
+ }
13731
+ const inputTarget = f.inputOptions?.target;
13732
+ if (inputTarget === rootFieldId) {
13733
+ return true;
13734
+ }
13735
+ return false;
13736
+ });
13737
+ const legacyField = [
13738
+ ...form.fields ?? [],
13739
+ ...form.deletedFields ?? []
13740
+ ].find((f) => {
13741
+ if (f.id === rootFieldId || f.target === rootFieldId) {
13742
+ return true;
13743
+ }
13744
+ return false;
13745
+ });
13746
+ if (platformisedField) {
13747
+ return this.getPlatformisedFieldValidation(platformisedField);
13748
+ } else if (legacyField) {
13749
+ return this.getLegacyFieldValidation(legacyField);
13750
+ } else {
13751
+ return void 0;
13752
+ }
13753
+ }
13754
+ getLegacyFieldValidation(field) {
13755
+ return field.validation;
13756
+ }
13757
+ getPlatformisedFieldValidation(field) {
13758
+ const inputOptions = field.inputOptions;
13759
+ if (!inputOptions) {
13760
+ return void 0;
13761
+ }
13762
+ const validation = {};
13763
+ if (inputOptions.numberOptions?.validation) {
13764
+ validation.number = inputOptions.numberOptions.validation;
13765
+ }
13766
+ if (inputOptions.stringOptions?.validation) {
13767
+ validation.string = inputOptions.stringOptions.validation;
13768
+ }
13769
+ if (inputOptions.arrayOptions?.validation) {
13770
+ validation.array = inputOptions.arrayOptions.validation;
13771
+ }
13772
+ if (Object.keys(validation).length > 0) {
13773
+ return validation;
13774
+ }
13775
+ return void 0;
13776
+ }
13777
+ getFieldValue(submission, fieldPath) {
13778
+ const pathParts = fieldPath.split("/");
13779
+ let value = submission;
13780
+ for (const part of pathParts) {
13781
+ value = value?.[part];
13782
+ }
13783
+ return typeof value === "string" ? value : void 0;
13445
13784
  }
13446
13785
  isFormDisabled(formSchema) {
13447
13786
  const isDeadlinePast = formSchema?.limitationRule?.dateTimeDeadline && new Date(formSchema.limitationRule.dateTimeDeadline).getTime() <= (/* @__PURE__ */ new Date()).getTime();
@@ -13481,7 +13820,7 @@ var init_form_validator = __esm({
13481
13820
  });
13482
13821
 
13483
13822
  // ../form-validator/dist/esm/lib/backward-compatibility/field-utils.js
13484
- function isInputField(field) {
13823
+ function isInputField2(field) {
13485
13824
  if (field.validation) {
13486
13825
  asRequired(field.target, field, "target");
13487
13826
  }
@@ -13503,7 +13842,7 @@ function compareSchemaBackwardCompatibility(newSchema, knownSchema) {
13503
13842
  };
13504
13843
  }
13505
13844
  function getNonCompatibleFields(newSchema, knownSchema) {
13506
- newSchema.fields?.forEach(isInputField);
13845
+ newSchema.fields?.forEach(isInputField2);
13507
13846
  const newFieldsWithTarget = (newSchema.fields ?? []).filter(fieldHasTarget);
13508
13847
  const knownInputFields = [
13509
13848
  ...knownSchema.fields ?? [],
@@ -13862,6 +14201,8 @@ var init_form_schema_validator = __esm({
13862
14201
  var esm_exports = {};
13863
14202
  __export(esm_exports, {
13864
14203
  CALLING_COUNTRY_CODES: () => CALLING_COUNTRY_CODES,
14204
+ DateFieldErrorSuffix: () => DateFieldErrorSuffix,
14205
+ DateTimeFieldErrorSuffix: () => DateTimeFieldErrorSuffix,
13865
14206
  ErrorTypes: () => ErrorType,
13866
14207
  FILE_UPLOAD_FIELD_TYPE: () => FILE_UPLOAD_FIELD_TYPE,
13867
14208
  FormSchemaValidator: () => FormSchemaValidator,
@@ -13879,6 +14220,7 @@ var init_esm = __esm({
13879
14220
  init_form_schema_validator();
13880
14221
  init_errors();
13881
14222
  init_calling_country_codes();
14223
+ init_error_derivation();
13882
14224
  }
13883
14225
  });
13884
14226
 
@@ -15829,7 +16171,7 @@ function useFormFields() {
15829
16171
  var import_extends5 = __toESM(require_extends());
15830
16172
 
15831
16173
  // ../form-fields/dist/esm/ui/form/types/form-view.js
15832
- function isInputField2(field) {
16174
+ function isInputField(field) {
15833
16175
  return Boolean(field.target);
15834
16176
  }
15835
16177
  function isNestedFormField(field) {
@@ -16152,7 +16494,7 @@ var FormField = (_ref) => {
16152
16494
  FieldLayout
16153
16495
  }));
16154
16496
  }
16155
- if (isInputField2(field)) {
16497
+ if (isInputField(field)) {
16156
16498
  return /* @__PURE__ */ React14__default.default.createElement(InputField, (0, import_extends5.default)({}, rest, {
16157
16499
  field,
16158
16500
  FieldLayout
@@ -16244,7 +16586,7 @@ var DefaultFieldLayout = (_ref) => {
16244
16586
  layout,
16245
16587
  fieldType
16246
16588
  } = fieldView;
16247
- const dataHook = `form-field-${isInputField2(fieldView) ? fieldView.target : fieldView.id}`;
16589
+ const dataHook = `form-field-${isInputField(fieldView) ? fieldView.target : fieldView.id}`;
16248
16590
  const dataAttributes = {
16249
16591
  "data-hook": dataHook,
16250
16592
  "data-field-type": fieldType