@wix/form-public 0.45.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.js CHANGED
@@ -900,6 +900,13 @@ var init_types_impl2 = __esm({
900
900
  ErrorType2["MISSING_SERVICE_OPTION_ERROR"] = "MISSING_SERVICE_OPTION_ERROR";
901
901
  ErrorType2["INVALID_SERVICE_OPTIONS_ERROR"] = "INVALID_SERVICE_OPTIONS_ERROR";
902
902
  ErrorType2["NO_AVAILABLE_SERVICE_OPTIONS_ERROR"] = "NO_AVAILABLE_SERVICE_OPTIONS_ERROR";
903
+ ErrorType2["EXACT_CHARACTER_LENGTH_ERROR"] = "EXACT_CHARACTER_LENGTH_ERROR";
904
+ ErrorType2["CHARACTER_LENGTH_RANGE_ERROR"] = "CHARACTER_LENGTH_RANGE_ERROR";
905
+ ErrorType2["VALUE_RANGE_ERROR"] = "VALUE_RANGE_ERROR";
906
+ ErrorType2["EXACT_ITEMS_NUMBER_ERROR"] = "EXACT_ITEMS_NUMBER_ERROR";
907
+ ErrorType2["DECIMAL_POINT_ERROR"] = "DECIMAL_POINT_ERROR";
908
+ ErrorType2["INCOMPLETE_DATE_ERROR"] = "INCOMPLETE_DATE_ERROR";
909
+ ErrorType2["INVALID_VALUE_FOR_PATTERN_ERROR"] = "INVALID_VALUE_FOR_PATTERN_ERROR";
903
910
  })(ErrorType || (ErrorType = {}));
904
911
  (function(Format6) {
905
912
  Format6["UNDEFINED"] = "UNDEFINED";
@@ -1712,6 +1719,267 @@ var init_errors = __esm({
1712
1719
  }
1713
1720
  });
1714
1721
 
1722
+ // ../form-validator/dist/esm/lib/error-derivation/types.js
1723
+ var DateFieldErrorSuffix, DateTimeFieldErrorSuffix;
1724
+ var init_types = __esm({
1725
+ "../form-validator/dist/esm/lib/error-derivation/types.js"() {
1726
+ (function(DateFieldErrorSuffix2) {
1727
+ DateFieldErrorSuffix2["YEAR_MONTH"] = "year-month";
1728
+ DateFieldErrorSuffix2["YEAR_DAY"] = "year-day";
1729
+ DateFieldErrorSuffix2["MONTH_DAY"] = "month-day";
1730
+ DateFieldErrorSuffix2["YEAR"] = "year";
1731
+ DateFieldErrorSuffix2["MONTH"] = "month";
1732
+ DateFieldErrorSuffix2["DAY"] = "day";
1733
+ })(DateFieldErrorSuffix || (DateFieldErrorSuffix = {}));
1734
+ (function(DateTimeFieldErrorSuffix2) {
1735
+ DateTimeFieldErrorSuffix2["YEAR_MONTH_TIME"] = "year-month-time";
1736
+ DateTimeFieldErrorSuffix2["YEAR_DAY_TIME"] = "year-day-time";
1737
+ DateTimeFieldErrorSuffix2["MONTH_DAY_TIME"] = "month-day-time";
1738
+ DateTimeFieldErrorSuffix2["YEAR_TIME"] = "year-time";
1739
+ DateTimeFieldErrorSuffix2["MONTH_TIME"] = "month-time";
1740
+ DateTimeFieldErrorSuffix2["DAY_TIME"] = "day-time";
1741
+ DateTimeFieldErrorSuffix2["TIME"] = "time";
1742
+ })(DateTimeFieldErrorSuffix || (DateTimeFieldErrorSuffix = {}));
1743
+ }
1744
+ });
1745
+
1746
+ // ../form-validator/dist/esm/lib/error-derivation/helpers.js
1747
+ function getDecimalPlacesCount(multipleOf) {
1748
+ return String(multipleOf).split(".")?.[1]?.length || 0;
1749
+ }
1750
+ function deserializeDate(value) {
1751
+ if (!value) {
1752
+ return {};
1753
+ }
1754
+ const parts = value.split("-");
1755
+ if (parts.length === 3) {
1756
+ const [year, month, day] = parts;
1757
+ return {
1758
+ YEAR: year || void 0,
1759
+ MONTH: month || void 0,
1760
+ DAY: day || void 0
1761
+ };
1762
+ }
1763
+ return {};
1764
+ }
1765
+ function parseDateTimeValue(value) {
1766
+ if (!value) {
1767
+ return ["", ""];
1768
+ }
1769
+ const separator = value.indexOf("T") > -1 ? "T" : " ";
1770
+ const [date, time] = value.split(separator);
1771
+ return [date || "", time || ""];
1772
+ }
1773
+ function isTimeComplete(time) {
1774
+ if (!time) {
1775
+ return false;
1776
+ }
1777
+ const [hours, minutes] = time.split(":");
1778
+ return !!hours && !!minutes;
1779
+ }
1780
+ var init_helpers = __esm({
1781
+ "../form-validator/dist/esm/lib/error-derivation/helpers.js"() {
1782
+ }
1783
+ });
1784
+
1785
+ // ../form-validator/dist/esm/lib/error-derivation/error-derivers.js
1786
+ function deriveError(validation, error, options) {
1787
+ if (!error) {
1788
+ return error;
1789
+ }
1790
+ return derivePatternError(error) ?? deriveCharacterLengthError(validation, error) ?? deriveItemsCountError(validation, error) ?? deriveNumericValueError(validation, error) ?? deriveDateFieldError(validation, error, options?.value) ?? deriveDateTimeFieldError(validation, error, options?.value) ?? error;
1791
+ }
1792
+ function derivePatternError(error) {
1793
+ if (!error || error.errorType !== ErrorType.PATTERN_ERROR) {
1794
+ return void 0;
1795
+ }
1796
+ return {
1797
+ ...error,
1798
+ errorType: ErrorType.INVALID_VALUE_FOR_PATTERN_ERROR
1799
+ };
1800
+ }
1801
+ function deriveCharacterLengthError(validation, error) {
1802
+ if (!error) {
1803
+ return void 0;
1804
+ }
1805
+ const isMinMaxLengthError = error.errorType === ErrorType.MIN_LENGTH_ERROR || error.errorType === ErrorType.MAX_LENGTH_ERROR;
1806
+ if (!isMinMaxLengthError) {
1807
+ return void 0;
1808
+ }
1809
+ const minLength = validation?.string?.minLength;
1810
+ const maxLength = validation?.string?.maxLength;
1811
+ const hasMinMax = minLength && maxLength;
1812
+ if (hasMinMax && minLength === maxLength) {
1813
+ return {
1814
+ ...error,
1815
+ errorType: ErrorType.EXACT_CHARACTER_LENGTH_ERROR,
1816
+ errorMessage: `must have exactly ${minLength} characters`
1817
+ };
1818
+ }
1819
+ if (hasMinMax && minLength !== maxLength) {
1820
+ return {
1821
+ ...error,
1822
+ errorType: ErrorType.CHARACTER_LENGTH_RANGE_ERROR,
1823
+ errorMessage: `must have between ${minLength} and ${maxLength} characters`,
1824
+ params: {
1825
+ minLimit: minLength,
1826
+ maxLimit: maxLength
1827
+ }
1828
+ };
1829
+ }
1830
+ return void 0;
1831
+ }
1832
+ function deriveItemsCountError(validation, error) {
1833
+ if (!error) {
1834
+ return void 0;
1835
+ }
1836
+ const isMinMaxItemsError = error.errorType === ErrorType.MIN_ITEMS_ERROR || error.errorType === ErrorType.MAX_ITEMS_ERROR;
1837
+ if (!isMinMaxItemsError) {
1838
+ return void 0;
1839
+ }
1840
+ const minItems = validation?.array?.minItems;
1841
+ const maxItems = validation?.array?.maxItems;
1842
+ const hasMinMax = minItems && maxItems;
1843
+ if (hasMinMax && minItems === maxItems) {
1844
+ return {
1845
+ ...error,
1846
+ errorType: ErrorType.EXACT_ITEMS_NUMBER_ERROR,
1847
+ errorMessage: `must choose ${minItems} options`
1848
+ };
1849
+ }
1850
+ return void 0;
1851
+ }
1852
+ function deriveNumericValueError(validation, error) {
1853
+ if (!error) {
1854
+ return void 0;
1855
+ }
1856
+ const isMinMaxValueError = error.errorType === ErrorType.MIN_VALUE_ERROR || error.errorType === ErrorType.MAX_VALUE_ERROR;
1857
+ const isMultipleOfValueError = error.errorType === ErrorType.MULTIPLE_OF_VALUE_ERROR;
1858
+ if (isMinMaxValueError) {
1859
+ const { minPrice, maxPrice } = validation?.predefined?.paymentOptions?.products?.[0]?.dynamicPriceOptions ?? {};
1860
+ const minimum = validation?.number?.minimum ?? minPrice;
1861
+ const maximum = validation?.number?.maximum ?? maxPrice;
1862
+ const hasMinMax = minimum !== void 0 && maximum !== void 0;
1863
+ if (hasMinMax && minimum !== maximum) {
1864
+ return {
1865
+ ...error,
1866
+ errorType: ErrorType.VALUE_RANGE_ERROR,
1867
+ errorMessage: `must be from ${minimum} to ${maximum}`,
1868
+ params: {
1869
+ minLimit: minimum,
1870
+ maxLimit: maximum
1871
+ }
1872
+ };
1873
+ }
1874
+ }
1875
+ if (isMultipleOfValueError) {
1876
+ const multipleOf = validation?.number?.multipleOf;
1877
+ if (multipleOf) {
1878
+ const decimalPlaces = getDecimalPlacesCount(multipleOf);
1879
+ return {
1880
+ ...error,
1881
+ errorType: ErrorType.DECIMAL_POINT_ERROR,
1882
+ errorMessage: `must have ${decimalPlaces} number(s) after the decimal point`,
1883
+ params: {
1884
+ number: decimalPlaces
1885
+ }
1886
+ };
1887
+ }
1888
+ }
1889
+ return void 0;
1890
+ }
1891
+ function deriveDateFieldError(_validation, error, value) {
1892
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1893
+ if (!error || !isFormatError) {
1894
+ return void 0;
1895
+ }
1896
+ const { YEAR: _YEAR, MONTH, DAY } = deserializeDate(value);
1897
+ const YEAR = _YEAR?.length === 4;
1898
+ if (!YEAR && !MONTH && !DAY) {
1899
+ return void 0;
1900
+ }
1901
+ const withErrorSuffix = (suffix) => ({
1902
+ ...error,
1903
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1904
+ params: {
1905
+ suffix
1906
+ }
1907
+ });
1908
+ if (!YEAR && !MONTH) {
1909
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_MONTH);
1910
+ } else if (!YEAR && !DAY) {
1911
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_DAY);
1912
+ } else if (!MONTH && !DAY) {
1913
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH_DAY);
1914
+ } else if (!YEAR) {
1915
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR);
1916
+ } else if (!MONTH) {
1917
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH);
1918
+ } else if (!DAY) {
1919
+ return withErrorSuffix(DateFieldErrorSuffix.DAY);
1920
+ } else {
1921
+ return void 0;
1922
+ }
1923
+ }
1924
+ function deriveDateTimeFieldError(validation, error, value) {
1925
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1926
+ if (!error || !isFormatError) {
1927
+ return void 0;
1928
+ }
1929
+ const [date, time] = parseDateTimeValue(value);
1930
+ const dateValidationError = deriveDateFieldError(validation, error, date);
1931
+ const { YEAR, MONTH, DAY } = deserializeDate(date);
1932
+ const timeComplete = isTimeComplete(time);
1933
+ if (!timeComplete && YEAR?.length !== 4 && !MONTH && !DAY) {
1934
+ return void 0;
1935
+ }
1936
+ if (!timeComplete) {
1937
+ const withErrorSuffix = (suffix) => ({
1938
+ ...error,
1939
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1940
+ params: {
1941
+ suffix
1942
+ }
1943
+ });
1944
+ const isDateFieldError = (validationError) => {
1945
+ return validationError?.errorType === ErrorType.INCOMPLETE_DATE_ERROR;
1946
+ };
1947
+ if (isDateFieldError(dateValidationError)) {
1948
+ switch (dateValidationError.params.suffix) {
1949
+ case DateFieldErrorSuffix.YEAR_MONTH:
1950
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_MONTH_TIME);
1951
+ case DateFieldErrorSuffix.YEAR_DAY:
1952
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_DAY_TIME);
1953
+ case DateFieldErrorSuffix.MONTH_DAY:
1954
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_DAY_TIME);
1955
+ case DateFieldErrorSuffix.YEAR:
1956
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_TIME);
1957
+ case DateFieldErrorSuffix.MONTH:
1958
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_TIME);
1959
+ case DateFieldErrorSuffix.DAY:
1960
+ return withErrorSuffix(DateTimeFieldErrorSuffix.DAY_TIME);
1961
+ }
1962
+ }
1963
+ return withErrorSuffix(DateTimeFieldErrorSuffix.TIME);
1964
+ }
1965
+ return dateValidationError;
1966
+ }
1967
+ var init_error_derivers = __esm({
1968
+ "../form-validator/dist/esm/lib/error-derivation/error-derivers.js"() {
1969
+ init_errors();
1970
+ init_types();
1971
+ init_helpers();
1972
+ }
1973
+ });
1974
+
1975
+ // ../form-validator/dist/esm/lib/error-derivation/index.js
1976
+ var init_error_derivation = __esm({
1977
+ "../form-validator/dist/esm/lib/error-derivation/index.js"() {
1978
+ init_error_derivers();
1979
+ init_types();
1980
+ }
1981
+ });
1982
+
1715
1983
  // ../../node_modules/ajv/dist/compile/codegen/code.js
1716
1984
  var require_code = __commonJS({
1717
1985
  "../../node_modules/ajv/dist/compile/codegen/code.js"(exports) {
@@ -13308,6 +13576,7 @@ var init_form_validator = __esm({
13308
13576
  "../form-validator/dist/esm/lib/submission-validation/form-validator.js"() {
13309
13577
  init_errors();
13310
13578
  init_types_impl();
13579
+ init_error_derivation();
13311
13580
  init_create_ajv_schema_validator();
13312
13581
  init_form_schema_mapper();
13313
13582
  init_property_schema_mapper();
@@ -13408,10 +13677,11 @@ var init_form_validator = __esm({
13408
13677
  throw new Error(`Missing precompiled schema for form ${formId}`);
13409
13678
  }
13410
13679
  validate2(fieldSubmission);
13411
- return makeValidationErrors(validate2.errors);
13680
+ const baseErrors = makeValidationErrors(validate2.errors);
13681
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13412
13682
  }
13413
13683
  validateSubmissionWithFieldMask(ajv, formId, fieldSubmission, fieldIdMasks) {
13414
- return fieldIdMasks.reduce((acc, mask) => {
13684
+ const baseErrors = fieldIdMasks.reduce((acc, mask) => {
13415
13685
  const validate2 = ajv.getSchema(`${formId}#/properties/${mask}`);
13416
13686
  if (!validate2) {
13417
13687
  acc.push({
@@ -13427,6 +13697,82 @@ var init_form_validator = __esm({
13427
13697
  }
13428
13698
  return acc;
13429
13699
  }, []);
13700
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13701
+ }
13702
+ deriveErrors(formId, errors, submission) {
13703
+ return errors.map((error) => {
13704
+ const validation = this.getFieldValidation(formId, error.errorPath);
13705
+ const value = this.getFieldValue(submission, error.errorPath);
13706
+ return deriveError(validation, error, { value });
13707
+ }).filter((error) => error !== void 0);
13708
+ }
13709
+ getFieldValidation(formId, fieldPath) {
13710
+ const form = this.formSchemas.find((f) => f.id === formId);
13711
+ if (!form) {
13712
+ return void 0;
13713
+ }
13714
+ const pathParts = fieldPath.split("/");
13715
+ const rootFieldId = pathParts[0];
13716
+ const platformisedField = [
13717
+ ...form.formFields ?? [],
13718
+ ...form.deletedFormFields ?? []
13719
+ ].find((f) => {
13720
+ if (f.id === rootFieldId) {
13721
+ return true;
13722
+ }
13723
+ const inputTarget = f.inputOptions?.target;
13724
+ if (inputTarget === rootFieldId) {
13725
+ return true;
13726
+ }
13727
+ return false;
13728
+ });
13729
+ const legacyField = [
13730
+ ...form.fields ?? [],
13731
+ ...form.deletedFields ?? []
13732
+ ].find((f) => {
13733
+ if (f.id === rootFieldId || f.target === rootFieldId) {
13734
+ return true;
13735
+ }
13736
+ return false;
13737
+ });
13738
+ if (platformisedField) {
13739
+ return this.getPlatformisedFieldValidation(platformisedField);
13740
+ } else if (legacyField) {
13741
+ return this.getLegacyFieldValidation(legacyField);
13742
+ } else {
13743
+ return void 0;
13744
+ }
13745
+ }
13746
+ getLegacyFieldValidation(field) {
13747
+ return field.validation;
13748
+ }
13749
+ getPlatformisedFieldValidation(field) {
13750
+ const inputOptions = field.inputOptions;
13751
+ if (!inputOptions) {
13752
+ return void 0;
13753
+ }
13754
+ const validation = {};
13755
+ if (inputOptions.numberOptions?.validation) {
13756
+ validation.number = inputOptions.numberOptions.validation;
13757
+ }
13758
+ if (inputOptions.stringOptions?.validation) {
13759
+ validation.string = inputOptions.stringOptions.validation;
13760
+ }
13761
+ if (inputOptions.arrayOptions?.validation) {
13762
+ validation.array = inputOptions.arrayOptions.validation;
13763
+ }
13764
+ if (Object.keys(validation).length > 0) {
13765
+ return validation;
13766
+ }
13767
+ return void 0;
13768
+ }
13769
+ getFieldValue(submission, fieldPath) {
13770
+ const pathParts = fieldPath.split("/");
13771
+ let value = submission;
13772
+ for (const part of pathParts) {
13773
+ value = value?.[part];
13774
+ }
13775
+ return typeof value === "string" ? value : void 0;
13430
13776
  }
13431
13777
  isFormDisabled(formSchema) {
13432
13778
  const isDeadlinePast = formSchema?.limitationRule?.dateTimeDeadline && new Date(formSchema.limitationRule.dateTimeDeadline).getTime() <= (/* @__PURE__ */ new Date()).getTime();
@@ -13466,7 +13812,7 @@ var init_form_validator = __esm({
13466
13812
  });
13467
13813
 
13468
13814
  // ../form-validator/dist/esm/lib/backward-compatibility/field-utils.js
13469
- function isInputField(field) {
13815
+ function isInputField2(field) {
13470
13816
  if (field.validation) {
13471
13817
  asRequired(field.target, field, "target");
13472
13818
  }
@@ -13488,7 +13834,7 @@ function compareSchemaBackwardCompatibility(newSchema, knownSchema) {
13488
13834
  };
13489
13835
  }
13490
13836
  function getNonCompatibleFields(newSchema, knownSchema) {
13491
- newSchema.fields?.forEach(isInputField);
13837
+ newSchema.fields?.forEach(isInputField2);
13492
13838
  const newFieldsWithTarget = (newSchema.fields ?? []).filter(fieldHasTarget);
13493
13839
  const knownInputFields = [
13494
13840
  ...knownSchema.fields ?? [],
@@ -13847,6 +14193,8 @@ var init_form_schema_validator = __esm({
13847
14193
  var esm_exports = {};
13848
14194
  __export(esm_exports, {
13849
14195
  CALLING_COUNTRY_CODES: () => CALLING_COUNTRY_CODES,
14196
+ DateFieldErrorSuffix: () => DateFieldErrorSuffix,
14197
+ DateTimeFieldErrorSuffix: () => DateTimeFieldErrorSuffix,
13850
14198
  ErrorTypes: () => ErrorType,
13851
14199
  FILE_UPLOAD_FIELD_TYPE: () => FILE_UPLOAD_FIELD_TYPE,
13852
14200
  FormSchemaValidator: () => FormSchemaValidator,
@@ -13864,6 +14212,7 @@ var init_esm = __esm({
13864
14212
  init_form_schema_validator();
13865
14213
  init_errors();
13866
14214
  init_calling_country_codes();
14215
+ init_error_derivation();
13867
14216
  }
13868
14217
  });
13869
14218
 
@@ -15814,7 +16163,7 @@ function useFormFields() {
15814
16163
  var import_extends5 = __toESM(require_extends());
15815
16164
 
15816
16165
  // ../form-fields/dist/esm/ui/form/types/form-view.js
15817
- function isInputField2(field) {
16166
+ function isInputField(field) {
15818
16167
  return Boolean(field.target);
15819
16168
  }
15820
16169
  function isNestedFormField(field) {
@@ -16137,7 +16486,7 @@ var FormField = (_ref) => {
16137
16486
  FieldLayout
16138
16487
  }));
16139
16488
  }
16140
- if (isInputField2(field)) {
16489
+ if (isInputField(field)) {
16141
16490
  return /* @__PURE__ */ React14.createElement(InputField, (0, import_extends5.default)({}, rest, {
16142
16491
  field,
16143
16492
  FieldLayout
@@ -16229,7 +16578,7 @@ var DefaultFieldLayout = (_ref) => {
16229
16578
  layout,
16230
16579
  fieldType
16231
16580
  } = fieldView;
16232
- const dataHook = `form-field-${isInputField2(fieldView) ? fieldView.target : fieldView.id}`;
16581
+ const dataHook = `form-field-${isInputField(fieldView) ? fieldView.target : fieldView.id}`;
16233
16582
  const dataAttributes = {
16234
16583
  "data-hook": dataHook,
16235
16584
  "data-field-type": fieldType