nox-validation 1.2.0 → 1.2.1

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 CHANGED
@@ -731,8 +731,6 @@ const buildNestedStructure = ({
731
731
 
732
732
  const currentDepth = currentDepthMap.get(isRoot ? item.path : rootPath) || 0;
733
733
 
734
- // if (currentDepth >= maxLevel) return;
735
-
736
734
  let childFields;
737
735
 
738
736
  let definedType = generateType(item, apiVersion);
package/lib/validate.js CHANGED
@@ -134,18 +134,64 @@ const handleMinMaxValidation = (
134
134
  }
135
135
  };
136
136
 
137
+ const isEmptyRelational = ({ api_version, value, interface }) => {
138
+ if (api_version === constants.API_VERSION.V1 && interface !== constants.interfaces.TRANSLATIONS) {
139
+ if (interface === constants.interfaces.MANY_TO_ANY) {
140
+ return (
141
+ value &&
142
+ typeChecks[constants.types.OBJECT](value) &&
143
+ value.hasOwnProperty("collection") &&
144
+ value.hasOwnProperty("sort") &&
145
+ value.hasOwnProperty("item") &&
146
+ value.collection !== null &&
147
+ value.collection !== "" &&
148
+ value.sort !== null &&
149
+ value.sort !== "" &&
150
+ value.item !== null &&
151
+ value.item !== ""
152
+ );
153
+ } else {
154
+ return value?.length > 0;
155
+ }
156
+ } else {
157
+ return value?.create?.length > 0 || value?.existing?.length > 0;
158
+ }
159
+ return false;
160
+ };
161
+
137
162
  const validateMetaRules = (
138
163
  field,
139
164
  addError,
140
165
  providedValue,
141
166
  currentPath,
142
167
  updateValue,
143
- error_messages
168
+ error_messages,
169
+ onlyFormFields,
170
+ apiVersion
144
171
  ) => {
145
172
  const fieldValue = providedValue;
146
173
  const { required = false, nullable = false } = field?.meta ?? {};
147
-
148
- if (required && isEmpty(fieldValue) && fieldValue !== null) {
174
+ const isRelational = [
175
+ constants.interfaces.FILES,
176
+ constants.interfaces.FILE,
177
+ constants.interfaces.FILE_IMAGE,
178
+ constants.interfaces.MANY_TO_MANY,
179
+ constants.interfaces.ONE_TO_MANY,
180
+ constants.interfaces.MANY_TO_ONE,
181
+ constants.interfaces.MANY_TO_ANY,
182
+ constants.interfaces.TRANSLATIONS,
183
+ ].includes(field?.meta?.interface);
184
+
185
+ const isValidRelational =
186
+ required && isRelational && !onlyFormFields
187
+ ? isEmptyRelational({
188
+ api_version: apiVersion,
189
+ value: fieldValue,
190
+ interface: field?.meta?.interface,
191
+ })
192
+ : true;
193
+
194
+ if ((required && isEmpty(fieldValue) && fieldValue !== null) || !isValidRelational) {
149
195
  const message = error_messages.REQUIRED.replace(`{field}`, formatLabel(field.display_label));
150
196
  addError(
151
197
  currentPath,
@@ -564,14 +610,24 @@ const validateField = (
564
610
  formData,
565
611
  updateValue,
566
612
  error_messages,
567
- onlyFormFields
613
+ onlyFormFields,
614
+ apiVersion
568
615
  ) => {
569
616
  if (onlyFormFields == true && (value === undefined || value === null)) return;
570
617
 
571
618
  const currentPath = fieldPath ? `${fieldPath}.${field.key.split(".").pop()}` : field.key;
572
619
  const fieldLabel = formatLabel(field.display_label);
573
620
 
574
- validateMetaRules(field, addError, value, currentPath, updateValue, error_messages);
621
+ validateMetaRules(
622
+ field,
623
+ addError,
624
+ value,
625
+ currentPath,
626
+ updateValue,
627
+ error_messages,
628
+ onlyFormFields,
629
+ apiVersion
630
+ );
575
631
 
576
632
  if (
577
633
  field.type === constants.types.OBJECT &&
@@ -618,7 +674,8 @@ const validateField = (
618
674
  formData,
619
675
  updateValue,
620
676
  error_messages,
621
- onlyFormFields
677
+ onlyFormFields,
678
+ apiVersion
622
679
  )
623
680
  );
624
681
  } else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
@@ -653,7 +710,8 @@ const validateField = (
653
710
  formData,
654
711
  updateValue,
655
712
  error_messages,
656
- onlyFormFields
713
+ onlyFormFields,
714
+ apiVersion
657
715
  )
658
716
  );
659
717
  });
@@ -725,7 +783,8 @@ const validate = (data) => {
725
783
  onlyFormFields,
726
784
  } = data;
727
785
 
728
- const error_messages = constants.LOCALE_MESSAGES[language] ?? constants.LOCALE_MESSAGES[constants.LANGUAGES.en];
786
+ const error_messages =
787
+ constants.LOCALE_MESSAGES[language] ?? constants.LOCALE_MESSAGES[constants.LANGUAGES.en];
729
788
 
730
789
  let result = { status: true, errors: {}, data: formData };
731
790
 
@@ -923,7 +982,8 @@ const validate = (data) => {
923
982
  formData,
924
983
  updateValue,
925
984
  error_messages,
926
- onlyFormFields
985
+ onlyFormFields,
986
+ apiVersion
927
987
  );
928
988
  });
929
989
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {