@beseif-solutions/prow-core 0.3.0 → 0.3.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.
@@ -3,7 +3,7 @@ import { Core } from "../core";
3
3
  export type CoreConfig = {
4
4
  inputs?: Record<string, any>;
5
5
  credentials?: Record<string, any>;
6
- outputs?: Record<string, any>;
6
+ outputs?: any;
7
7
  };
8
8
  export type CoreFunction<Config extends CoreConfig = {}> = (core: Core, configuration: ({
9
9
  sandbox: boolean;
@@ -1,7 +1,7 @@
1
1
  import Joi from "joi";
2
2
  import { Field } from "../minified-utils/fields";
3
3
  import { Common, CoreConfig, CoreFunction } from "./commons";
4
- import { OneOrArray } from "../minified-utils/types";
4
+ import { ObjectOrArray } from "../minified-utils/types";
5
5
  type CommonConnectableEvent<Keys extends string = string> = {
6
6
  key: Keys;
7
7
  };
@@ -155,7 +155,7 @@ export type ScheduledTrigger<Config extends EventConfig> = CommonEvents<Config>
155
155
  };
156
156
  export type Trigger<Config extends EventConfig = {}> = ImmediateTrigger<Config> | ScheduledTrigger<Config>;
157
157
  type StandardOutput<Config extends EventConfig> = {
158
- output: OneOrArray<Config[`outputs`]>;
158
+ output: ObjectOrArray<Config[`outputs`]>;
159
159
  flags: Partial<Record<Config[`flags`][`outputs`][number], true>>;
160
160
  };
161
161
  type MemoryOutput<Config extends MemoryEventConfig> = {
@@ -38,6 +38,7 @@ type StringInputField<Extend = Record<never, never>> = {
38
38
  max?: number;
39
39
  regexp?: string;
40
40
  pattern?: `email` | `uri`;
41
+ allow_empty?: boolean;
41
42
  } & (SelectField<string, Extend> | CustomDisplayTextField | IDETextField | {});
42
43
  type NumberInputField<Extend = Record<never, never>> = {
43
44
  type: `number`;
@@ -79,6 +79,7 @@ const stringInputFieldSchema = () => joi_1.default.object({
79
79
  max: joi_1.default.number(),
80
80
  regexp: joi_1.default.string(),
81
81
  pattern: joi_1.default.string().valid(`email`, `uri`),
82
+ allow_empty: joi_1.default.bool().default(false),
82
83
  })
83
84
  .concat(joi_1.default.object({
84
85
  format: joi_1.default.string().valid(`select`, `textarea`, `rich-text`, `markdown`, `code`),
@@ -164,6 +165,7 @@ const fileSchema = () => joi_1.default.object({
164
165
  const fileReplacer = (field) => ({
165
166
  key: field.key,
166
167
  type: `dict`,
168
+ unknown: true,
167
169
  items: [
168
170
  {
169
171
  key: `name`,
@@ -256,10 +258,10 @@ const getSimpleFieldSchema = (field) => {
256
258
  if (field.length) {
257
259
  typeSchema = typeSchema.length(field.length);
258
260
  }
259
- if (field.min) {
261
+ if (field.min > 0) {
260
262
  typeSchema = typeSchema.min(field.min);
261
263
  }
262
- else {
264
+ else if (field.allow_empty) {
263
265
  typeSchema = typeSchema.allow(``);
264
266
  }
265
267
  if (field.max) {
@@ -474,12 +476,10 @@ const recursive_schema = async (field, data, options) => {
474
476
  let genericItemSchema;
475
477
  if (noArrayField.type === `dict` && noArrayField.items) {
476
478
  const items = [];
477
- if (lodash_1.default.isObject(resolved)) {
478
- for (const item of noArrayField.items) {
479
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
480
- if (recursive) {
481
- items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
482
- }
479
+ for (const item of noArrayField.items) {
480
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
481
+ if (recursive) {
482
+ items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
483
483
  }
484
484
  }
485
485
  genericItemSchema = getObjectFieldSchema(noArrayField, items);
@@ -489,16 +489,15 @@ const recursive_schema = async (field, data, options) => {
489
489
  genericItemSchema = getSimpleFieldSchema(noArrayField);
490
490
  }
491
491
  calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
492
+ lodash_1.default.unset(data, relativeKey);
492
493
  }
493
494
  }
494
495
  else if (newField.type === `dict` && newField.items) {
495
496
  const items = [];
496
- if (lodash_1.default.isObject(resolved)) {
497
- for (const item of newField.items) {
498
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
499
- if (recursive) {
500
- items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
501
- }
497
+ for (const item of newField.items) {
498
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
499
+ if (recursive) {
500
+ items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
502
501
  }
503
502
  }
504
503
  calculatedSchema = getObjectFieldSchema(newField, items);
@@ -9,4 +9,4 @@ export type DeepPartial<T extends Record<string, any>> = {
9
9
  [P in keyof T]?: T[P] extends Record<string, any> ? DeepPartial<T[P]> : T[P];
10
10
  };
11
11
  export type AsyncReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => Promise<infer U> ? U : T extends (...args: any[]) => infer U ? U : any;
12
- export type OneOrArray<T> = T | T[];
12
+ export type ObjectOrArray<T> = T extends any[] ? T : T extends Record<string, any> ? T | T[] : T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -4,7 +4,7 @@ import { Core } from "../core";
4
4
  export type CoreConfig = {
5
5
  inputs?: Record<string, any>,
6
6
  credentials?: Record<string, any>,
7
- outputs?: Record<string, any>,
7
+ outputs?: any,
8
8
  };
9
9
 
10
10
  export type CoreFunction<Config extends CoreConfig = {}> = (core: Core, configuration: ({
@@ -1,7 +1,7 @@
1
1
  import Joi from "joi";
2
2
  import { Field, fieldsSchema } from "../minified-utils/fields";
3
3
  import { Common, commonSchema, CoreConfig, CoreFunction, idSchema } from "./commons";
4
- import { OneOrArray } from "../minified-utils/types";
4
+ import { ObjectOrArray } from "../minified-utils/types";
5
5
 
6
6
  type CommonConnectableEvent<Keys extends string = string> = {
7
7
  key: Keys,
@@ -125,7 +125,7 @@ export type Trigger<Config extends EventConfig = {}> =
125
125
  | ScheduledTrigger<Config>;
126
126
 
127
127
  type StandardOutput<Config extends EventConfig> =
128
- { output: OneOrArray<Config[`outputs`]>, flags: Partial<Record<Config[`flags`][`outputs`][number], true>> };
128
+ { output: ObjectOrArray<Config[`outputs`]>, flags: Partial<Record<Config[`flags`][`outputs`][number], true>> };
129
129
 
130
130
  type MemoryOutput<Config extends MemoryEventConfig> = {
131
131
  memory: Config[`memory`],
@@ -59,6 +59,7 @@ type StringInputField<Extend = Record<never, never>> = {
59
59
  max?: number,
60
60
  regexp?: string,
61
61
  pattern?: `email` | `uri`,
62
+ allow_empty?: boolean,
62
63
  } & (
63
64
  | SelectField<string, Extend>
64
65
  | CustomDisplayTextField
@@ -275,6 +276,7 @@ const stringInputFieldSchema = () =>
275
276
  max: Joi.number(),
276
277
  regexp: Joi.string(),
277
278
  pattern: Joi.string().valid(`email`, `uri`),
279
+ allow_empty: Joi.bool().default(false),
278
280
  })
279
281
  .concat(
280
282
  Joi.object({
@@ -375,6 +377,7 @@ const fileSchema = () => Joi.object({
375
377
  const fileReplacer = (field: Common & FileInputField): Field => ({
376
378
  key: field.key,
377
379
  type: `dict`,
380
+ unknown: true,
378
381
  items: [
379
382
  {
380
383
  key: `name`,
@@ -474,9 +477,9 @@ const getSimpleFieldSchema = (field: Field) => {
474
477
  case `string`:
475
478
  typeSchema = Joi.string();
476
479
  if (field.length) { typeSchema = (typeSchema as StringSchema).length(field.length); }
477
- if (field.min) {
480
+ if (field.min > 0) {
478
481
  typeSchema = (typeSchema as StringSchema).min(field.min);
479
- } else {
482
+ } else if (field.allow_empty) {
480
483
  typeSchema = typeSchema.allow(``);
481
484
  }
482
485
  if (field.max) { typeSchema = (typeSchema as StringSchema).max(field.max); }
@@ -682,11 +685,9 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
682
685
  if (noArrayField.type === `dict` && noArrayField.items) {
683
686
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
684
687
 
685
- if (_.isObject(resolved)) {
686
- for (const item of noArrayField.items) {
687
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
688
- if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
689
- }
688
+ for (const item of noArrayField.items) {
689
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
690
+ if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
690
691
  }
691
692
 
692
693
  genericItemSchema = getObjectFieldSchema(noArrayField, items);
@@ -696,15 +697,16 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
696
697
  }
697
698
 
698
699
  calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
700
+
701
+ // unset value if it wasn't resolved
702
+ _.unset(data, relativeKey);
699
703
  }
700
704
  } else if (newField.type === `dict` && newField.items) {
701
705
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
702
706
 
703
- if (_.isObject(resolved)) {
704
- for (const item of newField.items) {
705
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
706
- if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
707
- }
707
+ for (const item of newField.items) {
708
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
709
+ if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
708
710
  }
709
711
 
710
712
  calculatedSchema = getObjectFieldSchema(newField, items);
@@ -17,4 +17,7 @@ export type AsyncReturnType<T extends (...args: any[]) => any> =
17
17
  T extends (...args: any[]) => infer U ? U :
18
18
  any;
19
19
 
20
- export type OneOrArray<T> = T | T[];
20
+ export type ObjectOrArray<T> =
21
+ T extends any[] ? T
22
+ : T extends Record<string, any> ? T | T[]
23
+ : T[];