@bolttech/form-engine-core 0.0.1-beta.42 → 0.0.1-beta.44

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
@@ -140,45 +140,31 @@ function extractFieldKeys(expression) {
140
140
  * // expressions will contain an array of objects with origin expressions, field keys, and destination paths.
141
141
  * ```
142
142
  */
143
- function traverseObject(obj, path) {
143
+ function traverseObject(element, path) {
144
144
  const result = [];
145
- for (const key in obj) {
146
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
147
- const value = obj[key];
148
- if (Array.isArray(value)) {
149
- value.forEach((item, index) => {
150
- if (typeof item === 'object') {
151
- result.push(...traverseObject(item, `${path ? `${path}.` : ``}${key}.${index}`));
152
- } else if (typeof item === 'string') {
153
- if (String(item).includes('${')) {
154
- // const extractedPath = item.replace(/\$|{|}/g, '').split('.');
155
- const extractedOriginPath = `${path ? `${path}.` : ``}${key}`.split('.');
156
- result.push(Object.assign(Object.assign({
157
- originExpression: item
158
- }, extractFieldKeys(item)), {
159
- destinationKey: extractedOriginPath[0],
160
- destinationProperty: extractedOriginPath[1],
161
- destinationPath: extractedOriginPath.slice(2)
162
- }));
163
- }
164
- }
165
- });
166
- } else if (typeof value === 'object') {
145
+ if (Array.isArray(element)) {
146
+ element.forEach((item, index) => {
147
+ result.push(...traverseObject(item, `${path ? `${path}.` : ``}${index}`));
148
+ });
149
+ } else if (typeof element === 'object') {
150
+ for (const key in element) {
151
+ if (Object.prototype.hasOwnProperty.call(element, key)) {
152
+ const value = element[key];
167
153
  result.push(...traverseObject(value, `${path ? `${path}.` : ``}${key}`));
168
- } else if (typeof value === 'string') {
169
- if (value.includes('${')) {
170
- // const extractedPath = value.replace(/\$|{|}/g, '').split('.');
171
- const destinationPath = `${path ? `${path}.` : ``}${key}`.split('.');
172
- result.push(Object.assign(Object.assign({
173
- originExpression: value
174
- }, extractFieldKeys(value)), {
175
- destinationKey: destinationPath[0],
176
- destinationProperty: destinationPath[1],
177
- destinationPath: destinationPath.slice(2)
178
- }));
179
- }
180
154
  }
181
155
  }
156
+ } else if (typeof element === 'string') {
157
+ if (element.includes('${')) {
158
+ // const extractedPath = value.replace(/\$|{|}/g, '').split('.');
159
+ const destinationPath = (path ? path : '').split('.');
160
+ result.push(Object.assign(Object.assign({
161
+ originExpression: element
162
+ }, extractFieldKeys(element)), {
163
+ destinationKey: destinationPath[0],
164
+ destinationProperty: destinationPath[1],
165
+ destinationPath: destinationPath.slice(2)
166
+ }));
167
+ }
182
168
  }
183
169
  return result;
184
170
  }
@@ -1236,6 +1222,44 @@ const between = (value, validations) => {
1236
1222
  const num = Number(value);
1237
1223
  return !Number.isNaN(num) && !(+num >= validations.between.start && +num <= validations.between.end);
1238
1224
  };
1225
+ /**
1226
+ * Checks if a given value is less than a specified threshold.
1227
+ *
1228
+ * @param value - The value to be checked. This value will be converted to a number.
1229
+ * @param validations - An object containing validation methods, specifically the `lessThan` property which holds the threshold value.
1230
+ * @returns Returns `false` if the `lessThan` threshold is not provided or if the value is not a valid number. Otherwise, returns `true` if the value is greater than or equal to the `lessThan` threshold.
1231
+ *
1232
+ * @example
1233
+ * ```typescript
1234
+ * const validations = { lessThan: 10 };
1235
+ * console.log(lessThan(5, validations)); // true
1236
+ * console.log(lessThan(15, validations)); // false
1237
+ * ```
1238
+ */
1239
+ const lessThan = (value, validations) => {
1240
+ const number = Number(value);
1241
+ if (!validations.lessThan || Number.isNaN(number)) return false;
1242
+ return number >= validations.lessThan;
1243
+ };
1244
+ /**
1245
+ * Checks if a given value is greater than a specified threshold.
1246
+ *
1247
+ * @param value - The value to be checked. This value will be converted to a number.
1248
+ * @param validations - An object containing validation methods, specifically the `greaterThan` property which holds the threshold value.
1249
+ * @returns Returns `false` if the `greaterThan` threshold is not provided or if the value is not a valid number. Otherwise, returns `true` if the value is less than or equal to the `greaterThan` threshold.
1250
+ *
1251
+ * @example
1252
+ * ```typescript
1253
+ * const validations = { greaterThan: 10 };
1254
+ * console.log(greaterThan(15, validations)); // true
1255
+ * console.log(greaterThan(5, validations)); // false
1256
+ * ```
1257
+ */
1258
+ const greaterThan = (value, validations) => {
1259
+ const number = Number(value);
1260
+ if (!validations.greaterThan || Number.isNaN(number)) return false;
1261
+ return number <= validations.greaterThan;
1262
+ };
1239
1263
  /**
1240
1264
  * Validates if a value contains sequential numbers.
1241
1265
  *
@@ -2341,6 +2365,8 @@ const multipleValidations = (value, methods) => {
2341
2365
  const validations = {
2342
2366
  max,
2343
2367
  min,
2368
+ lessThan,
2369
+ greaterThan,
2344
2370
  length,
2345
2371
  regex,
2346
2372
  url,
@@ -2887,6 +2913,17 @@ class FormField {
2887
2913
  }) {
2888
2914
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2889
2915
  return __awaiter(this, void 0, void 0, function* () {
2916
+ const configRequest = config => __awaiter(this, void 0, void 0, function* () {
2917
+ try {
2918
+ const responseData = yield makeRequest(config.method, config.url, config.headers, config.body);
2919
+ const apiResponseData = JSON.parse(String(responseData));
2920
+ const response = config.resultPath ? get(apiResponseData, config.resultPath) : apiResponseData;
2921
+ // this.apiResponseData = { response };
2922
+ return response;
2923
+ } catch (e) {
2924
+ return !isNil(config === null || config === void 0 ? void 0 : config.fallbackValue) ? config.fallbackValue : 'error';
2925
+ }
2926
+ });
2890
2927
  if (!((_b = (_a = this.apiSchema) === null || _a === void 0 ? void 0 : _a.defaultConfig) === null || _b === void 0 ? void 0 : _b.events.includes(event)) && !(((_c = this.apiSchema) === null || _c === void 0 ? void 0 : _c.configs) && Object.keys((_d = this.apiSchema) === null || _d === void 0 ? void 0 : _d.configs).some(key => {
2891
2928
  var _a, _b;
2892
2929
  return (_b = (_a = this.apiSchema) === null || _a === void 0 ? void 0 : _a.configs) === null || _b === void 0 ? void 0 : _b[key].events.includes(event);
@@ -2897,19 +2934,10 @@ class FormField {
2897
2934
  };
2898
2935
  const config = (_e = this.apiSchema.defaultConfig) === null || _e === void 0 ? void 0 : _e.config;
2899
2936
  if (config && ((_g = (_f = this.apiSchema) === null || _f === void 0 ? void 0 : _f.defaultConfig) === null || _g === void 0 ? void 0 : _g.events.includes(event)) && this.checkApiRequestValidations(config)) {
2900
- try {
2901
- const responseData = yield makeRequest(config.method, config.url, config.headers, config.body);
2902
- const apiResponseData = JSON.parse(String(responseData));
2903
- const response = config.resultPath ? get(apiResponseData, config.resultPath) : apiResponseData;
2904
- // this.apiResponseData = { response };
2905
- responses.default = {
2906
- response
2907
- };
2908
- } catch (e) {
2909
- responses.default = {
2910
- response: !isNil(config === null || config === void 0 ? void 0 : config.fallbackValue) ? config.fallbackValue : 'error'
2911
- };
2912
- }
2937
+ const response = yield configRequest(config);
2938
+ responses.default = {
2939
+ response
2940
+ };
2913
2941
  }
2914
2942
  if (((_h = this.apiSchema) === null || _h === void 0 ? void 0 : _h.configs) && Object.keys((_j = this.apiSchema) === null || _j === void 0 ? void 0 : _j.configs).some(key => {
2915
2943
  var _a, _b;
@@ -2920,38 +2948,21 @@ class FormField {
2920
2948
  @TODO handle promises with error
2921
2949
  */
2922
2950
  const result = yield Promise.all(Object.keys(this.apiSchema.configs).map(configKey => __awaiter(this, void 0, void 0, function* () {
2923
- return new Promise(res => {
2924
- var _a, _b, _c, _d;
2925
- const config = (_b = (_a = this.apiSchema) === null || _a === void 0 ? void 0 : _a.configs) === null || _b === void 0 ? void 0 : _b[configKey].config;
2926
- if (config && ((_d = (_c = this.apiSchema) === null || _c === void 0 ? void 0 : _c.configs) === null || _d === void 0 ? void 0 : _d[configKey].events.includes(event)) && this.checkApiRequestValidations(config)) {
2927
- try {
2928
- makeRequest(config.method, config.url, config.headers, config.body).then(responseData => {
2929
- const apiResponseData = JSON.parse(String(responseData));
2930
- const response = get(apiResponseData, config.resultPath || '');
2931
- res({
2932
- name: configKey,
2933
- result: {
2934
- response
2935
- }
2936
- });
2937
- // responses.named![configKey] = { response };
2938
- });
2939
- } catch (e) {
2940
- res({
2941
- name: configKey,
2942
- result: {
2943
- response: !isNil(config.fallbackValue) ? config.fallbackValue : 'error'
2944
- }
2945
- });
2951
+ var _k, _l, _m, _o;
2952
+ const config = (_l = (_k = this.apiSchema) === null || _k === void 0 ? void 0 : _k.configs) === null || _l === void 0 ? void 0 : _l[configKey].config;
2953
+ if (config && ((_o = (_m = this.apiSchema) === null || _m === void 0 ? void 0 : _m.configs) === null || _o === void 0 ? void 0 : _o[configKey].events.includes(event)) && this.checkApiRequestValidations(config)) {
2954
+ const response = yield configRequest(config);
2955
+ return {
2956
+ name: configKey,
2957
+ result: {
2958
+ response
2946
2959
  }
2947
- }
2948
- });
2960
+ };
2961
+ }
2962
+ return null;
2949
2963
  })));
2950
- result.forEach(({
2951
- name,
2952
- result
2953
- }) => {
2954
- if (responses.named) responses.named[name] = result;
2964
+ result.forEach(payload => {
2965
+ if (payload && responses.named) responses.named[payload.name] = payload.result;
2955
2966
  });
2956
2967
  }
2957
2968
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine-core",
3
- "version": "0.0.1-beta.42",
3
+ "version": "0.0.1-beta.44",
4
4
  "module": "./index.esm.js",
5
5
  "type": "module",
6
6
  "main": "./index.esm.js",
@@ -60,5 +60,5 @@ declare function extractFieldKeys(expression: string): {
60
60
  * // expressions will contain an array of objects with origin expressions, field keys, and destination paths.
61
61
  * ```
62
62
  */
63
- declare function traverseObject(obj: any, path?: string): TSubscribedTemplates[];
63
+ declare function traverseObject(element: any, path?: string): TSubscribedTemplates[];
64
64
  export { makeRequest, traverseObject, extractFieldKeys };
@@ -311,6 +311,8 @@ type TDateValidation = {
311
311
  *
312
312
  * @property {number} [max] - Maximum value or length.
313
313
  * @property {number} [min] - Minimum value or length.
314
+ * @property {number} [lessThan] - Minimum value or length.
315
+ * @property {number} [greaterThan] - Minimum value or length.
314
316
  * @property {TLengthValidation} [length] - Length validation rule.
315
317
  * @property {boolean} [required] - Indicates if the field is required.
316
318
  * @property {unknown} [value] - Specific value to match.
@@ -344,6 +346,8 @@ type TDateValidation = {
344
346
  * const validationMethods: TValidationMethods = {
345
347
  * max: 100,
346
348
  * min: 1,
349
+ * lessThan: 1995,
350
+ * greaterThan: 82000,
347
351
  * length: { rule: 'greaterOrEqual', target: 8 },
348
352
  * required: true,
349
353
  * regex: '^[a-zA-Z0-9]+$',
@@ -394,6 +398,8 @@ type TDateValidation = {
394
398
  type TValidationMethods = {
395
399
  max?: number;
396
400
  min?: number;
401
+ lessThan?: number;
402
+ greaterThan?: number;
397
403
  length?: TLengthValidation;
398
404
  required?: boolean;
399
405
  value?: unknown;
@@ -80,6 +80,36 @@ export declare const min: (value: unknown, validations: TValidationMethods) => b
80
80
  * ```
81
81
  */
82
82
  export declare const between: (value: unknown, validations: TValidationMethods) => boolean;
83
+ /**
84
+ * Checks if a given value is less than a specified threshold.
85
+ *
86
+ * @param value - The value to be checked. This value will be converted to a number.
87
+ * @param validations - An object containing validation methods, specifically the `lessThan` property which holds the threshold value.
88
+ * @returns Returns `false` if the `lessThan` threshold is not provided or if the value is not a valid number. Otherwise, returns `true` if the value is greater than or equal to the `lessThan` threshold.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const validations = { lessThan: 10 };
93
+ * console.log(lessThan(5, validations)); // true
94
+ * console.log(lessThan(15, validations)); // false
95
+ * ```
96
+ */
97
+ export declare const lessThan: (value: unknown, validations: TValidationMethods) => boolean;
98
+ /**
99
+ * Checks if a given value is greater than a specified threshold.
100
+ *
101
+ * @param value - The value to be checked. This value will be converted to a number.
102
+ * @param validations - An object containing validation methods, specifically the `greaterThan` property which holds the threshold value.
103
+ * @returns Returns `false` if the `greaterThan` threshold is not provided or if the value is not a valid number. Otherwise, returns `true` if the value is less than or equal to the `greaterThan` threshold.
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const validations = { greaterThan: 10 };
108
+ * console.log(greaterThan(15, validations)); // true
109
+ * console.log(greaterThan(5, validations)); // false
110
+ * ```
111
+ */
112
+ export declare const greaterThan: (value: unknown, validations: TValidationMethods) => boolean;
83
113
  /**
84
114
  * Validates if a value contains sequential numbers.
85
115
  *