nox-validation 1.0.6 → 1.0.7

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.
Files changed (2) hide show
  1. package/lib/helpers.js +84 -93
  2. package/package.json +1 -1
package/lib/helpers.js CHANGED
@@ -158,30 +158,6 @@ const isEmpty = (val) => {
158
158
  };
159
159
 
160
160
  const convertTypes = (field) => {
161
- // array_type:
162
- // item.type !== item.schema_definition.type &&
163
- // item.schema_definition.type !== constants.types.ALIAS
164
- // ? item.schema_definition.type
165
- // : item.type === item.schema_definition.type &&
166
- // item.type === constants.types.ARRAY
167
- // ? constants.types.OBJECT
168
- // : null,
169
-
170
- // type:
171
- // item.type === constants.types.ALIAS ||
172
- // item.schema_definition.type === constants.types.ALIAS
173
- // ? constants.types.OBJECT
174
- // : item.type,
175
-
176
- // 'list-o2m', // alias
177
- // 'list-m2o', // objectid
178
- // 'list-m2m', // alias
179
- // 'file', // objectid
180
- // 'file-image', // objectid
181
- // 'files', // alias
182
- // 'list-m2a', // alias
183
- // 'translations', // alias
184
-
185
161
  let { type, schema_definition, meta } = field;
186
162
  let array_type = schema_definition?.type ?? null;
187
163
  let interfaceType = meta?.interface;
@@ -207,7 +183,7 @@ const convertTypes = (field) => {
207
183
  return {
208
184
  type: constants.types.ARRAY,
209
185
  array_type: constants.types.OBJECT_ID,
210
- find_relations: true,
186
+ find_relations: false,
211
187
  };
212
188
  }
213
189
 
@@ -270,90 +246,83 @@ const convertTypesV1 = (field) => {
270
246
  return { type, array_type, find_relations };
271
247
  };
272
248
 
273
- const generateRelationalFieldV1 = (key = "", collectionFields = [], relationType) => {
274
- const generateField = (name, path, schema_definition_type, type, childrenFields = []) => {
275
- const relationalPath = key ? `${key}.${path}` : path;
276
-
249
+ const generateField = (name, path, schema_definition_type, type, childrenFields = []) => {
250
+ childrenFields = childrenFields?.map((child) => {
277
251
  return {
278
- display_label: name,
279
- key: relationalPath,
280
- value: relationalPath,
281
- children: childrenFields,
282
- type: type,
283
- meta: {
284
- required: false,
285
- nullable: false,
286
- hidden: false,
287
- },
288
- validations: [],
289
- array_type: schema_definition_type,
252
+ ...child,
253
+ value: `${path}.${child.value}`,
254
+ key: `${path}.${child.key}`,
290
255
  };
256
+ });
257
+
258
+ return {
259
+ display_label: name,
260
+ // field: name,
261
+ key: path,
262
+ value: path,
263
+ children: childrenFields,
264
+ type: type,
265
+ meta: {
266
+ required: false,
267
+ nullable: false,
268
+ hidden: false,
269
+ },
270
+ validations: [],
271
+ schema_definition: {
272
+ type: schema_definition_type,
273
+ },
291
274
  };
275
+ };
292
276
 
277
+ const generateRelationalFieldV1 = (key = "", collectionFields = [], relationType) => {
293
278
  if (relationType === constants.interfaces.MANY_TO_ANY) {
294
279
  return [
295
- generateField("collection", "collection", constants.types.STRING, constants.types.STRING),
296
- generateField("sort", "sort", constants.types.NUMBER, constants.types.NUMBER),
297
- generateField("item", "item", constants.types.OBJECT_ID, constants.types.OBJECT_ID),
280
+ generateField(
281
+ "collection",
282
+ `${key}.collection`,
283
+ constants.types.STRING,
284
+ constants.types.STRING
285
+ ),
286
+ generateField("sort", `${key}.sort`, constants.types.NUMBER, constants.types.NUMBER),
287
+ generateField("item", `${key}.item`, constants.types.OBJECT_ID, constants.types.OBJECT_ID),
298
288
  ];
299
289
  } else {
300
- return [];
290
+ return null;
301
291
  }
302
292
  };
303
293
 
304
294
  const generateRelationalField = (key = "", collectionFields = [], relationType) => {
305
- const generateField = (name, path, schema_definition_type, type, childrenFields = []) => {
306
- const relationalPath = key ? `${key}.${path}` : path;
307
- childrenFields = childrenFields?.map((child) => {
308
- return {
309
- ...child,
310
- key: `${relationalPath}.${child.key}`,
311
- value: `${relationalPath}.${child.value}`,
312
- };
313
- });
314
-
315
- return {
316
- display_label: name,
317
- key: relationalPath,
318
- value: relationalPath,
319
- children: childrenFields,
320
- type: type,
321
- meta: {
322
- required: false,
323
- nullable: false,
324
- hidden: false,
325
- },
326
- validations: [],
327
- array_type: schema_definition_type,
328
- };
329
- };
330
-
331
295
  if (relationType === constants.interfaces.MANY_TO_ANY) {
332
296
  const existingField = generateField(
333
- `${key}.existing`,
334
297
  "existing",
298
+ `${key}.existing`,
335
299
  constants.types.OBJECT,
336
300
  constants.types.ARRAY
337
301
  );
338
302
  existingField.children = [
339
303
  generateField(
340
304
  "collection",
341
- "existing.collection",
305
+ `${key}.existing.collection`,
342
306
  constants.types.STRING,
343
307
  constants.types.STRING
344
308
  ),
345
- generateField("sort", "existing.sort", constants.types.NUMBER, constants.types.NUMBER),
346
- generateField("item", "existing.item", constants.types.OBJECT_ID, constants.types.OBJECT_ID),
309
+ generateField("sort", `${key}.existing.sort`, constants.types.NUMBER, constants.types.NUMBER),
310
+ generateField(
311
+ "item",
312
+ `${key}.existing.item`,
313
+ constants.types.OBJECT_ID,
314
+ constants.types.OBJECT_ID
315
+ ),
347
316
  ];
348
317
  const deleteField = generateField(
349
318
  "delete",
350
- "delete",
319
+ `${key}.delete`,
351
320
  constants.types.OBJECT_ID,
352
321
  constants.types.ARRAY
353
322
  );
354
323
  const createField = generateField(
355
324
  "create",
356
- "create",
325
+ `${key}.create`,
357
326
  constants.types.OBJECT,
358
327
  constants.types.ARRAY,
359
328
  collectionFields
@@ -361,14 +330,14 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
361
330
  createField.children = [
362
331
  generateField(
363
332
  "collection",
364
- "create.collection",
333
+ `${key}.create.collection`,
365
334
  constants.types.STRING,
366
335
  constants.types.STRING
367
336
  ),
368
- generateField("sort", "create.sort", constants.types.NUMBER, constants.types.NUMBER),
337
+ generateField("sort", `${key}.create.sort`, constants.types.NUMBER, constants.types.NUMBER),
369
338
  generateField(
370
339
  "item",
371
- "create.item",
340
+ `${key}.create.item`,
372
341
  constants.types.OBJECT,
373
342
  constants.types.OBJECT,
374
343
  collectionFields
@@ -376,7 +345,7 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
376
345
  ];
377
346
  const updateField = generateField(
378
347
  "update",
379
- "update",
348
+ `${key}.update`,
380
349
  constants.types.OBJECT,
381
350
  constants.types.ARRAY,
382
351
  collectionFields
@@ -384,14 +353,14 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
384
353
  updateField.children = [
385
354
  generateField(
386
355
  "collection",
387
- "update.collection",
356
+ `${key}.update.collection`,
388
357
  constants.types.STRING,
389
358
  constants.types.STRING
390
359
  ),
391
- generateField("sort", "update.sort", constants.types.NUMBER, constants.types.NUMBER),
360
+ generateField("sort", `${key}.update.sort`, constants.types.NUMBER, constants.types.NUMBER),
392
361
  generateField(
393
362
  "item",
394
- "update.item",
363
+ `${key}.update.item`,
395
364
  constants.types.OBJECT,
396
365
  constants.types.OBJECT,
397
366
  collectionFields
@@ -400,18 +369,23 @@ const generateRelationalField = (key = "", collectionFields = [], relationType)
400
369
  return [existingField, deleteField, createField, updateField];
401
370
  } else {
402
371
  return [
403
- generateField("existing", "existing", constants.types.OBJECT_ID, constants.types.ARRAY),
404
- generateField("delete", "delete", constants.types.OBJECT_ID, constants.types.ARRAY),
372
+ generateField(
373
+ "existing",
374
+ `${key}.existing`,
375
+ constants.types.OBJECT_ID,
376
+ constants.types.ARRAY
377
+ ),
378
+ generateField("delete", `${key}.delete`, constants.types.OBJECT_ID, constants.types.ARRAY),
405
379
  generateField(
406
380
  "create",
407
- "create",
381
+ `${key}.create`,
408
382
  constants.types.OBJECT,
409
383
  constants.types.ARRAY,
410
384
  collectionFields
411
385
  ),
412
386
  generateField(
413
387
  "update",
414
- "update",
388
+ `${key}.update`,
415
389
  constants.types.OBJECT,
416
390
  constants.types.ARRAY,
417
391
  collectionFields
@@ -573,6 +547,18 @@ const getChildFields = (relationDetail, allFields, relational_fields, isTranslat
573
547
  }
574
548
  };
575
549
 
550
+ const default_fields = [
551
+ "updated_by",
552
+ "created_by",
553
+ "created_at",
554
+ "updated_at",
555
+ "nox_created_at",
556
+ "nox_updated_at",
557
+ "nox_created_by",
558
+ "nox_updated_by",
559
+ "_id",
560
+ ];
561
+
576
562
  const buildNestedStructure = (
577
563
  schemaFields,
578
564
  allFields,
@@ -588,13 +574,20 @@ const buildNestedStructure = (
588
574
  schemaFields.sort((a, b) => a.path.split(".").length - b.path.split(".").length);
589
575
 
590
576
  schemaFields.forEach((item) => {
577
+ const pathParts = item.path.split(".");
578
+ const key = pathParts.join(".");
579
+
591
580
  let childFields;
592
581
 
593
582
  const definedType =
594
583
  apiVersion === constants.API_VERSION.V1 ? convertTypesV1(item) : convertTypes(item);
595
- const isUserKey = item?.path?.includes("updated_by") || item?.path?.includes("created_by");
584
+ // const isUserKey = default_fields.some((field) => item?.path?.includes(field));
596
585
 
597
- if (definedType.find_relations && !isUserKey) {
586
+ if (
587
+ definedType.find_relations &&
588
+ (item?.type === constants.types.ALIAS ||
589
+ item?.schema_definition?.type === constants.types.ALIAS)
590
+ ) {
598
591
  const relationDetail = getForeignCollectionDetails({
599
592
  relations: relations,
600
593
  collection: item?.schema_id,
@@ -624,12 +617,10 @@ const buildNestedStructure = (
624
617
  relational_fields,
625
618
  isSeparatedFields
626
619
  );
620
+
627
621
  }
628
622
  }
629
623
 
630
- const pathParts = item.path.split(".");
631
- const key = pathParts.join(".");
632
-
633
624
  const node = {
634
625
  field_id: item?._id,
635
626
  display_label: pathParts[pathParts.length - 1],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {