@beseif-solutions/prow-core 1.0.2 → 1.0.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.
@@ -71,7 +71,7 @@ class Context {
71
71
  }
72
72
  }
73
73
  else {
74
- resolved = ``;
74
+ return undefined;
75
75
  }
76
76
  }
77
77
  else {
@@ -457,9 +457,12 @@ const recursive_schema = async (field, data, options) => {
457
457
  : newField.key;
458
458
  const value = lodash_1.default.get(data, relativeKey, newField.default);
459
459
  const resolved = context_1.default.resolve(newField, value, options.context);
460
- if (typeof resolved !== `undefined`) {
460
+ if (!options.no_undefined || typeof resolved !== `undefined`) {
461
461
  lodash_1.default.set(data, relativeKey, resolved);
462
462
  }
463
+ if (typeof resolved === `undefined` && options.array) {
464
+ return { schema: null, field: null };
465
+ }
463
466
  let calculatedSchema;
464
467
  if (newField.as_array) {
465
468
  const noArrayField = lodash_1.default.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]);
@@ -472,6 +475,8 @@ const recursive_schema = async (field, data, options) => {
472
475
  itemSchemas.push(itemSchema);
473
476
  }
474
477
  }
478
+ const withoutUndefined = resolved.filter((r) => typeof r !== `undefined`);
479
+ lodash_1.default.set(data, relativeKey, withoutUndefined);
475
480
  }
476
481
  calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
477
482
  }
@@ -480,7 +485,7 @@ const recursive_schema = async (field, data, options) => {
480
485
  if (noArrayField.type === `dict` && noArrayField.items) {
481
486
  const items = [];
482
487
  for (const item of noArrayField.items) {
483
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
488
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context, no_undefined: true });
484
489
  if (recursive) {
485
490
  items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
486
491
  }
@@ -495,15 +500,21 @@ const recursive_schema = async (field, data, options) => {
495
500
  }
496
501
  }
497
502
  else if (newField.type === `dict` && newField.items) {
498
- const items = [];
499
- for (const item of newField.items) {
500
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
501
- if (recursive) {
502
- items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
503
+ if (resolved || newField.required) {
504
+ const items = [];
505
+ for (const item of newField.items) {
506
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
507
+ if (recursive) {
508
+ items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
509
+ }
503
510
  }
511
+ calculatedSchema = getObjectFieldSchema(newField, items);
512
+ newField.items = items.map((i) => i.field);
513
+ }
514
+ else {
515
+ delete newField.items;
516
+ calculatedSchema = getSimpleFieldSchema(newField);
504
517
  }
505
- calculatedSchema = getObjectFieldSchema(newField, items);
506
- newField.items = items.map((i) => i.field);
507
518
  }
508
519
  else {
509
520
  calculatedSchema = getSimpleFieldSchema(newField);
@@ -600,7 +611,7 @@ const extract_recursive = async (validation, options, path = ``) => {
600
611
  const key = lodash_1.default.compact([path, field.key]).join(`.`);
601
612
  lodash_1.default.set(response, key, options.original ? field.original : field.value);
602
613
  }
603
- return response;
614
+ return JSON.parse(JSON.stringify(response));
604
615
  }
605
616
  catch (e) {
606
617
  throw e;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -82,7 +82,11 @@ class Context {
82
82
  } else {
83
83
  resolved = _.toString(get);
84
84
  }
85
- } else { resolved = ``; }
85
+ } else {
86
+ // empty value
87
+ // resolved = ``;
88
+ return undefined;
89
+ }
86
90
  } else {
87
91
  // partial expression
88
92
  const parts = split(unresolved);
@@ -478,7 +478,7 @@ export const alter = (field: Field, relative: string, data: Record<string, any>)
478
478
  return cloned;
479
479
  };
480
480
 
481
- const recursive_schema = async (field: Field, data: Record<string, any>, options: { alter: boolean, relative?: string, array?: boolean, context: Record<string, any> }): Promise<{ schema: Joi.Schema, field: Field }> => {
481
+ const recursive_schema = async (field: Field, data: Record<string, any>, options: { alter: boolean, context: Record<string, any>, relative?: string, array?: boolean, no_undefined?: boolean }): Promise<{ schema: Joi.Schema, field: Field }> => {
482
482
  try {
483
483
  let newField: Field = _.cloneDeep(field);
484
484
 
@@ -505,9 +505,12 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
505
505
 
506
506
  const value = _.get(data, relativeKey, newField.default);
507
507
 
508
- // resolve value with context (even if it's not valid - omit undefined)
508
+ // resolve value with context (even if it's not valid)
509
509
  const resolved = context.resolve(newField, value, options.context);
510
- if (typeof resolved !== `undefined`) { _.set(data, relativeKey, resolved); }
510
+ if (!options.no_undefined || typeof resolved !== `undefined`) { _.set(data, relativeKey, resolved); }
511
+
512
+ // omit undefined for array mappings
513
+ if (typeof resolved === `undefined` && options.array) { return { schema: null, field: null }; }
511
514
 
512
515
  // get schema for field with data
513
516
  let calculatedSchema: Joi.Schema;
@@ -521,6 +524,10 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
521
524
  const { schema: itemSchema } = await recursive_schema(noArrayField, data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
522
525
  if (itemSchema) { itemSchemas.push(itemSchema); }
523
526
  }
527
+
528
+ // override array value - omit undefined items
529
+ const withoutUndefined = resolved.filter((r) => typeof r !== `undefined`);
530
+ _.set(data, relativeKey, withoutUndefined);
524
531
  }
525
532
 
526
533
  calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
@@ -530,7 +537,7 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
530
537
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
531
538
 
532
539
  for (const item of noArrayField.items) {
533
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
540
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context, no_undefined: true });
534
541
  if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
535
542
  }
536
543
 
@@ -543,15 +550,20 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
543
550
  calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
544
551
  }
545
552
  } else if (newField.type === `dict` && newField.items) {
546
- const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
553
+ if (resolved || newField.required) {
554
+ const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
547
555
 
548
- for (const item of newField.items) {
549
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
550
- if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
551
- }
556
+ for (const item of newField.items) {
557
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
558
+ if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
559
+ }
552
560
 
553
- calculatedSchema = getObjectFieldSchema(newField, items);
554
- newField.items = items.map((i) => i.field);
561
+ calculatedSchema = getObjectFieldSchema(newField, items);
562
+ newField.items = items.map((i) => i.field);
563
+ } else {
564
+ delete newField.items;
565
+ calculatedSchema = getSimpleFieldSchema(newField);
566
+ }
555
567
  } else {
556
568
  calculatedSchema = getSimpleFieldSchema(newField);
557
569
  }
@@ -642,7 +654,8 @@ const extract_recursive = async (validation: Field<ValidationExtended>[], option
642
654
  const key = _.compact([path, field.key]).join(`.`);
643
655
  _.set(response, key, options.original ? field.original : field.value);
644
656
  }
645
- return response;
657
+ // remove undefined values
658
+ return JSON.parse(JSON.stringify(response));
646
659
  } catch (e) { throw e; }
647
660
  };
648
661