@bolttech/form-engine-core 0.0.1-beta.19 → 0.0.1-beta.20
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 +79 -36
- package/package.json +1 -1
- package/src/types/schema.d.ts +6 -1
- package/src/validations/date.d.ts +1 -0
package/index.esm.js
CHANGED
|
@@ -1929,41 +1929,44 @@ const conditions = (value, validations) => {
|
|
|
1929
1929
|
};
|
|
1930
1930
|
|
|
1931
1931
|
/**
|
|
1932
|
-
*
|
|
1932
|
+
* @internal
|
|
1933
|
+
* Validates if a date string matches a specific date format.
|
|
1934
|
+
* The function accepts strings with '/' or '-' separators and removes them before validating the format.
|
|
1933
1935
|
*
|
|
1934
|
-
* @param {string}
|
|
1935
|
-
* @param {
|
|
1936
|
-
*
|
|
1936
|
+
* @param {string} string - The date string to be validated. It can contain '/' or '-' separators.
|
|
1937
|
+
* @param {string} format - The expected date format. It can be one of the following:
|
|
1938
|
+
* 'DDMMYYYY', 'YYYYMMDD', 'YYYYDDMM', 'MMDDYYYY',
|
|
1939
|
+
* 'DMYYYY', 'YYYYMD', 'YYYYDM', 'MDYYYY'.
|
|
1940
|
+
* @returns {boolean} - Returns `false` if the date string matches the specified format, otherwise `true`.
|
|
1937
1941
|
*
|
|
1938
1942
|
* @example
|
|
1939
|
-
*
|
|
1940
|
-
*
|
|
1941
|
-
* betweenDates: [
|
|
1942
|
-
* { origin: { value: '2023-01-01', format: 'yyyy-MM-dd' }, operator: '>=' },
|
|
1943
|
-
* { origin: { value: '2023-12-31', format: 'yyyy-MM-dd' }, operator: '<=' }
|
|
1944
|
-
* ]
|
|
1945
|
-
* };
|
|
1943
|
+
* // Returns false
|
|
1944
|
+
* invalidStringDate("25/07/1997", "DDMMYYYY");
|
|
1946
1945
|
*
|
|
1947
|
-
*
|
|
1948
|
-
*
|
|
1946
|
+
* @example
|
|
1947
|
+
* // Returns false
|
|
1948
|
+
* invalidStringDate("1997-07-25", "YYYYMMDD");
|
|
1949
1949
|
*
|
|
1950
|
-
*
|
|
1951
|
-
*
|
|
1952
|
-
*
|
|
1950
|
+
* @example
|
|
1951
|
+
* // Returns true, as the format does not match
|
|
1952
|
+
* invalidStringDate("1997/25/07", "MMDDYYYY");
|
|
1953
1953
|
*/
|
|
1954
|
-
const
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
if (((_a = validations.betweenDates) === null || _a === void 0 ? void 0 : _a.length) != 2) return false;
|
|
1958
|
-
for (const validation of validations.betweenDates) {
|
|
1959
|
-
if (date(value, {
|
|
1960
|
-
date: validation
|
|
1961
|
-
})) {
|
|
1962
|
-
fail = true;
|
|
1963
|
-
break;
|
|
1964
|
-
}
|
|
1954
|
+
const invalidStringDate = (value, format) => {
|
|
1955
|
+
if (!value.includes('/') && !value.includes('-') || !format) {
|
|
1956
|
+
return true;
|
|
1965
1957
|
}
|
|
1966
|
-
|
|
1958
|
+
const valueParts = value.replace(/[-/]/g, '');
|
|
1959
|
+
const dateMapper = {
|
|
1960
|
+
DDMMYYYY: /^(\d{2})(\d{2})(\d{4})$/,
|
|
1961
|
+
YYYYMMDD: /^(\d{4})(\d{2})(\d{2})$/,
|
|
1962
|
+
YYYYDDMM: /^(\d{4})(\d{2})(\d{2})$/,
|
|
1963
|
+
MMDDYYYY: /^(\d{2})(\d{2})(\d{4})$/,
|
|
1964
|
+
DMYYYY: /^(\d{1,2})(\d{1,2})(\d{4})$/,
|
|
1965
|
+
YYYYMD: /^(\d{4})(\d{1,2})(\d{1,2})$/,
|
|
1966
|
+
YYYYDM: /^(\d{4})(\d{1,2})(\d{1,2})$/,
|
|
1967
|
+
MDYYYY: /^(\d{1,2})(\d{1,2})(\d{4})$/
|
|
1968
|
+
};
|
|
1969
|
+
return !dateMapper[format].test(valueParts);
|
|
1967
1970
|
};
|
|
1968
1971
|
/**
|
|
1969
1972
|
* @internal
|
|
@@ -2016,6 +2019,44 @@ const dateRearrangeMapper = {
|
|
|
2016
2019
|
MMDDYYYY: value => value,
|
|
2017
2020
|
timestamp: value => new Date(value).toString()
|
|
2018
2021
|
};
|
|
2022
|
+
/**
|
|
2023
|
+
* @function betweenDates
|
|
2024
|
+
* Validates if a date value falls between two specified dates.
|
|
2025
|
+
*
|
|
2026
|
+
* @param {string} value - The date value to be validated in string format.
|
|
2027
|
+
* @param {TValidationMethods} validations - The validation methods object containing the betweenDates validation rules.
|
|
2028
|
+
* @returns {boolean} - Returns `true` if the date value fails the betweenDates validation, otherwise `false`.
|
|
2029
|
+
*
|
|
2030
|
+
* @example
|
|
2031
|
+
* ```typescript
|
|
2032
|
+
* const validations = {
|
|
2033
|
+
* betweenDates: [
|
|
2034
|
+
* { origin: { value: '2023-01-01', format: 'yyyy-MM-dd' }, operator: '>=' },
|
|
2035
|
+
* { origin: { value: '2023-12-31', format: 'yyyy-MM-dd' }, operator: '<=' }
|
|
2036
|
+
* ]
|
|
2037
|
+
* };
|
|
2038
|
+
*
|
|
2039
|
+
* const result1 = betweenDates('2023-06-01', validations);
|
|
2040
|
+
* console.log(result1); // false (date is within the range)
|
|
2041
|
+
*
|
|
2042
|
+
* const result2 = betweenDates('2024-01-01', validations);
|
|
2043
|
+
* console.log(result2); // true (date is outside the range)
|
|
2044
|
+
* ```
|
|
2045
|
+
*/
|
|
2046
|
+
const betweenDates = (value, validations) => {
|
|
2047
|
+
var _a;
|
|
2048
|
+
let fail = false;
|
|
2049
|
+
if (((_a = validations.betweenDates) === null || _a === void 0 ? void 0 : _a.length) != 2) return false;
|
|
2050
|
+
for (const validation of validations.betweenDates) {
|
|
2051
|
+
if (date(value, {
|
|
2052
|
+
date: validation
|
|
2053
|
+
})) {
|
|
2054
|
+
fail = true;
|
|
2055
|
+
break;
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
return fail;
|
|
2059
|
+
};
|
|
2019
2060
|
/**
|
|
2020
2061
|
* @function date
|
|
2021
2062
|
* Validates a date value based on various date conditions and intervals.
|
|
@@ -2049,22 +2090,24 @@ const dateRearrangeMapper = {
|
|
|
2049
2090
|
* ```
|
|
2050
2091
|
*/
|
|
2051
2092
|
const date = (value, validations) => {
|
|
2052
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
2093
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
2053
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)) {
|
|
2054
2095
|
return false;
|
|
2055
2096
|
}
|
|
2056
2097
|
const originValue = validations.date.origin.value || value;
|
|
2057
|
-
|
|
2098
|
+
const mappedValue = dateRearrangeMapper[(_e = validations.date) === null || _e === void 0 ? void 0 : _e.origin.format](originValue).toString();
|
|
2099
|
+
if (invalidStringDate(mappedValue, (_f = validations.date) === null || _f === void 0 ? void 0 : _f.origin.format)) return false;
|
|
2100
|
+
let originDate = new Date(mappedValue);
|
|
2058
2101
|
let targetDate = new Date();
|
|
2059
2102
|
let target = new Date();
|
|
2060
|
-
if ((
|
|
2061
|
-
target = new Date(dateRearrangeMapper[(
|
|
2103
|
+
if ((_h = (_g = validations.date) === null || _g === void 0 ? void 0 : _g.target) === null || _h === void 0 ? void 0 : _h.format) {
|
|
2104
|
+
target = new Date(dateRearrangeMapper[(_k = (_j = validations.date) === null || _j === void 0 ? void 0 : _j.target) === null || _k === void 0 ? void 0 : _k.format](validations.date.target.value).toString());
|
|
2062
2105
|
targetDate = target;
|
|
2063
2106
|
}
|
|
2064
2107
|
if (validations.date.origin.intervals) {
|
|
2065
2108
|
targetDate = getIntervalsDate(originDate, validations.date.origin.intervals);
|
|
2066
2109
|
const date = new Date();
|
|
2067
|
-
if (((
|
|
2110
|
+
if (((_l = validations.date.target) === null || _l === void 0 ? void 0 : _l.value) && target) {
|
|
2068
2111
|
date.setDate(target.getDate());
|
|
2069
2112
|
date.setMonth(target.getMonth());
|
|
2070
2113
|
}
|
|
@@ -2083,7 +2126,7 @@ const date = (value, validations) => {
|
|
|
2083
2126
|
'===': originTimestamp === targetTimestamp,
|
|
2084
2127
|
'!==': originTimestamp !== targetTimestamp
|
|
2085
2128
|
};
|
|
2086
|
-
return operationsMapper[(
|
|
2129
|
+
return operationsMapper[(_m = validations.date) === null || _m === void 0 ? void 0 : _m.operator];
|
|
2087
2130
|
};
|
|
2088
2131
|
/**
|
|
2089
2132
|
* @function validDate
|
|
@@ -3291,10 +3334,10 @@ class FormCore {
|
|
|
3291
3334
|
const error = handleValidation(field.value, structElement.validations, validations, validationKey);
|
|
3292
3335
|
if (Array.isArray(structElement.fields)) {
|
|
3293
3336
|
structElement.fields.forEach(fieldKey => {
|
|
3294
|
-
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = !error;
|
|
3337
|
+
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = structElement.showOnlyIfTrue ? error : !error;
|
|
3295
3338
|
});
|
|
3296
3339
|
} else if (structElement.fields) {
|
|
3297
|
-
if (!this.fields.has(structElement.fields)) console.warn(`failed to update visibility onto field ${structElement.fields}`);else this.fields.get(structElement.fields).visibility = !error;
|
|
3340
|
+
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;
|
|
3298
3341
|
}
|
|
3299
3342
|
});
|
|
3300
3343
|
});
|
package/package.json
CHANGED
package/src/types/schema.d.ts
CHANGED
|
@@ -618,18 +618,23 @@ type TMasks = {
|
|
|
618
618
|
* @type TVisibility
|
|
619
619
|
* Represents the visibility conditions for form fields based on validations.
|
|
620
620
|
*
|
|
621
|
+
* @property {boolean} showOnlyIfTrue - Enables visibility of fields only if any or all validation conditions are positive.
|
|
621
622
|
* @property {TSchemaValidation} validations - The validation methods to determine visibility.
|
|
622
623
|
* @property {string[] | string} fields - The fields to be shown or hidden based on validations.
|
|
624
|
+
* @property {string[]} events - Events where visibility will occur.
|
|
623
625
|
*
|
|
624
626
|
* @example
|
|
625
627
|
* ```typescript
|
|
626
628
|
* const visibility: TVisibility = {
|
|
629
|
+
* showOnlyIfTrue: false,
|
|
627
630
|
* validations: { required: true },
|
|
628
|
-
* fields: ['fieldName']
|
|
631
|
+
* fields: ['fieldName'],
|
|
632
|
+
* events: ['ON_FIELD_CHANGE']
|
|
629
633
|
* };
|
|
630
634
|
* ```
|
|
631
635
|
*/
|
|
632
636
|
type TVisibility = {
|
|
637
|
+
showOnlyIfTrue?: boolean;
|
|
633
638
|
validations: TSchemaValidation;
|
|
634
639
|
fields: string[] | string;
|
|
635
640
|
events: Partial<TEvents>[];
|