nox-validation 1.2.9 → 1.3.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
@@ -267,101 +267,14 @@ const generateType = (field, api) => {
267
267
  }
268
268
  };
269
269
 
270
- const convertTypes = (field) => {
271
- let { type, schema_definition, meta } = field;
272
- let array_type = schema_definition?.type ?? null;
273
- let interfaceType = meta?.interface;
274
- let find_relations = false;
275
-
276
- if (
277
- [
278
- constants.interfaces.ONE_TO_MANY,
279
- constants.interfaces.MANY_TO_MANY,
280
- constants.interfaces.MANY_TO_ANY,
281
- constants.interfaces.MANY_TO_ONE,
282
- constants.interfaces.TRANSLATIONS,
283
- ].includes(interfaceType)
284
- ) {
285
- return {
286
- type: constants.types.OBJECT,
287
- array_type: null,
288
- find_relations: true,
289
- };
290
- }
291
-
292
- if (interfaceType === constants.interfaces.FILES) {
293
- return {
294
- type: constants.types.ARRAY,
295
- array_type: constants.types.OBJECT_ID,
296
- find_relations: false,
297
- };
298
- }
299
-
300
- if (type !== schema_definition?.type && schema_definition?.type !== constants.types.ALIAS) {
301
- array_type = schema_definition.type;
302
- }
303
- return { type, array_type, find_relations };
304
- };
305
-
306
- const convertTypesV1 = (field) => {
307
- let { type, schema_definition, meta } = field;
308
- let array_type = schema_definition?.type ?? null;
309
- let interfaceType = meta?.interface;
310
- let find_relations = false;
311
-
312
- if ([constants.interfaces.TRANSLATIONS].includes(interfaceType)) {
313
- return {
314
- type: constants.types.OBJECT,
315
- array_type: null,
316
- find_relations: true,
317
- };
318
- }
319
-
320
- if ([constants.interfaces.MANY_TO_ANY].includes(interfaceType)) {
321
- return {
322
- type: constants.types.ARRAY,
323
- array_type: constants.types.OBJECT,
324
- find_relations: true,
325
- };
326
- }
327
-
328
- if ([constants.interfaces.MANY_TO_ONE].includes(interfaceType)) {
329
- return {
330
- type: constants.types.OBJECT_ID,
331
- array_type: null,
332
- find_relations: false,
333
- };
334
- }
335
-
336
- if (
337
- [
338
- constants.interfaces.ONE_TO_MANY,
339
- constants.interfaces.MANY_TO_MANY,
340
- constants.interfaces.FILES,
341
- ].includes(interfaceType)
342
- ) {
343
- return {
344
- type: constants.types.ARRAY,
345
- array_type: constants.types.OBJECT_ID,
346
- find_relations: false,
347
- };
348
- }
349
-
350
- if (type !== schema_definition?.type && schema_definition?.type !== constants.types.ALIAS) {
351
- array_type = schema_definition.type;
352
- find_relations = false;
353
- }
354
-
355
- return { type, array_type, find_relations };
356
- };
357
-
358
270
  const generateField = (
359
271
  name,
360
272
  path,
361
273
  schema_definition_type,
362
274
  type,
363
275
  childrenFields = [],
364
- relationType = "none"
276
+ relationType = "none",
277
+ alternateType=[]
365
278
  ) => {
366
279
  childrenFields = childrenFields?.map((child) => {
367
280
  return {
@@ -378,6 +291,7 @@ const generateField = (
378
291
  value: path,
379
292
  children: childrenFields,
380
293
  type: type,
294
+ alternateType,
381
295
  meta: {
382
296
  required: false,
383
297
  nullable: false,
@@ -489,7 +403,8 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
489
403
  constants.types.OBJECT,
490
404
  constants.types.OBJECT,
491
405
  collectionFields,
492
- relationType
406
+ 'none',
407
+ [constants.types.OBJECT_ID]
493
408
  ),
494
409
  ];
495
410
  const updateField = generateField(
@@ -513,7 +428,8 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
513
428
  constants.types.OBJECT,
514
429
  constants.types.OBJECT,
515
430
  collectionFields,
516
- relationType
431
+ "none",
432
+ [constants.types.OBJECT_ID]
517
433
  ),
518
434
  ];
519
435
  return [existingField, deleteField, createField, updateField];
@@ -789,6 +705,49 @@ const buildNestedStructure = ({
789
705
  childFields = createChildrenFieldsFiles(key);
790
706
  }
791
707
 
708
+ const isArray =
709
+ item.type === item?.schema_definition.type && item.type === constants.types.ARRAY;
710
+
711
+ let children = [];
712
+
713
+ if (childFields?.length > 0) {
714
+ if (isV2File) {
715
+ children = childFields;
716
+ } else if (
717
+ apiVersion === constants.API_VERSION.V1 &&
718
+ item.meta?.interface !== constants.interfaces.TRANSLATIONS
719
+ ) {
720
+ children = generateRelationalFieldV1(key, childFields, item.meta?.interface);
721
+ } else {
722
+ children = generateRelationalField(key, childFields, item.meta?.interface);
723
+ }
724
+ }
725
+
726
+ if (isArray) {
727
+ const idField = {
728
+ field_id: `${key}._id`,
729
+ schema_id: item?.schema_id,
730
+ display_label: "_id",
731
+ key: `${key}._id`,
732
+ value: `${key}._id`,
733
+ alternateType:[],
734
+ meta: {
735
+ interface: "none",
736
+ required: false,
737
+ nullable: false,
738
+ hidden: false,
739
+ options: {},
740
+ },
741
+ validations: [],
742
+ custom_error_message: null,
743
+ children: [],
744
+ type: constants.types.OBJECT_ID,
745
+ array_type: definedType.array_type,
746
+ default_value: null,
747
+ };
748
+ children.push(idField);
749
+ }
750
+
792
751
  const node = {
793
752
  field_id: item?._id,
794
753
  schema_id: item?.schema_id,
@@ -804,15 +763,15 @@ const buildNestedStructure = ({
804
763
  },
805
764
  validations: generateModifiedRules(item?.meta, item?.meta?.validations?.validation_msg),
806
765
  custom_error_message: item?.meta?.validations?.validation_msg,
807
- children:
808
- childFields?.length > 0
809
- ? isV2File
810
- ? childFields
811
- : apiVersion === constants.API_VERSION.V1 &&
812
- item.meta?.interface !== constants.interfaces.TRANSLATIONS
813
- ? generateRelationalFieldV1(key, childFields, item.meta?.interface)
814
- : generateRelationalField(key, childFields, item.meta?.interface)
815
- : [],
766
+ children,
767
+ // childFields?.length > 0
768
+ // ? isV2File
769
+ // ? childFields
770
+ // : apiVersion === constants.API_VERSION.V1 &&
771
+ // item.meta?.interface !== constants.interfaces.TRANSLATIONS
772
+ // ? generateRelationalFieldV1(key, childFields, item.meta?.interface)
773
+ // : generateRelationalField(key, childFields, item.meta?.interface)
774
+ // : [],
816
775
  type: definedType.type,
817
776
  array_type: definedType.array_type,
818
777
  default_value: item?.schema_definition?.default,
@@ -1168,16 +1127,17 @@ const getDefaultValues = (tree) => {
1168
1127
  return defaultValues;
1169
1128
  };
1170
1129
 
1171
- const extractRelationalParents=(path)=> {
1130
+ const extractRelationalParents = (path) => {
1172
1131
  const match = path?.match(/^([^.\[\]]+)\.(create|update|delete|existing)\[(\d+)\](?:\.(.*))?/);
1173
1132
  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] || '';
1133
+ const secondParent = match[1];
1134
+ const firstParent =
1135
+ match[0].split(".")[0] + "." + match[2] + "[" + path.match(/\[(\d+)\]/)[1] + "]"; //
1136
+ const afterKey = match[3] || "";
1177
1137
  return { firstParent, secondParent, afterKey };
1178
1138
  }
1179
1139
  return null;
1180
- }
1140
+ };
1181
1141
 
1182
1142
  module.exports = {
1183
1143
  generateModifiedRules,
@@ -1197,5 +1157,5 @@ module.exports = {
1197
1157
  getAllFields,
1198
1158
  getForeignCollectionDetails,
1199
1159
  getDefaultValues,
1200
- extractRelationalParents
1160
+ extractRelationalParents,
1201
1161
  };
package/lib/validate.js CHANGED
@@ -157,7 +157,7 @@ const isEmptyRelational = ({ api_version, value, interface }) => {
157
157
  return value?.length > 0;
158
158
  }
159
159
  } else {
160
- return value?.create?.length > 0 || value?.existing?.length > 0 || value?.update?.length > 0;
160
+ return value?.create?.length > 0 || value?.existing?.length > 0 || value?.update?.length > 0;
161
161
  }
162
162
  return false;
163
163
  };
@@ -244,10 +244,14 @@ const validateMetaRules = (
244
244
  );
245
245
  }
246
246
 
247
+ const validType =
248
+ field?.alternateType?.length > 0
249
+ ? [field.type, ...field?.alternateType].some((type) => typeChecks[type](fieldValue))
250
+ : typeChecks[field.type](fieldValue,{ key: currentPath, updateValue });
251
+
247
252
  if (
248
253
  !isEmpty(fieldValue) &&
249
- typeChecks[field.type] &&
250
- !typeChecks[field.type](fieldValue, { key: currentPath, updateValue })
254
+ !validType
251
255
  ) {
252
256
  const message = error_messages.INVALID_TYPE.replace(
253
257
  `{field}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.2.9",
3
+ "version": "1.3.1",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {