nox-validation 1.5.2 → 1.5.3

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/constant.js CHANGED
@@ -148,6 +148,7 @@ const interfaces = Object.freeze({
148
148
  MULTIPLE_DROPDOWN: "dropdown_multiple",
149
149
  TAGS: "tags",
150
150
  ARRAY_OF_VALUES: "array_of_values",
151
+ OBJECT: "object",
151
152
  });
152
153
 
153
154
  const API_VERSION = Object.freeze({
package/lib/helpers.js CHANGED
@@ -313,6 +313,7 @@ const generateField = (
313
313
  hidden: false,
314
314
  }
315
315
  ) => {
316
+ const { staticType } = meta;
316
317
  childrenFields = childrenFields?.map((child) => {
317
318
  const childKey = path ? `${path}.${child.key}` : child.key;
318
319
  const childValue = path ? `${path}.${child.value}` : child.value;
@@ -320,6 +321,7 @@ const generateField = (
320
321
  return {
321
322
  ...child,
322
323
  value: childKey,
324
+ meta: staticType ? { ...child?.meta, staticType } : child?.meta,
323
325
  key: childValue,
324
326
  isRelationalUpdate: path.includes("update"),
325
327
  };
@@ -406,7 +408,11 @@ const generateRelationalField = (
406
408
  "existing",
407
409
  `${key}.existing`,
408
410
  constants.types.OBJECT,
409
- constants.types.ARRAY
411
+ constants.types.OBJECT,
412
+ [],
413
+ "none",
414
+ [],
415
+ { staticType: "existing" }
410
416
  );
411
417
  existingField.children = [
412
418
  generateField(
@@ -442,7 +448,11 @@ const generateRelationalField = (
442
448
  "delete",
443
449
  `${key}.delete`,
444
450
  constants.types.OBJECT,
445
- constants.types.ARRAY
451
+ constants.types.ARRAY,
452
+ [],
453
+ "none",
454
+ [],
455
+ { staticType: "delete" }
446
456
  );
447
457
  deleteField.children = [
448
458
  generateField(
@@ -479,7 +489,11 @@ const generateRelationalField = (
479
489
  `${key}.create`,
480
490
  constants.types.OBJECT,
481
491
  constants.types.ARRAY,
482
- collectionFields
492
+ // collectionFields //TODO:REMOVED CHILDREN
493
+ [],
494
+ "none",
495
+ [],
496
+ { staticType: "create" }
483
497
  );
484
498
  createField.children = [
485
499
  generateField(
@@ -520,7 +534,11 @@ const generateRelationalField = (
520
534
  `${key}.update`,
521
535
  constants.types.OBJECT,
522
536
  constants.types.ARRAY,
523
- collectionFields
537
+ // collectionFields //TODO:REMOVED CHILDREN
538
+ [],
539
+ "none",
540
+ [],
541
+ { staticType: "update" }
524
542
  );
525
543
  updateField.children = [
526
544
  generateField(
@@ -563,27 +581,41 @@ const generateRelationalField = (
563
581
  "existing",
564
582
  `${key}.existing`,
565
583
  constants.types.OBJECT_ID,
566
- constants.types.ARRAY
584
+ constants.types.ARRAY,
585
+ [],
586
+ "none",
587
+ [],
588
+ { staticType: "existing" }
567
589
  ),
568
590
  generateField(
569
591
  "delete",
570
592
  `${key}.delete`,
571
593
  constants.types.OBJECT_ID,
572
- constants.types.ARRAY
594
+ constants.types.ARRAY,
595
+ [],
596
+ "none",
597
+ [],
598
+ { staticType: "delete" }
573
599
  ),
574
600
  generateField(
575
601
  "create",
576
602
  `${key}.create`,
577
603
  constants.types.OBJECT,
578
604
  constants.types.ARRAY,
579
- collectionFields
605
+ collectionFields,
606
+ "none",
607
+ [],
608
+ { staticType: "create" }
580
609
  ),
581
610
  generateField(
582
611
  "update",
583
612
  `${key}.update`,
584
613
  constants.types.OBJECT,
585
614
  constants.types.ARRAY,
586
- collectionFields
615
+ collectionFields,
616
+ "none",
617
+ [],
618
+ { staticType: "update" }
587
619
  ),
588
620
  ];
589
621
  }
@@ -830,6 +862,8 @@ const buildNestedStructure = ({
830
862
  childFields = childFields.map((f) => {
831
863
  if (
832
864
  f.field === relationDetail.foreign_field ||
865
+ f.field === relationDetail.foreign_collection ||
866
+ f.field === relationDetail.junction_field_foreign ||
833
867
  f.field === relationDetail.junction_field_this
834
868
  ) {
835
869
  return {
@@ -1364,6 +1398,23 @@ const getSelectedNodes = ({
1364
1398
  }) => {
1365
1399
  const result = [];
1366
1400
 
1401
+ function updateChildKeys(node, parentKey, newParentKey) {
1402
+ let updatedNode = { ...node };
1403
+ if (
1404
+ typeof node.key === "string" &&
1405
+ node.key.startsWith(parentKey) &&
1406
+ !node.key.startsWith(newParentKey)
1407
+ ) {
1408
+ updatedNode.key = node.key.replace(parentKey, newParentKey);
1409
+ }
1410
+ if (Array.isArray(node.children) && node.children.length > 0) {
1411
+ updatedNode.children = node.children.map((c) =>
1412
+ updateChildKeys(c, parentKey, newParentKey)
1413
+ );
1414
+ }
1415
+ return updatedNode;
1416
+ }
1417
+
1367
1418
  function traverse(currentNode) {
1368
1419
  if (conditionFn(currentNode)) {
1369
1420
  actionFn(currentNode);
@@ -1372,29 +1423,16 @@ const getSelectedNodes = ({
1372
1423
 
1373
1424
  if (Array.isArray(currentNode.children)) {
1374
1425
  currentNode.children.forEach((child) => {
1375
- let childNode = {
1376
- ...child,
1377
- key:
1378
- currentNode.type === constants.types.ARRAY
1379
- ? child.key.includes(`${currentNode.key}[`)
1380
- ? child.key
1381
- : child.key.replace(currentNode.key, `${currentNode.key}[0]`)
1382
- : child.key,
1383
- };
1426
+ let childNode = { ...child };
1384
1427
 
1385
- if (Array.isArray(child.children) && child.children.length > 0) {
1386
- const updateChildKeys = (node, parentKey, newParentKey) => {
1387
- let updatedNode = {
1388
- ...node,
1389
- key: node.key.replace(parentKey, newParentKey),
1390
- };
1391
- if (Array.isArray(node.children) && node.children.length > 0) {
1392
- updatedNode.children = node.children.map((c) =>
1393
- updateChildKeys(c, parentKey, newParentKey)
1394
- );
1395
- }
1396
- return updatedNode;
1397
- };
1428
+ if (
1429
+ currentNode.type === constants.types.ARRAY &&
1430
+ typeof child.key === "string"
1431
+ ) {
1432
+ const arrayKey = `${currentNode.key}[0]`;
1433
+ if (!childNode.key.startsWith(arrayKey)) {
1434
+ childNode = updateChildKeys(child, currentNode.key, arrayKey);
1435
+ }
1398
1436
  }
1399
1437
 
1400
1438
  if (!skipFn(currentNode)) traverse(childNode);
package/lib/validate.js CHANGED
@@ -219,12 +219,24 @@ const isEmptyRelational = ({
219
219
  case constants.interfaces.ONE_TO_MANY:
220
220
  case constants.interfaces.MANY_TO_ONE:
221
221
  return remainingItems({
222
- all: existingValue?.map((item) => item._id),
222
+ all: Array.isArray(existingValue)
223
+ ? existingValue.length === 0
224
+ ? []
225
+ : typeof existingValue[0] === "string"
226
+ ? existingValue
227
+ : existingValue.map((item) => item._id)
228
+ : typeof existingValue === "string"
229
+ ? [existingValue]
230
+ : existingValue && typeof existingValue === "object"
231
+ ? [existingValue._id]
232
+ : [],
223
233
  obj: value,
224
234
  });
225
235
  case constants.interfaces.MANY_TO_ANY:
226
236
  return remainingItems({
227
- all: existingValue?.map((item) => item.item),
237
+ all: Array.isArray(existingValue)
238
+ ? existingValue?.map((item) => item.item)
239
+ : [],
228
240
  obj: value,
229
241
  isM2A: true,
230
242
  });
@@ -250,7 +262,8 @@ const validateMetaRules = (
250
262
  fieldOptions,
251
263
  formData,
252
264
  language_codes = [],
253
- existingForm = {}
265
+ existingForm = {},
266
+ getNodes = true
254
267
  ) => {
255
268
  const fieldValue = providedValue;
256
269
 
@@ -259,6 +272,7 @@ const validateMetaRules = (
259
272
  nullable = true,
260
273
  options,
261
274
  is_m2a_item,
275
+ hidden = false,
262
276
  } = field?.meta ?? {};
263
277
 
264
278
  if (field?.validations?.find((i) => i?.case === "not_empty")) {
@@ -266,7 +280,7 @@ const validateMetaRules = (
266
280
  nullable = false;
267
281
  }
268
282
 
269
- if (field?.validations?.find((i) => i?.case === "empty")) {
283
+ if (field?.validations?.find((i) => i?.case === "empty") || hidden) {
270
284
  required = false;
271
285
  nullable = true;
272
286
  }
@@ -340,7 +354,19 @@ const validateMetaRules = (
340
354
  : true;
341
355
 
342
356
  const isEmptyVal = is_m2a_item ? !fieldValue : isEmpty(fieldValue);
343
- if ((required && !onlyFormFields && isEmptyVal) || !isValidRelational) {
357
+
358
+ let invalidRequire;
359
+ if (onlyFormFields) {
360
+ if (typeof fieldValue !== "undefined" && fieldValue !== null) {
361
+ invalidRequire = isEmptyVal;
362
+ } else {
363
+ invalidRequire = false;
364
+ }
365
+ } else {
366
+ invalidRequire = required && isEmptyVal;
367
+ }
368
+
369
+ if (invalidRequire || !isValidRelational) {
344
370
  const message = error_messages.REQUIRED.replace(
345
371
  `{field}`,
346
372
  formatLabel(field.display_label)
@@ -376,7 +402,7 @@ const validateMetaRules = (
376
402
 
377
403
  if (
378
404
  (isEmpty(fieldValue) &&
379
- !field?.meta?.hidden &&
405
+ required &&
380
406
  ![
381
407
  constants.interfaces.FILES,
382
408
  constants.interfaces.FILE,
@@ -389,7 +415,12 @@ const validateMetaRules = (
389
415
  ].includes(field?.meta?.interface)) ||
390
416
  (field?.meta?.interface === constants.interfaces.TRANSLATIONS &&
391
417
  language_codes?.length &&
392
- !onlyFormFields)
418
+ !onlyFormFields) ||
419
+ ([constants.interfaces.OBJECT, constants.interfaces.ITEMS].includes(
420
+ field?.meta?.interface
421
+ ) &&
422
+ !onlyFormFields &&
423
+ getNodes)
393
424
  ) {
394
425
  const isTranslationChild =
395
426
  field?.meta?.interface === constants.interfaces.TRANSLATIONS;
@@ -405,11 +436,12 @@ const validateMetaRules = (
405
436
  label: node.display_label,
406
437
  }),
407
438
  actionFn: (node) => {
408
- let fPath = node.key;
409
- if (fPath.includes("update")) return;
410
- if (fPath.includes("create") && !isTranslationChild) return;
411
- if (fPath.includes("delete") && !isTranslationChild) return;
412
- if (fPath.includes("existing") && !isTranslationChild) return;
439
+ const staticType = node?.meta?.staticType;
440
+ let fPath = node.key?.replace("[0][0]", "[0]");
441
+
442
+ if (staticType === "update") return;
443
+ if (staticType === "delete" && !isTranslationChild) return;
444
+ if (staticType === "existing" && !isTranslationChild) return;
413
445
 
414
446
  const label = formatLabel(node.display_label);
415
447
  const message = error_messages.REQUIRED.replace("{field}", label);
@@ -458,7 +490,12 @@ const validateMetaRules = (
458
490
  "create.",
459
491
  isTranslationChild ? "create[lang_code]." : "create[0]."
460
492
  );
461
- if (isEmpty(fieldValue)) buildError(singlePath);
493
+ if (
494
+ constants.interfaces.ITEMS === field?.meta?.interface
495
+ ? true
496
+ : isEmpty(fieldValue)
497
+ )
498
+ buildError(singlePath);
462
499
  }
463
500
  },
464
501
  });
@@ -911,7 +948,8 @@ const validateField = (
911
948
  apiVersion,
912
949
  fieldOptions,
913
950
  language_codes,
914
- existingForm
951
+ existingForm,
952
+ getNodes = true
915
953
  ) => {
916
954
  if (onlyFormFields == true && value === undefined) return;
917
955
 
@@ -934,7 +972,8 @@ const validateField = (
934
972
  fieldOptions,
935
973
  formData,
936
974
  language_codes,
937
- existingForm
975
+ existingForm,
976
+ getNodes
938
977
  );
939
978
 
940
979
  if (
@@ -971,7 +1010,8 @@ const validateField = (
971
1010
  apiVersion,
972
1011
  fieldOptions,
973
1012
  language_codes,
974
- existingForm
1013
+ existingForm,
1014
+ false
975
1015
  )
976
1016
  );
977
1017
  } else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
@@ -1023,7 +1063,8 @@ const validateField = (
1023
1063
  apiVersion,
1024
1064
  fieldOptions,
1025
1065
  language_codes,
1026
- existingForm
1066
+ existingForm,
1067
+ false
1027
1068
  )
1028
1069
  );
1029
1070
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {