nox-validation 1.5.4 → 1.5.5
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/lib/helpers.js +8 -3
- package/lib/validate.js +18 -7
- package/package.json +1 -1
package/lib/helpers.js
CHANGED
|
@@ -203,7 +203,7 @@ const isEmpty = (val) => {
|
|
|
203
203
|
if (val == null) return true;
|
|
204
204
|
if (typeof val === "boolean") return false;
|
|
205
205
|
if (typeof val === "number") return false;
|
|
206
|
-
if (typeof val === "string") return val.length === 0;
|
|
206
|
+
if (typeof val === "string") return val?.trim().length === 0;
|
|
207
207
|
if (Array.isArray(val)) return val.length === 0;
|
|
208
208
|
if (typeof val === "object") return Object.keys(val).length === 0;
|
|
209
209
|
|
|
@@ -986,9 +986,14 @@ const buildNestedStructure = ({
|
|
|
986
986
|
},
|
|
987
987
|
validations: generateModifiedRules(
|
|
988
988
|
item?.meta,
|
|
989
|
-
item?.meta?.validations?.validation_msg
|
|
989
|
+
item?.meta?.validations?.validation_msg?.trim() === ""
|
|
990
|
+
? null
|
|
991
|
+
: item?.meta?.validations?.validation_msg
|
|
990
992
|
),
|
|
991
|
-
custom_error_message:
|
|
993
|
+
custom_error_message:
|
|
994
|
+
item?.meta?.validations?.validation_msg?.trim() === ""
|
|
995
|
+
? null
|
|
996
|
+
: item?.meta?.validations?.validation_msg,
|
|
992
997
|
children,
|
|
993
998
|
type: definedType.type,
|
|
994
999
|
array_type: definedType.array_type,
|
package/lib/validate.js
CHANGED
|
@@ -908,10 +908,10 @@ const handleRule = (
|
|
|
908
908
|
|
|
909
909
|
switch (rule.case) {
|
|
910
910
|
case constants.rulesTypes.EMPTY:
|
|
911
|
-
if (value) addValidationError("EMPTY", {}, custom_message);
|
|
911
|
+
if (!isEmpty(value)) addValidationError("EMPTY", {}, custom_message);
|
|
912
912
|
break;
|
|
913
913
|
case constants.rulesTypes.NOT_EMPTY:
|
|
914
|
-
if (
|
|
914
|
+
if (isEmpty(value) || value.length === 0)
|
|
915
915
|
addValidationError("NOT_EMPTY", {}, custom_message);
|
|
916
916
|
break;
|
|
917
917
|
case constants.rulesTypes.ONE_OF:
|
|
@@ -1014,7 +1014,7 @@ const validateField = (
|
|
|
1014
1014
|
if (
|
|
1015
1015
|
field.type === constants.types.OBJECT &&
|
|
1016
1016
|
field.children &&
|
|
1017
|
-
value &&
|
|
1017
|
+
!isEmpty(value) &&
|
|
1018
1018
|
typeChecks[constants.types.OBJECT](value)
|
|
1019
1019
|
) {
|
|
1020
1020
|
let itemSchemaId = null;
|
|
@@ -1055,7 +1055,11 @@ const validateField = (
|
|
|
1055
1055
|
value.forEach((item, index) => {
|
|
1056
1056
|
const itemPath = `${currentPath}[${index}]`;
|
|
1057
1057
|
|
|
1058
|
-
if (
|
|
1058
|
+
if (
|
|
1059
|
+
(choices.includes(field?.meta?.interface) && !isEmpty(item)) ||
|
|
1060
|
+
(["tags", "array_of_values"].includes(field?.meta?.interface) &&
|
|
1061
|
+
!isEmpty(item))
|
|
1062
|
+
) {
|
|
1059
1063
|
applyValidations(
|
|
1060
1064
|
field,
|
|
1061
1065
|
item,
|
|
@@ -1139,9 +1143,10 @@ const applyValidations = (
|
|
|
1139
1143
|
) => {
|
|
1140
1144
|
if (
|
|
1141
1145
|
!field.validations ||
|
|
1142
|
-
value
|
|
1143
|
-
value ===
|
|
1144
|
-
value ===
|
|
1146
|
+
isEmpty(value)
|
|
1147
|
+
// value === null ||
|
|
1148
|
+
// value === undefined ||
|
|
1149
|
+
// value === ""
|
|
1145
1150
|
)
|
|
1146
1151
|
return true;
|
|
1147
1152
|
return !field.validations.some(
|
|
@@ -1220,6 +1225,12 @@ const validate = (data) => {
|
|
|
1220
1225
|
};
|
|
1221
1226
|
|
|
1222
1227
|
const addError = (fieldPath, obj, field) => {
|
|
1228
|
+
fieldPath = [...choices, "tags", "array_of_values"].includes(
|
|
1229
|
+
field?.meta?.interface
|
|
1230
|
+
)
|
|
1231
|
+
? fieldPath?.replace(/\[\d+\].*$/, "")
|
|
1232
|
+
: fieldPath;
|
|
1233
|
+
|
|
1223
1234
|
const fieldKey = getLastChildKey(fieldPath);
|
|
1224
1235
|
const isBypass = byPassKeys?.some((key) => key === fieldKey);
|
|
1225
1236
|
|