nox-validation 1.2.3 → 1.2.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 +12 -0
- package/lib/validate.js +43 -8
- package/package.json +1 -1
package/lib/helpers.js
CHANGED
|
@@ -1168,6 +1168,17 @@ const getDefaultValues = (tree) => {
|
|
|
1168
1168
|
return defaultValues;
|
|
1169
1169
|
};
|
|
1170
1170
|
|
|
1171
|
+
const extractRelationalParents=(path)=> {
|
|
1172
|
+
const match = path?.match(/^(translations)\.(create|update|delete|existing)\[\d+\]/);
|
|
1173
|
+
if (match) {
|
|
1174
|
+
const secondParent = match[1];
|
|
1175
|
+
const firstParent = match[0].split('.')[0] + '.' + match[2] + '[' + path.match(/\[(\d+)\]/)[1] + ']'; //
|
|
1176
|
+
const afterKey = match[3] || '';
|
|
1177
|
+
return { firstParent, secondParent, afterKey };
|
|
1178
|
+
}
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1171
1182
|
module.exports = {
|
|
1172
1183
|
generateModifiedRules,
|
|
1173
1184
|
getFieldsGroupBySchemaId,
|
|
@@ -1186,4 +1197,5 @@ module.exports = {
|
|
|
1186
1197
|
getAllFields,
|
|
1187
1198
|
getForeignCollectionDetails,
|
|
1188
1199
|
getDefaultValues,
|
|
1200
|
+
extractRelationalParents
|
|
1189
1201
|
};
|
package/lib/validate.js
CHANGED
|
@@ -8,6 +8,7 @@ const {
|
|
|
8
8
|
setValue,
|
|
9
9
|
isEmpty,
|
|
10
10
|
getParentKey,
|
|
11
|
+
extractRelationalParents,
|
|
11
12
|
} = require("./helpers");
|
|
12
13
|
|
|
13
14
|
const choices = ["radio", "checkboxes", "dropdown_multiple", "dropdown"];
|
|
@@ -169,7 +170,9 @@ const validateMetaRules = (
|
|
|
169
170
|
updateValue,
|
|
170
171
|
error_messages,
|
|
171
172
|
onlyFormFields,
|
|
172
|
-
apiVersion
|
|
173
|
+
apiVersion,
|
|
174
|
+
fieldOptions,
|
|
175
|
+
formData
|
|
173
176
|
) => {
|
|
174
177
|
const fieldValue = providedValue;
|
|
175
178
|
const { required = false, nullable = false, options } = field?.meta ?? {};
|
|
@@ -630,7 +633,8 @@ const validateField = (
|
|
|
630
633
|
updateValue,
|
|
631
634
|
error_messages,
|
|
632
635
|
onlyFormFields,
|
|
633
|
-
apiVersion
|
|
636
|
+
apiVersion,
|
|
637
|
+
fieldOptions
|
|
634
638
|
) => {
|
|
635
639
|
if (onlyFormFields == true && (value === undefined || value === null)) return;
|
|
636
640
|
|
|
@@ -645,7 +649,9 @@ const validateField = (
|
|
|
645
649
|
updateValue,
|
|
646
650
|
error_messages,
|
|
647
651
|
onlyFormFields,
|
|
648
|
-
apiVersion
|
|
652
|
+
apiVersion,
|
|
653
|
+
fieldOptions,
|
|
654
|
+
formData
|
|
649
655
|
);
|
|
650
656
|
|
|
651
657
|
if (
|
|
@@ -694,7 +700,8 @@ const validateField = (
|
|
|
694
700
|
updateValue,
|
|
695
701
|
error_messages,
|
|
696
702
|
onlyFormFields,
|
|
697
|
-
apiVersion
|
|
703
|
+
apiVersion,
|
|
704
|
+
fieldOptions
|
|
698
705
|
)
|
|
699
706
|
);
|
|
700
707
|
} else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
|
|
@@ -703,7 +710,7 @@ const validateField = (
|
|
|
703
710
|
value.forEach((item, index) => {
|
|
704
711
|
const itemPath = `${currentPath}[${index}]`;
|
|
705
712
|
|
|
706
|
-
if (choices.includes(field?.meta?.interface)) {
|
|
713
|
+
if (choices.includes(field?.meta?.interface) && !isEmpty(item)) {
|
|
707
714
|
applyValidations(field, item, addError, itemPath, formData, error_messages);
|
|
708
715
|
}
|
|
709
716
|
|
|
@@ -736,7 +743,8 @@ const validateField = (
|
|
|
736
743
|
updateValue,
|
|
737
744
|
error_messages,
|
|
738
745
|
onlyFormFields,
|
|
739
|
-
apiVersion
|
|
746
|
+
apiVersion,
|
|
747
|
+
fieldOptions
|
|
740
748
|
)
|
|
741
749
|
);
|
|
742
750
|
});
|
|
@@ -758,7 +766,7 @@ const validateField = (
|
|
|
758
766
|
};
|
|
759
767
|
|
|
760
768
|
const applyValidations = (field, value, addError, currentPath, formData, error_messages) => {
|
|
761
|
-
if (!field.validations || value === null || value === undefined) return true;
|
|
769
|
+
if (!field.validations || value === null || value === undefined || value === "") return true;
|
|
762
770
|
return !field.validations.some(
|
|
763
771
|
(rule) =>
|
|
764
772
|
handleRule(rule, field, value, addError, currentPath, formData, error_messages) === false
|
|
@@ -820,6 +828,32 @@ const validate = (data) => {
|
|
|
820
828
|
const addError = (fieldPath, obj, field) => {
|
|
821
829
|
if (byPassKeys.some((key) => fieldPath.startsWith(key))) return;
|
|
822
830
|
if (!result.errors[fieldPath] && !field?.meta?.hidden) {
|
|
831
|
+
const pathResult = extractRelationalParents(fieldPath);
|
|
832
|
+
if (pathResult) {
|
|
833
|
+
const { firstParent, secondParent } = pathResult;
|
|
834
|
+
const secondParentField = fields.find((f) => f.path === secondParent);
|
|
835
|
+
if (
|
|
836
|
+
secondParentField &&
|
|
837
|
+
secondParentField?.meta?.interface === constants.interfaces.TRANSLATIONS
|
|
838
|
+
) {
|
|
839
|
+
const languageKey = secondParentField?.meta?.options?.language_field;
|
|
840
|
+
const firstParentValue = getValue(formData, firstParent);
|
|
841
|
+
if (firstParentValue && typeChecks[constants.types.OBJECT](firstParentValue)) {
|
|
842
|
+
const codeKey = Object.keys(firstParentValue).find((key) => key.includes(languageKey));
|
|
843
|
+
const codeValue = codeKey ? firstParentValue[codeKey] : null;
|
|
844
|
+
if (codeValue) {
|
|
845
|
+
const translation_key = fieldPath.replace(
|
|
846
|
+
firstParent,
|
|
847
|
+
`${secondParent}.${codeValue}`
|
|
848
|
+
);
|
|
849
|
+
if (translation_key) {
|
|
850
|
+
obj.translation_path = translation_key;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
823
857
|
result.errors[fieldPath] = obj;
|
|
824
858
|
result.status = false;
|
|
825
859
|
}
|
|
@@ -1008,7 +1042,8 @@ const validate = (data) => {
|
|
|
1008
1042
|
updateValue,
|
|
1009
1043
|
error_messages,
|
|
1010
1044
|
onlyFormFields,
|
|
1011
|
-
apiVersion
|
|
1045
|
+
apiVersion,
|
|
1046
|
+
fieldOptions
|
|
1012
1047
|
);
|
|
1013
1048
|
});
|
|
1014
1049
|
return result;
|