@beseif-solutions/prow-core 0.2.4 → 0.2.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.
@@ -345,11 +345,10 @@ const getSimpleFieldSchema = (field) => {
345
345
  }
346
346
  return getGeneralSchema(field, typeSchema);
347
347
  };
348
- const getArrayFieldSchema = (field, items) => {
348
+ const getGeneralArraySchema = (field, schema) => {
349
349
  if (!field.as_array) {
350
350
  throw new Error(`as_array required for array schema generator`);
351
351
  }
352
- let schema = joi_1.default.array().ordered(...items);
353
352
  if (field.array_length) {
354
353
  schema = schema.length(field.array_length);
355
354
  }
@@ -361,6 +360,20 @@ const getArrayFieldSchema = (field, items) => {
361
360
  }
362
361
  return getGeneralSchema(field, schema);
363
362
  };
363
+ const getItemsArrayFieldSchema = (field, alternatives) => {
364
+ if (!field.as_array) {
365
+ throw new Error(`as_array required for array schema generator`);
366
+ }
367
+ const schema = joi_1.default.array().items(...alternatives);
368
+ return getGeneralArraySchema(field, schema);
369
+ };
370
+ const getOrderedArrayFieldSchema = (field, items) => {
371
+ if (!field.as_array) {
372
+ throw new Error(`as_array required for array schema generator`);
373
+ }
374
+ const schema = joi_1.default.array().ordered(...items);
375
+ return getGeneralArraySchema(field, schema);
376
+ };
364
377
  const getObjectFieldSchema = (field, items) => {
365
378
  if (field.type !== `dict`) {
366
379
  throw new Error(`type must be dict for object schema generator`);
@@ -443,17 +456,24 @@ const recursive_schema = async (field, data, options) => {
443
456
  const resolved = context_1.default.resolve(newField, value, options.context);
444
457
  lodash_1.default.set(data, relativeKey, resolved);
445
458
  let calculatedSchema;
446
- if (newField.as_array && resolved) {
447
- const itemSchemas = [];
448
- if (lodash_1.default.isArray(resolved)) {
449
- for (let i = 0; i < (resolved || []).length; i++) {
450
- const { schema: itemSchema } = await recursive_schema(lodash_1.default.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]), data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
451
- if (itemSchema) {
452
- itemSchemas.push(itemSchema);
459
+ if (newField.as_array) {
460
+ const noArrayField = lodash_1.default.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]);
461
+ if (resolved) {
462
+ const itemSchemas = [];
463
+ if (lodash_1.default.isArray(resolved)) {
464
+ for (let i = 0; i < (resolved || []).length; i++) {
465
+ const { schema: itemSchema } = await recursive_schema(noArrayField, data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
466
+ if (itemSchema) {
467
+ itemSchemas.push(itemSchema);
468
+ }
453
469
  }
454
470
  }
471
+ calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
472
+ }
473
+ else {
474
+ const genericItemSchema = getSimpleFieldSchema(noArrayField);
475
+ calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
455
476
  }
456
- calculatedSchema = getArrayFieldSchema(newField, itemSchemas);
457
477
  }
458
478
  else if (newField.type === `dict` && newField.items) {
459
479
  const items = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -548,11 +548,9 @@ const getSimpleFieldSchema = (field: Field) => {
548
548
  return getGeneralSchema(field, typeSchema);
549
549
  };
550
550
 
551
- const getArrayFieldSchema = (field: Field, items: Joi.Schema[]) => {
551
+ const getGeneralArraySchema = (field: Field, schema: Joi.ArraySchema) => {
552
552
  if (!field.as_array) { throw new Error(`as_array required for array schema generator`); }
553
553
 
554
- let schema = Joi.array().ordered(...items);
555
-
556
554
  if (field.array_length) { schema = schema.length(field.array_length); }
557
555
  if (field.array_min) { schema = schema.min(field.array_min); }
558
556
  if (field.array_max) { schema = schema.max(field.array_max); }
@@ -560,6 +558,20 @@ const getArrayFieldSchema = (field: Field, items: Joi.Schema[]) => {
560
558
  return getGeneralSchema(field, schema);
561
559
  };
562
560
 
561
+ const getItemsArrayFieldSchema = (field: Field, alternatives: Joi.Schema[]) => {
562
+ if (!field.as_array) { throw new Error(`as_array required for array schema generator`); }
563
+
564
+ const schema = Joi.array().items(...alternatives);
565
+ return getGeneralArraySchema(field, schema);
566
+ };
567
+
568
+ const getOrderedArrayFieldSchema = (field: Field, items: Joi.Schema[]) => {
569
+ if (!field.as_array) { throw new Error(`as_array required for array schema generator`); }
570
+
571
+ const schema = Joi.array().ordered(...items);
572
+ return getGeneralArraySchema(field, schema);
573
+ };
574
+
563
575
  const getObjectFieldSchema = (field: Field, items: { key: string, schema: Joi.Schema }[]) => {
564
576
  if (field.type !== `dict`) { throw new Error(`type must be dict for object schema generator`); }
565
577
  if (!field.items) { throw new Error(`items required for object schema generator`); }
@@ -652,18 +664,23 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
652
664
 
653
665
  // get schema for field with data
654
666
  let calculatedSchema: Joi.Schema;
655
- if (newField.as_array && resolved) {
656
- const itemSchemas: Joi.Schema[] = [];
657
- if (_.isArray(resolved)) {
658
- for (let i = 0; i < (resolved || []).length; i++) {
659
- const { schema: itemSchema } = await recursive_schema(
660
- _.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]) as Field,
661
- data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
662
- if (itemSchema) { itemSchemas.push(itemSchema); }
667
+ if (newField.as_array) {
668
+ const noArrayField = _.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]) as Field;
669
+
670
+ if (resolved) {
671
+ const itemSchemas: Joi.Schema[] = [];
672
+ if (_.isArray(resolved)) {
673
+ for (let i = 0; i < (resolved || []).length; i++) {
674
+ const { schema: itemSchema } = await recursive_schema(noArrayField, data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
675
+ if (itemSchema) { itemSchemas.push(itemSchema); }
676
+ }
663
677
  }
664
- }
665
678
 
666
- calculatedSchema = getArrayFieldSchema(newField, itemSchemas);
679
+ calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
680
+ } else {
681
+ const genericItemSchema = getSimpleFieldSchema(noArrayField);
682
+ calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
683
+ }
667
684
  } else if (newField.type === `dict` && newField.items) {
668
685
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
669
686