@bolttech/form-engine-core 0.0.1-beta.24 → 0.0.1-beta.26

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/index.esm.js CHANGED
@@ -1933,7 +1933,7 @@ const conditions = (value, validations) => {
1933
1933
  * Validates if a date string matches a specific date format.
1934
1934
  * The function accepts strings with '/' or '-' separators and removes them before validating the format.
1935
1935
  *
1936
- * @param {string} string - The date string to be validated. It can contain '/' or '-' separators.
1936
+ * @param {string} value - The date string to be validated. It can contain '/' or '-' separators.
1937
1937
  * @param {string} format - The expected date format. It can be one of the following:
1938
1938
  * 'DDMMYYYY', 'YYYYMMDD', 'YYYYDDMM', 'MMDDYYYY',
1939
1939
  * 'DMYYYY', 'YYYYMD', 'YYYYDM', 'MDYYYY'.
@@ -2090,25 +2090,23 @@ const betweenDates = (value, validations) => {
2090
2090
  * ```
2091
2091
  */
2092
2092
  const date = (value, validations) => {
2093
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
2093
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
2094
2094
  if (!((_b = (_a = validations.date) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.value) && !((_d = (_c = validations.date) === null || _c === void 0 ? void 0 : _c.origin) === null || _d === void 0 ? void 0 : _d.intervals)) {
2095
2095
  return false;
2096
2096
  }
2097
2097
  const originValue = validations.date.origin.value || value;
2098
2098
  const mappedValue = dateRearrangeMapper[(_e = validations.date) === null || _e === void 0 ? void 0 : _e.origin.format](originValue).toString();
2099
- console.log('date (invalidStringDate): ', value, invalidStringDate(mappedValue, (_f = validations.date) === null || _f === void 0 ? void 0 : _f.origin.format));
2100
- if (invalidStringDate(mappedValue, (_g = validations.date) === null || _g === void 0 ? void 0 : _g.origin.format)) return false;
2101
2099
  let originDate = new Date(mappedValue);
2102
2100
  let targetDate = new Date();
2103
2101
  let target = new Date();
2104
- if ((_j = (_h = validations.date) === null || _h === void 0 ? void 0 : _h.target) === null || _j === void 0 ? void 0 : _j.format) {
2105
- target = new Date(dateRearrangeMapper[(_l = (_k = validations.date) === null || _k === void 0 ? void 0 : _k.target) === null || _l === void 0 ? void 0 : _l.format](validations.date.target.value).toString());
2102
+ if ((_g = (_f = validations.date) === null || _f === void 0 ? void 0 : _f.target) === null || _g === void 0 ? void 0 : _g.format) {
2103
+ target = new Date(dateRearrangeMapper[(_j = (_h = validations.date) === null || _h === void 0 ? void 0 : _h.target) === null || _j === void 0 ? void 0 : _j.format](validations.date.target.value).toString());
2106
2104
  targetDate = target;
2107
2105
  }
2108
2106
  if (validations.date.origin.intervals) {
2109
2107
  targetDate = getIntervalsDate(originDate, validations.date.origin.intervals);
2110
2108
  const date = new Date();
2111
- if (((_m = validations.date.target) === null || _m === void 0 ? void 0 : _m.value) && target) {
2109
+ if (((_k = validations.date.target) === null || _k === void 0 ? void 0 : _k.value) && target) {
2112
2110
  date.setDate(target.getDate());
2113
2111
  date.setMonth(target.getMonth());
2114
2112
  }
@@ -2117,6 +2115,9 @@ const date = (value, validations) => {
2117
2115
  if (validations.date.onlyValidDate && (!(targetDate instanceof Date && isFinite(targetDate)) || !(targetDate instanceof Date && isFinite(originDate)) || originValue.length < 8)) {
2118
2116
  return true;
2119
2117
  }
2118
+ if (invalidStringDate(mappedValue, (_l = validations.date) === null || _l === void 0 ? void 0 : _l.origin.format)) {
2119
+ return false;
2120
+ }
2120
2121
  const originTimestamp = originDate.getTime();
2121
2122
  const targetTimestamp = targetDate.getTime();
2122
2123
  const operationsMapper = {
@@ -2127,11 +2128,7 @@ const date = (value, validations) => {
2127
2128
  '===': originTimestamp === targetTimestamp,
2128
2129
  '!==': originTimestamp !== targetTimestamp
2129
2130
  };
2130
- console.log('date (operationsMapper): ', {
2131
- originDate,
2132
- targetDate
2133
- }, operationsMapper[(_o = validations.date) === null || _o === void 0 ? void 0 : _o.operator]);
2134
- return operationsMapper[(_p = validations.date) === null || _p === void 0 ? void 0 : _p.operator];
2131
+ return operationsMapper[(_m = validations.date) === null || _m === void 0 ? void 0 : _m.operator];
2135
2132
  };
2136
2133
  /**
2137
2134
  * @function validDate
@@ -2160,12 +2157,8 @@ const validDate = (value, validations) => {
2160
2157
  const month = parseInt(dateParts[0], 10) - 1; // Month is zero-based
2161
2158
  const day = parseInt(dateParts[1], 10);
2162
2159
  const date = new Date(year, month, day);
2163
- /* This line is here because we need to guarantee that the field will not be valid while typing,
2164
- * that is, imagine the scenario where the user wants to type 1995, when typing 19 or 199,
2165
- * the function returns valid, as there is a year 19 or 199.
2166
- */
2167
2160
  if (date.getFullYear() < 1900) {
2168
- return true;
2161
+ return false;
2169
2162
  }
2170
2163
  // Check if the date is valid
2171
2164
  const isValidDate = date.getFullYear() === year && date.getMonth() === month && date.getDate() === day;
@@ -3326,6 +3319,18 @@ class FormCore {
3326
3319
  }
3327
3320
  });
3328
3321
  }
3322
+ setFieldVisibility(field, hasError, showOnlyIfTrue) {
3323
+ console.log('setFieldVisibility:', {
3324
+ field,
3325
+ hasError,
3326
+ showOnlyIfTrue
3327
+ });
3328
+ if (!this.fields.has(field)) {
3329
+ console.warn(`failed to update visibility onto field ${field}`);
3330
+ } else {
3331
+ this.fields.get(field).visibility = showOnlyIfTrue ? hasError : !hasError;
3332
+ }
3333
+ }
3329
3334
  /**
3330
3335
  * Validates visibility conditions for a given event and updates field visibility accordingly.
3331
3336
  *
@@ -3339,17 +3344,25 @@ class FormCore {
3339
3344
  }) {
3340
3345
  const field = this.fields.get(key);
3341
3346
  const structVisibility = field === null || field === void 0 ? void 0 : field.visibilityConditions;
3342
- if (!structVisibility || !(structVisibility === null || structVisibility === void 0 ? void 0 : structVisibility.some(config => config.events.includes(event)))) return;
3347
+ if (!structVisibility || !(structVisibility === null || structVisibility === void 0 ? void 0 : structVisibility.some(config => config.events.includes(event)))) {
3348
+ return;
3349
+ }
3343
3350
  structVisibility.forEach(structElement => {
3344
3351
  if (!structElement.events.includes(event)) return;
3345
3352
  Object.keys(structElement.validations).forEach(validationKey => {
3346
3353
  const error = handleValidation(field.value, structElement.validations, validations, validationKey);
3354
+ console.log('validateVisibility: ', {
3355
+ value: field === null || field === void 0 ? void 0 : field.value,
3356
+ validationKey,
3357
+ error,
3358
+ showCondition: !!(field.value && structElement.showOnlyIfTrue)
3359
+ });
3347
3360
  if (Array.isArray(structElement.fields)) {
3348
3361
  structElement.fields.forEach(fieldKey => {
3349
- if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = structElement.showOnlyIfTrue ? error : !error;
3362
+ this.setFieldVisibility(fieldKey, error, !!(field.value && structElement.showOnlyIfTrue));
3350
3363
  });
3351
3364
  } else if (structElement.fields) {
3352
- if (!this.fields.has(structElement.fields)) console.warn(`failed to update visibility onto field ${structElement.fields}`);else this.fields.get(structElement.fields).visibility = structElement.showOnlyIfTrue ? error : !error;
3365
+ this.setFieldVisibility(structElement.fields, error, !!(field.value && structElement.showOnlyIfTrue));
3353
3366
  }
3354
3367
  });
3355
3368
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine-core",
3
- "version": "0.0.1-beta.24",
3
+ "version": "0.0.1-beta.26",
4
4
  "module": "./index.esm.js",
5
5
  "type": "module",
6
6
  "main": "./index.esm.js",
@@ -139,6 +139,7 @@ declare class FormCore {
139
139
  * @private
140
140
  */
141
141
  private static checkIndexes;
142
+ private setFieldVisibility;
142
143
  /**
143
144
  * Validates visibility conditions for a given event and updates field visibility accordingly.
144
145
  *