@beseif-solutions/prow-core 1.2.0 → 1.2.2

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.
@@ -7,12 +7,14 @@ import { Source } from '../entities/source';
7
7
  export type LocalizedField = {
8
8
  label: string;
9
9
  help: string;
10
+ instructions?: string;
10
11
  ui?: Record<string, any>;
11
12
  };
12
13
  export type LocalizedProperties = {
13
14
  name: string;
14
15
  description: string;
15
16
  help?: Record<string, string>;
17
+ instructions?: string;
16
18
  };
17
19
  type TOptions = {
18
20
  fallback?: boolean;
@@ -98,6 +98,7 @@ const localizeField = (t, element, field, recursive = {}) => {
98
98
  const root = field.t || recursive.t || `${element}.fields`;
99
99
  field.label = t(`${root}.${key}.label`, { fallback: true });
100
100
  field.help = t(`${root}.${key}.help`);
101
+ field.instructions = t(`${root}.${key}.instructions`, { fallback: true, optional: true });
101
102
  field.ui = t(`${root}.${key}.ui`, { fallback: true, optional: true }) || {};
102
103
  if (field.type === `dict` && field.items) {
103
104
  field.items = field.items.map((f) => localizeField(t, element, f, { parent: `${key}.items`, t: field.t }));
@@ -107,6 +108,7 @@ const localizeField = (t, element, field, recursive = {}) => {
107
108
  ...fc,
108
109
  label: t(`${root}.${key}.choices.${fc.key}.label`, { fallback: true }),
109
110
  help: t(`${root}.${key}.choices.${fc.key}.help`),
111
+ instructions: t(`${root}.${key}.choices.${fc.key}.instructions`, { fallback: true, optional: true }),
110
112
  ui: {
111
113
  ...t(`${root}.${key}.choices.${fc.key}.ui`, { fallback: true, optional: true }) || {},
112
114
  ...fc.group && {
@@ -131,10 +133,12 @@ const localizeFields = (t, element) => {
131
133
  exports.localizeFields = localizeFields;
132
134
  const localizeProperties = (t, element) => {
133
135
  const help = t(`${element.id}.help`, { fallback: true, optional: true });
136
+ const instructions = t(`${element.id}.instructions`, { fallback: true, optional: true });
134
137
  return {
135
138
  name: t(`${element.id}.name`, { fallback: true }),
136
139
  description: t(`${element.id}.description`),
137
140
  ...help && { help: help },
141
+ ...instructions && { instructions: instructions },
138
142
  };
139
143
  };
140
144
  exports.localizeProperties = localizeProperties;
@@ -410,25 +410,25 @@ const alter = (field, relative, data) => {
410
410
  exports.alter = alter;
411
411
  const recursive_schema = async (field, data, options) => {
412
412
  try {
413
- let newField = lodash_1.default.cloneDeep(field);
414
- if (newField.altered_by?.length > 0) {
415
- newField = (0, exports.alter)(newField, options.relative, data);
413
+ let alteredField = lodash_1.default.cloneDeep(field);
414
+ if (alteredField.altered_by?.length > 0) {
415
+ alteredField = (0, exports.alter)(alteredField, options.relative, data);
416
416
  if (options.alter) {
417
- delete newField.altered_by;
417
+ delete alteredField.altered_by;
418
418
  }
419
- if (typeof newField.show === `boolean` && !newField.show) {
419
+ if (typeof alteredField.show === `boolean` && !alteredField.show) {
420
420
  return;
421
421
  }
422
422
  }
423
423
  const relativeKey = options.relative ?
424
424
  options.relative.endsWith(`]`) ?
425
- options.array ? options.relative : `${options.relative}.${newField.key}`
426
- : `${options.relative}.${newField.key}`
427
- : newField.key;
428
- const value = lodash_1.default.get(data, relativeKey, newField.default);
429
- const resolved = context_1.default.resolve(newField, value, options.context);
425
+ options.array ? options.relative : `${options.relative}.${alteredField.key}`
426
+ : `${options.relative}.${alteredField.key}`
427
+ : alteredField.key;
428
+ const value = lodash_1.default.get(data, relativeKey, alteredField.default);
429
+ const resolved = context_1.default.resolve(alteredField, value, options.context);
430
430
  if (!options.no_undefined || typeof resolved !== `undefined`) {
431
- if (newField.type === `any` && typeof resolved === `undefined`) {
431
+ if (alteredField.type === `any` && typeof resolved === `undefined`) {
432
432
  lodash_1.default.set(data, relativeKey, null);
433
433
  }
434
434
  else {
@@ -439,8 +439,8 @@ const recursive_schema = async (field, data, options) => {
439
439
  return { schema: null, field: null };
440
440
  }
441
441
  let calculatedSchema;
442
- if (newField.as_array) {
443
- const noArrayField = lodash_1.default.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]);
442
+ if (alteredField.as_array) {
443
+ const noArrayField = lodash_1.default.omit(alteredField, [`as_array`, `array_min`, `array_max`, `array_length`]);
444
444
  if (resolved) {
445
445
  const itemSchemas = [];
446
446
  if (lodash_1.default.isArray(resolved)) {
@@ -453,7 +453,7 @@ const recursive_schema = async (field, data, options) => {
453
453
  const withoutUndefined = resolved.filter((r) => typeof r !== `undefined`);
454
454
  lodash_1.default.set(data, relativeKey, withoutUndefined);
455
455
  }
456
- calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
456
+ calculatedSchema = getOrderedArrayFieldSchema(alteredField, itemSchemas);
457
457
  }
458
458
  else {
459
459
  let genericItemSchema;
@@ -471,36 +471,35 @@ const recursive_schema = async (field, data, options) => {
471
471
  else {
472
472
  genericItemSchema = getSimpleFieldSchema(noArrayField);
473
473
  }
474
- calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
474
+ calculatedSchema = getItemsArrayFieldSchema(alteredField, [genericItemSchema]);
475
475
  }
476
476
  }
477
- else if (newField.type === `dict` && newField.items) {
478
- if (resolved || newField.required) {
477
+ else if (alteredField.type === `dict` && alteredField.items) {
478
+ if (resolved || alteredField.required) {
479
479
  const items = [];
480
- for (const item of newField.items) {
480
+ for (const item of alteredField.items) {
481
481
  const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
482
482
  if (recursive) {
483
483
  items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
484
484
  }
485
485
  }
486
- calculatedSchema = getObjectFieldSchema(newField, items);
487
- newField.items = items.map((i) => i.field);
486
+ calculatedSchema = getObjectFieldSchema(alteredField, items);
487
+ alteredField.items = items.map((i) => i.field);
488
488
  }
489
489
  else {
490
- delete newField.items;
491
- calculatedSchema = getSimpleFieldSchema(newField);
490
+ calculatedSchema = getSimpleFieldSchema(lodash_1.default.omit(alteredField, [`items`]));
492
491
  }
493
492
  }
494
493
  else {
495
- calculatedSchema = getSimpleFieldSchema(newField);
494
+ calculatedSchema = getSimpleFieldSchema(alteredField);
496
495
  }
497
496
  if (!calculatedSchema) {
498
- throw new Error(`Could not resolve schema for field ${newField.key}`);
497
+ throw new Error(`Could not resolve schema for field ${alteredField.key}`);
499
498
  }
500
- if (newField.disabled) {
499
+ if (alteredField.disabled) {
501
500
  lodash_1.default.unset(data, relativeKey);
502
501
  }
503
- return { schema: calculatedSchema, field: newField };
502
+ return { schema: calculatedSchema, field: alteredField };
504
503
  }
505
504
  catch (e) {
506
505
  throw e;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -11,6 +11,7 @@ import { Source } from '../entities/source';
11
11
  export type LocalizedField = {
12
12
  label: string,
13
13
  help: string,
14
+ instructions?: string,
14
15
  ui?: Record<string, any>,
15
16
  };
16
17
 
@@ -18,6 +19,7 @@ export type LocalizedProperties = {
18
19
  name: string,
19
20
  description: string,
20
21
  help?: Record<string, string>,
22
+ instructions?: string,
21
23
  };
22
24
 
23
25
  type TOptions = { fallback?: boolean, optional?: boolean };
@@ -143,6 +145,7 @@ const localizeField = (
143
145
 
144
146
  field.label = t(`${root}.${key}.label`, { fallback: true });
145
147
  field.help = t(`${root}.${key}.help`);
148
+ field.instructions = t(`${root}.${key}.instructions`, { fallback: true, optional: true });
146
149
  field.ui = t(`${root}.${key}.ui`, { fallback: true, optional: true }) || {};
147
150
 
148
151
  if (field.type === `dict` && field.items) {
@@ -152,6 +155,7 @@ const localizeField = (
152
155
  ...fc,
153
156
  label: t(`${root}.${key}.choices.${fc.key}.label`, { fallback: true }),
154
157
  help: t(`${root}.${key}.choices.${fc.key}.help`),
158
+ instructions: t(`${root}.${key}.choices.${fc.key}.instructions`, { fallback: true, optional: true }),
155
159
  ui: {
156
160
  ...t(`${root}.${key}.choices.${fc.key}.ui`, { fallback: true, optional: true }) || {},
157
161
  ...fc.group && {
@@ -184,10 +188,12 @@ export const localizeProperties = (
184
188
  element: Action | Trigger | Credentials | Provider | Source,
185
189
  ): LocalizedProperties => {
186
190
  const help = t<Record<string, any>>(`${element.id}.help`, { fallback: true, optional: true });
191
+ const instructions = t<string>(`${element.id}.instructions`, { fallback: true, optional: true });
187
192
  return {
188
193
  name: t(`${element.id}.name`, { fallback: true }),
189
194
  description: t(`${element.id}.description`),
190
195
  ...help && { help: help },
196
+ ...instructions && { instructions: instructions },
191
197
  };
192
198
  };
193
199
 
@@ -459,30 +459,30 @@ export const alter = (field: Field, relative: string, data: Record<string, any>)
459
459
 
460
460
  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 }> => {
461
461
  try {
462
- let newField: Field = _.cloneDeep(field);
462
+ let alteredField: Field = _.cloneDeep(field);
463
463
 
464
464
  // alterations over already resolved data (previous fields)
465
- if (newField.altered_by?.length > 0) {
466
- newField = alter(newField, options.relative, data);
465
+ if (alteredField.altered_by?.length > 0) {
466
+ alteredField = alter(alteredField, options.relative, data);
467
467
 
468
468
  // remove altered_by only if it's not an array -> array alterations must be kept for front-end rendering
469
- if (options.alter) { delete newField.altered_by; }
469
+ if (options.alter) { delete alteredField.altered_by; }
470
470
 
471
- if (typeof newField.show === `boolean` && !newField.show) { return; }
471
+ if (typeof alteredField.show === `boolean` && !alteredField.show) { return; }
472
472
  }
473
473
 
474
474
  const relativeKey = options.relative ?
475
475
  options.relative.endsWith(`]`) ?
476
- options.array ? options.relative : `${options.relative}.${newField.key}`
477
- : `${options.relative}.${newField.key}`
478
- : newField.key;
476
+ options.array ? options.relative : `${options.relative}.${alteredField.key}`
477
+ : `${options.relative}.${alteredField.key}`
478
+ : alteredField.key;
479
479
 
480
- const value = _.get(data, relativeKey, newField.default);
480
+ const value = _.get(data, relativeKey, alteredField.default);
481
481
 
482
482
  // resolve value with context (even if it's not valid)
483
- const resolved = context.resolve(newField, value, options.context);
483
+ const resolved = context.resolve(alteredField, value, options.context);
484
484
  if (!options.no_undefined || typeof resolved !== `undefined`) {
485
- if (newField.type === `any` && typeof resolved === `undefined`) {
485
+ if (alteredField.type === `any` && typeof resolved === `undefined`) {
486
486
  // any field - allow_nullish
487
487
  _.set(data, relativeKey, null);
488
488
  } else {
@@ -495,8 +495,8 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
495
495
 
496
496
  // get schema for field with data
497
497
  let calculatedSchema: Joi.Schema;
498
- if (newField.as_array) {
499
- const noArrayField = _.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]) as Field;
498
+ if (alteredField.as_array) {
499
+ const noArrayField = _.omit(alteredField, [`as_array`, `array_min`, `array_max`, `array_length`]) as Field;
500
500
 
501
501
  if (resolved) {
502
502
  const itemSchemas: Joi.Schema[] = [];
@@ -511,7 +511,7 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
511
511
  _.set(data, relativeKey, withoutUndefined);
512
512
  }
513
513
 
514
- calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
514
+ calculatedSchema = getOrderedArrayFieldSchema(alteredField, itemSchemas);
515
515
  } else {
516
516
  let genericItemSchema: Joi.Schema;
517
517
  if (noArrayField.type === `dict` && noArrayField.items) {
@@ -528,33 +528,33 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
528
528
  genericItemSchema = getSimpleFieldSchema(noArrayField);
529
529
  }
530
530
 
531
- calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
531
+ calculatedSchema = getItemsArrayFieldSchema(alteredField, [genericItemSchema]);
532
532
  }
533
- } else if (newField.type === `dict` && newField.items) {
534
- if (resolved || newField.required) {
533
+ } else if (alteredField.type === `dict` && alteredField.items) {
534
+ if (resolved || alteredField.required) {
535
535
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
536
536
 
537
- for (const item of newField.items) {
537
+ for (const item of alteredField.items) {
538
538
  const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
539
539
  if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
540
540
  }
541
541
 
542
- calculatedSchema = getObjectFieldSchema(newField, items);
543
- newField.items = items.map((i) => i.field);
542
+ calculatedSchema = getObjectFieldSchema(alteredField, items);
543
+ alteredField.items = items.map((i) => i.field);
544
544
  } else {
545
- delete newField.items;
546
- calculatedSchema = getSimpleFieldSchema(newField);
545
+ // omit items to avoid simple field error
546
+ calculatedSchema = getSimpleFieldSchema(_.omit(alteredField, [`items`]) as Field);
547
547
  }
548
548
  } else {
549
- calculatedSchema = getSimpleFieldSchema(newField);
549
+ calculatedSchema = getSimpleFieldSchema(alteredField);
550
550
  }
551
551
 
552
- if (!calculatedSchema) { throw new Error(`Could not resolve schema for field ${newField.key}`); }
552
+ if (!calculatedSchema) { throw new Error(`Could not resolve schema for field ${alteredField.key}`); }
553
553
 
554
554
  // if disabled schema validation is done but the value is not set
555
- if (newField.disabled) { _.unset(data, relativeKey); }
555
+ if (alteredField.disabled) { _.unset(data, relativeKey); }
556
556
 
557
- return { schema: calculatedSchema, field: newField };
557
+ return { schema: calculatedSchema, field: alteredField };
558
558
  } catch (e) { throw e; }
559
559
  };
560
560