nox-validation 1.1.2 → 1.1.4

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
@@ -355,7 +355,7 @@ const convertTypesV1 = (field) => {
355
355
  return { type, array_type, find_relations };
356
356
  };
357
357
 
358
- const generateField = (name, path, schema_definition_type, type, childrenFields = []) => {
358
+ const generateField = (name, path, schema_definition_type, type, childrenFields = [],relationType="none") => {
359
359
  childrenFields = childrenFields?.map((child) => {
360
360
  return {
361
361
  ...child,
@@ -375,6 +375,7 @@ const generateField = (name, path, schema_definition_type, type, childrenFields
375
375
  required: false,
376
376
  nullable: false,
377
377
  hidden: false,
378
+ interface:relationType
378
379
  },
379
380
  validations: [],
380
381
  schema_definition: {
@@ -426,9 +427,24 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
426
427
  const deleteField = generateField(
427
428
  "delete",
428
429
  `${key}.delete`,
429
- constants.types.OBJECT_ID,
430
+ constants.types.OBJECT,
430
431
  constants.types.ARRAY
431
432
  );
433
+ deleteField.children = [
434
+ generateField(
435
+ "collection",
436
+ `${key}.delete.collection`,
437
+ constants.types.STRING,
438
+ constants.types.STRING
439
+ ),
440
+ generateField("sort", `${key}.delete.sort`, constants.types.NUMBER, constants.types.NUMBER),
441
+ generateField(
442
+ "item",
443
+ `${key}.delete.item`,
444
+ constants.types.OBJECT_ID,
445
+ constants.types.OBJECT_ID
446
+ ),
447
+ ];
432
448
  const createField = generateField(
433
449
  "create",
434
450
  `${key}.create`,
@@ -449,7 +465,8 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
449
465
  `${key}.create.item`,
450
466
  constants.types.OBJECT,
451
467
  constants.types.OBJECT,
452
- collectionFields
468
+ collectionFields,
469
+ relationType
453
470
  ),
454
471
  ];
455
472
  const updateField = generateField(
@@ -472,7 +489,8 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
472
489
  `${key}.update.item`,
473
490
  constants.types.OBJECT,
474
491
  constants.types.OBJECT,
475
- collectionFields
492
+ collectionFields,
493
+ relationType
476
494
  ),
477
495
  ];
478
496
  return [existingField, deleteField, createField, updateField];
@@ -633,7 +651,8 @@ const getCachedOrFetchFields = (schemaId, allFields, relational_fields) => {
633
651
  return fields;
634
652
  };
635
653
 
636
- const getChildFields = (relationDetail, allFields, relational_fields, isTranslation) => {
654
+ const getChildFields = (relationDetail, allFields, relational_fields, isTranslation,name) => {
655
+
637
656
  let key = isTranslation
638
657
  ? [
639
658
  ...(Array.isArray(relationDetail.junction_collection)
@@ -714,7 +733,7 @@ const buildNestedStructure = ({
714
733
  relationDetail,
715
734
  allFields,
716
735
  relational_fields,
717
- item?.meta?.interface === constants.interfaces.TRANSLATIONS
736
+ item?.meta?.interface === constants.interfaces.TRANSLATIONS,key
718
737
  );
719
738
  } else {
720
739
  childFields = getCachedFields(relationDetail, relational_fields);
@@ -740,6 +759,7 @@ const buildNestedStructure = ({
740
759
 
741
760
  const node = {
742
761
  field_id: item?._id,
762
+ schema_id: item?.schema_id,
743
763
  display_label: pathParts[pathParts.length - 1],
744
764
  key,
745
765
  value: key,
package/lib/validate.js CHANGED
@@ -563,8 +563,11 @@ const validateField = (
563
563
  addError,
564
564
  formData,
565
565
  updateValue,
566
- error_messages
566
+ error_messages,
567
+ onlyFormFields
567
568
  ) => {
569
+ if (onlyFormFields == true && (value === undefined || value === null)) return;
570
+
568
571
  const currentPath = fieldPath ? `${fieldPath}.${field.key.split(".").pop()}` : field.key;
569
572
  const fieldLabel = formatLabel(field.display_label);
570
573
 
@@ -576,7 +579,37 @@ const validateField = (
576
579
  value &&
577
580
  typeChecks[constants.types.OBJECT](value)
578
581
  ) {
579
- field.children.forEach((child) =>
582
+ const isManyToAnyItem =
583
+ field.display_label === "item" && field?.meta?.interface === constants.interfaces.MANY_TO_ANY;
584
+
585
+ const defaultKeys = new Set([
586
+ "_id",
587
+ "created_at",
588
+ "updated_at",
589
+ "__v",
590
+ "created_by",
591
+ "updated_by",
592
+ "nox_created_by",
593
+ "nox_updated_by",
594
+ "nox_created_at",
595
+ "nox_updated_at",
596
+ ]);
597
+
598
+ let itemSchemaId = null;
599
+
600
+ if (isManyToAnyItem) {
601
+ const itemKey = Object.keys(value).find((key) => !defaultKeys.has(key));
602
+ if (itemKey) {
603
+ const childField = field.children.find((child) => child.display_label === itemKey);
604
+ itemSchemaId = childField?.schema_id ?? null;
605
+ }
606
+ }
607
+
608
+ const childrenToValidate = itemSchemaId
609
+ ? field.children.filter((child) => child.schema_id === itemSchemaId)
610
+ : field.children;
611
+
612
+ childrenToValidate.forEach((child) =>
580
613
  validateField(
581
614
  child,
582
615
  value[child.key.split(".").pop()],
@@ -584,7 +617,8 @@ const validateField = (
584
617
  addError,
585
618
  formData,
586
619
  updateValue,
587
- error_messages
620
+ error_messages,
621
+ onlyFormFields
588
622
  )
589
623
  );
590
624
  } else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
@@ -618,7 +652,8 @@ const validateField = (
618
652
  addError,
619
653
  formData,
620
654
  updateValue,
621
- error_messages
655
+ error_messages,
656
+ onlyFormFields
622
657
  )
623
658
  );
624
659
  });
@@ -665,9 +700,16 @@ const schema = {
665
700
  apiVersion: { type: constants.types.STRING, array_type: null },
666
701
  language: { type: constants.types.STRING, array_type: null },
667
702
  maxLevel: { type: constants.types.NUMBER, array_type: null },
703
+ onlyFormFields: { type: constants.types.BOOLEAN, array_type: null },
668
704
  };
669
705
 
670
706
  const validate = (data) => {
707
+ if (!data?.language) {
708
+ data.language = constants.LANGUAGES.en;
709
+ }
710
+ if (data.onlyFormFields === undefined) {
711
+ data.onlyFormFields = false;
712
+ }
671
713
  const {
672
714
  formData,
673
715
  isSeparatedFields,
@@ -680,11 +722,8 @@ const validate = (data) => {
680
722
  apiVersion,
681
723
  language,
682
724
  maxLevel,
725
+ onlyFormFields,
683
726
  } = data;
684
-
685
- if (!data?.language) {
686
- data.language = constants.LANGUAGES.en;
687
- }
688
727
 
689
728
  const error_messages = constants.LOCALE_MESSAGES[language];
690
729
 
@@ -883,7 +922,8 @@ const validate = (data) => {
883
922
  addError,
884
923
  formData,
885
924
  updateValue,
886
- error_messages
925
+ error_messages,
926
+ onlyFormFields
887
927
  );
888
928
  });
889
929
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {