@beseif-solutions/prow-core 1.0.3 → 1.0.5

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.
@@ -457,7 +457,14 @@ 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
- lodash_1.default.set(data, relativeKey, resolved);
460
+ if (!options.no_undefined || typeof resolved !== `undefined`) {
461
+ if (newField.type === `any` && typeof resolved === `undefined`) {
462
+ lodash_1.default.set(data, relativeKey, null);
463
+ }
464
+ else {
465
+ lodash_1.default.set(data, relativeKey, resolved);
466
+ }
467
+ }
461
468
  if (typeof resolved === `undefined` && options.array) {
462
469
  return { schema: null, field: null };
463
470
  }
@@ -483,7 +490,7 @@ const recursive_schema = async (field, data, options) => {
483
490
  if (noArrayField.type === `dict` && noArrayField.items) {
484
491
  const items = [];
485
492
  for (const item of noArrayField.items) {
486
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
493
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context, no_undefined: true });
487
494
  if (recursive) {
488
495
  items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
489
496
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -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
 
@@ -507,7 +507,14 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
507
507
 
508
508
  // resolve value with context (even if it's not valid)
509
509
  const resolved = context.resolve(newField, value, options.context);
510
- _.set(data, relativeKey, resolved);
510
+ if (!options.no_undefined || typeof resolved !== `undefined`) {
511
+ if (newField.type === `any` && typeof resolved === `undefined`) {
512
+ // any field - allow_nullish
513
+ _.set(data, relativeKey, null);
514
+ } else {
515
+ _.set(data, relativeKey, resolved);
516
+ }
517
+ }
511
518
 
512
519
  // omit undefined for array mappings
513
520
  if (typeof resolved === `undefined` && options.array) { return { schema: null, field: null }; }
@@ -537,7 +544,7 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
537
544
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
538
545
 
539
546
  for (const item of noArrayField.items) {
540
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
547
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context, no_undefined: true });
541
548
  if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
542
549
  }
543
550