@legalplace/wizardx-core 4.5.5 → 4.6.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.
@@ -1,5 +1,26 @@
1
1
  export const checkDateValidity = (date) => {
2
- const [day, month, year] = (date === null || date === void 0 ? void 0 : date.toString().split("/")) || [];
3
- const dateObject = new Date(`${year}-${month}-${day}`);
4
- return new Date(dateObject).toString() !== "Invalid Date";
2
+ const dateComponents = date.split("/");
3
+ if (dateComponents.length !== 3) {
4
+ return false;
5
+ }
6
+ const day = parseInt(dateComponents[0], 10);
7
+ const month = parseInt(dateComponents[1], 10);
8
+ const year = parseInt(dateComponents[2], 10);
9
+ if (Number.isNaN(day) || Number.isNaN(month) || Number.isNaN(year)) {
10
+ return false;
11
+ }
12
+ if (month < 1 || month > 12) {
13
+ return false;
14
+ }
15
+ const daysInMonth = new Date(year, month, 0).getDate();
16
+ if (day < 1 || day > daysInMonth) {
17
+ return false;
18
+ }
19
+ if (year <= 1000) {
20
+ return false;
21
+ }
22
+ const dateObject = new Date(year, month - 1, day);
23
+ return (dateObject.getFullYear() === year &&
24
+ dateObject.getMonth() === month - 1 &&
25
+ dateObject.getDate() === day);
5
26
  };
@@ -0,0 +1,2 @@
1
+ import type { VariableV3 } from "@legalplace/models-v3-types";
2
+ export declare const checkMandatoryVariable: (variable: Readonly<VariableV3>, value: string | number | boolean) => boolean;
@@ -0,0 +1,21 @@
1
+ import { checkDateValidity } from "./date.helper";
2
+ import { checkPhoneNumberValidity } from "./phoneNumber.helper";
3
+ import { EMAIL_REGEX } from "../constants/emailValidation";
4
+ const formatsCheck = {
5
+ email: (value) => EMAIL_REGEX.test(value),
6
+ user_email: (value) => EMAIL_REGEX.test(value),
7
+ date: (value) => checkDateValidity(value),
8
+ mask: (value, maskFormula) => value.toString().trim().length === (maskFormula === null || maskFormula === void 0 ? void 0 : maskFormula.length),
9
+ phone_number: (value) => checkPhoneNumberValidity(value.toString()),
10
+ };
11
+ export const checkMandatoryVariable = (variable, value) => {
12
+ const { type, mask, mandatory } = variable;
13
+ if (!mandatory || typeof value !== "string") {
14
+ return true;
15
+ }
16
+ const formatCheck = formatsCheck[type];
17
+ if (!formatCheck) {
18
+ return true;
19
+ }
20
+ return formatCheck(value.toString().trim(), mask === null || mask === void 0 ? void 0 : mask.formula);
21
+ };
@@ -1,14 +1,13 @@
1
+ import { checkMandatoryVariable } from "../../helpers/mandatoryVariable";
1
2
  import { UPDATE_OPTION_INPUT, UPDATE_VARIABLE_INPUT, ADD_MULTIPLE_OCCURENCY, DELETE_MULTIPLE_OCCURENCY, } from "../constants/inputs";
2
3
  import { selectVariableInputByIndex, selectOptionInputByIndex, selectOptionInput, } from "../selectors/inputs";
3
4
  import { SET_MANDATORY_OPTION, SET_MANDATORY_VARIABLE, } from "../constants/mandatories";
4
5
  import { selectOptionRelations, selectOptionReference, selectSectionReference, selectVariableRelations, selectAllSectionsReferences, selectRadioSiblings, selectVariableReference, } from "../selectors/references";
5
6
  import { selectMandatoryOption, selectMandatoryVariable, } from "../selectors/mandatories";
6
7
  import { setMandatoryOptionAction, setMandatorySectionAction, setMandatoryVariableAction, } from "../actions/mandatories";
7
- import { UPDATE_OPTION_CONDITION, UPDATE_VARIABLE_CONDITION, UPDATE_SECTION_CONDITION, } from "../constants/conditions";
8
- import { selectOptionConditionValue, selectVariableConditionValue, selectIsOptionDisplayed, selectIsVariableDisplayed, } from "../selectors/conditions";
8
+ import { UPDATE_OPTION_CONDITION, UPDATE_VARIABLE_CONDITION, UPDATE_SECTION_CONDITION, UPDATE_VARIABLE_VALIDATOR_CONDITION, } from "../constants/conditions";
9
+ import { selectOptionConditionValue, selectVariableConditionValue, selectIsOptionDisplayed, selectIsVariableDisplayed, selectVariableValidatorConditionValue, } from "../selectors/conditions";
9
10
  import { FETCH_MODEL_SUCCEEDED } from "../constants/app";
10
- import { checkDateValidity } from "../../helpers/date.helper";
11
- import { checkPhoneNumberValidity } from "../../helpers/phoneNumber.helper";
12
11
  const validateSectionMandatory = (mpi, sectionId) => {
13
12
  const parentSection = selectSectionReference(sectionId);
14
13
  let childOptions = [...parentSection.options];
@@ -128,19 +127,16 @@ const watchUpdateVariableInputCondition = (mpi, next, action, ignoreNext) => {
128
127
  const inputValue = type === UPDATE_VARIABLE_INPUT
129
128
  ? `${value}`.trim().length > 0
130
129
  : `${selectVariableInputByIndex(id, index)}`.trim().length > 0;
131
- const { type: variableType } = selectVariableReference(id);
130
+ const variableRef = selectVariableReference(id);
131
+ const { validator } = variableRef;
132
132
  const conditionValue = action.type === UPDATE_VARIABLE_CONDITION
133
133
  ? action.value
134
134
  : selectVariableConditionValue(id, index) !== false;
135
135
  let mandatoryValue = !conditionValue || (conditionValue && inputValue);
136
- if (variableType === "date" &&
137
- typeof value === "string" &&
138
- !checkDateValidity(value)) {
139
- mandatoryValue = false;
140
- }
141
- if (variableType === "phone_number" &&
142
- typeof value === "string" &&
143
- !checkPhoneNumberValidity(value)) {
136
+ if ((type === UPDATE_VARIABLE_INPUT ||
137
+ type === UPDATE_VARIABLE_VALIDATOR_CONDITION) &&
138
+ (!checkMandatoryVariable(variableRef, value) ||
139
+ (validator && selectVariableValidatorConditionValue(id, index) === false))) {
144
140
  mandatoryValue = false;
145
141
  }
146
142
  mpi.dispatch(setMandatoryVariableAction(id, index, mandatoryValue));
@@ -212,6 +208,7 @@ const watchFetchModelSucceeded = (mpi, next, action) => {
212
208
  };
213
209
  const watchersEnum = {
214
210
  [UPDATE_OPTION_INPUT]: watchUpdateOptionInputCondition,
211
+ [UPDATE_VARIABLE_VALIDATOR_CONDITION]: watchUpdateVariableInputCondition,
215
212
  [UPDATE_OPTION_CONDITION]: watchUpdateOptionInputCondition,
216
213
  [UPDATE_VARIABLE_INPUT]: watchUpdateVariableInputCondition,
217
214
  [UPDATE_VARIABLE_CONDITION]: watchUpdateVariableInputCondition,
@@ -1,4 +1,5 @@
1
1
  import { select, takeLatest, put } from "redux-saga/effects";
2
+ import { checkMandatoryVariable } from "../../helpers/mandatoryVariable";
2
3
  import { INIT_INPUTS } from "../constants/inputs";
3
4
  import { selectMandatoryOptionItem, selectMandatoryOption, } from "../selectors/mandatories";
4
5
  import { initMandatoryOptionAction, initMandatoryVariableAction, } from "../actions/mandatories";
@@ -6,8 +7,6 @@ import { initOptionAction, initVariableAction } from "../actions/inputs";
6
7
  import { initConditionsAction } from "../actions/conditions";
7
8
  import { ConditionsInitiator } from "../../libs/ConditionsInitiator";
8
9
  import { selectOptionRelations, selectVariableReference, } from "../selectors/references";
9
- import { checkDateValidity } from "../../helpers/date.helper";
10
- import { checkPhoneNumberValidity } from "../../helpers/phoneNumber.helper";
11
10
  import { cancelOnResetState } from "../../helpers/sagaCancelOnResetState";
12
11
  const getValues = (arr1, arr2) => arr1.map((v, index) => arr2[index] || v);
13
12
  export function* initInputsDecorator(action) {
@@ -50,21 +49,18 @@ export function* initInputsDecorator(action) {
50
49
  }
51
50
  }
52
51
  const variablesIds = Object.keys(inputs.variables);
53
- for (let i = 0; i < variablesIds.length; i += 1) {
54
- const variableId = variablesIds[i];
52
+ for (let index = 0; index < variablesIds.length; index += 1) {
53
+ const variableId = variablesIds[index];
55
54
  const values = inputs.variables[variableId];
56
55
  yield put(initVariableAction(parseInt(variableId, 10), values));
57
56
  if (references.variables[variableId].mandatory === true)
58
57
  yield put(initMandatoryVariableAction(parseInt(variableId, 10), values.map((value) => {
59
- const { type: variableType } = selectVariableReference(parseInt(variableId, 10));
60
- if (variableType === "date" &&
61
- typeof value === "string" &&
62
- !checkDateValidity(value)) {
63
- return false;
64
- }
65
- if (variableType === "phone_number" &&
66
- typeof value === "string" &&
67
- !checkPhoneNumberValidity(value)) {
58
+ var _a, _b;
59
+ const variableRef = selectVariableReference(parseInt(variableId, 10));
60
+ const { validator, id } = variableRef;
61
+ if (!checkMandatoryVariable(variableRef, value) ||
62
+ (validator &&
63
+ ((_b = (_a = conditions.validators.variables) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b[index]) === false)) {
68
64
  return false;
69
65
  }
70
66
  return `${value}`.trim().length > 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/wizardx-core",
3
- "version": "4.5.5",
3
+ "version": "4.6.0",
4
4
  "author": "Moncef Hammou (moncef@legalplace.fr)",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -26,7 +26,7 @@
26
26
  "@legalplace/model-healthcheck": "^1.1.5",
27
27
  "@legalplace/referencesparser": "^3.0.5",
28
28
  "@legalplace/storybook": "^2.171.0",
29
- "@legalplace/typeorm-constants": "^3.20.0",
29
+ "@legalplace/typeorm-constants": "^3.20.1",
30
30
  "@loadable/component": "^5.15.0",
31
31
  "@redux-saga/core": "^1.1.3",
32
32
  "connected-react-router": "^6.8.0",
@@ -55,9 +55,9 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@legalplace/eslint-config": "^2.2.0",
58
- "@legalplace/models-v3-types": "^5.1.22",
58
+ "@legalplace/models-v3-types": "^5.1.23",
59
59
  "@legalplace/prettier-config": "^2.1.3",
60
- "@legalplace/typeorm-entities": "^5.24.4",
60
+ "@legalplace/typeorm-entities": "^5.24.5",
61
61
  "@swc-node/jest": "^1.3.2",
62
62
  "@swc/core": "^1.2.93",
63
63
  "@swc/jest": "^0.2.4",
@@ -95,5 +95,5 @@
95
95
  "*.test.ts",
96
96
  "*.test.tsx"
97
97
  ],
98
- "gitHead": "8284be459e1867d0acaf6919459d2bf050305e5c"
98
+ "gitHead": "eec1426c5120bf1a47a5cd9076341c0bf3e4084a"
99
99
  }