@beseif-solutions/prow-core 0.2.3 → 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.
@@ -56,7 +56,11 @@ const defaultSchema = (type) => joi_1.default.when(`as_array`, {
56
56
  then: joi_1.default.array().items(type),
57
57
  otherwise: type,
58
58
  });
59
- const anySchema = () => joi_1.default.alternatives(joi_1.default.string(), joi_1.default.bool(), joi_1.default.number(), joi_1.default.array().items(joi_1.default.link(`/`)), joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.link(`/`)));
59
+ const anySchema = () => {
60
+ const randId = Math.random().toString(36).substring(2, 15);
61
+ return joi_1.default.alternatives(joi_1.default.string(), joi_1.default.bool(), joi_1.default.number(), joi_1.default.array().items(joi_1.default.link(`#${randId}`)), joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.link(`#${randId}`)))
62
+ .id(randId);
63
+ };
60
64
  const anyInputFieldSchema = () => joi_1.default.object({
61
65
  default: defaultSchema(anySchema()),
62
66
  })
@@ -341,11 +345,10 @@ const getSimpleFieldSchema = (field) => {
341
345
  }
342
346
  return getGeneralSchema(field, typeSchema);
343
347
  };
344
- const getArrayFieldSchema = (field, items) => {
348
+ const getGeneralArraySchema = (field, schema) => {
345
349
  if (!field.as_array) {
346
350
  throw new Error(`as_array required for array schema generator`);
347
351
  }
348
- let schema = joi_1.default.array().ordered(...items);
349
352
  if (field.array_length) {
350
353
  schema = schema.length(field.array_length);
351
354
  }
@@ -357,6 +360,20 @@ const getArrayFieldSchema = (field, items) => {
357
360
  }
358
361
  return getGeneralSchema(field, schema);
359
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
+ };
360
377
  const getObjectFieldSchema = (field, items) => {
361
378
  if (field.type !== `dict`) {
362
379
  throw new Error(`type must be dict for object schema generator`);
@@ -439,17 +456,24 @@ const recursive_schema = async (field, data, options) => {
439
456
  const resolved = context_1.default.resolve(newField, value, options.context);
440
457
  lodash_1.default.set(data, relativeKey, resolved);
441
458
  let calculatedSchema;
442
- if (newField.as_array && resolved) {
443
- const itemSchemas = [];
444
- if (lodash_1.default.isArray(resolved)) {
445
- for (let i = 0; i < (resolved || []).length; i++) {
446
- 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 });
447
- if (itemSchema) {
448
- 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
+ }
449
469
  }
450
470
  }
471
+ calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
472
+ }
473
+ else {
474
+ const genericItemSchema = getSimpleFieldSchema(noArrayField);
475
+ calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
451
476
  }
452
- calculatedSchema = getArrayFieldSchema(newField, itemSchemas);
453
477
  }
454
478
  else if (newField.type === `dict` && newField.items) {
455
479
  const items = [];
@@ -502,11 +526,17 @@ const validate_internal = async (fields, data, options) => {
502
526
  if (error) {
503
527
  extended = {
504
528
  valid: false,
505
- errors: error.details.map((d) => ({
529
+ errors: error.details ? error.details.map((d) => ({
506
530
  type: d.type,
507
531
  path: convertPath([field.key, ...d.path]),
508
532
  message: d.message,
509
- })),
533
+ })) : [
534
+ {
535
+ type: `unknown`,
536
+ path: field.key,
537
+ message: error.message,
538
+ },
539
+ ],
510
540
  original: value,
511
541
  value: null,
512
542
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -242,13 +242,17 @@ const defaultSchema = (type: Joi.Schema) =>
242
242
  otherwise: type,
243
243
  });
244
244
 
245
- const anySchema = () =>
246
- Joi.alternatives(
245
+ const anySchema = () => {
246
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
247
+ const randId = Math.random().toString(36).substring(2, 15);
248
+ return Joi.alternatives(
247
249
  Joi.string(),
248
250
  Joi.bool(),
249
251
  Joi.number(),
250
- Joi.array().items(Joi.link(`/`)),
251
- Joi.object().pattern(Joi.string(), Joi.link(`/`)));
252
+ Joi.array().items(Joi.link(`#${randId}`)),
253
+ Joi.object().pattern(Joi.string(), Joi.link(`#${randId}`)))
254
+ .id(randId);
255
+ };
252
256
 
253
257
  const anyInputFieldSchema = () =>
254
258
  Joi.object({
@@ -544,11 +548,9 @@ const getSimpleFieldSchema = (field: Field) => {
544
548
  return getGeneralSchema(field, typeSchema);
545
549
  };
546
550
 
547
- const getArrayFieldSchema = (field: Field, items: Joi.Schema[]) => {
551
+ const getGeneralArraySchema = (field: Field, schema: Joi.ArraySchema) => {
548
552
  if (!field.as_array) { throw new Error(`as_array required for array schema generator`); }
549
553
 
550
- let schema = Joi.array().ordered(...items);
551
-
552
554
  if (field.array_length) { schema = schema.length(field.array_length); }
553
555
  if (field.array_min) { schema = schema.min(field.array_min); }
554
556
  if (field.array_max) { schema = schema.max(field.array_max); }
@@ -556,6 +558,20 @@ const getArrayFieldSchema = (field: Field, items: Joi.Schema[]) => {
556
558
  return getGeneralSchema(field, schema);
557
559
  };
558
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
+
559
575
  const getObjectFieldSchema = (field: Field, items: { key: string, schema: Joi.Schema }[]) => {
560
576
  if (field.type !== `dict`) { throw new Error(`type must be dict for object schema generator`); }
561
577
  if (!field.items) { throw new Error(`items required for object schema generator`); }
@@ -648,18 +664,23 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
648
664
 
649
665
  // get schema for field with data
650
666
  let calculatedSchema: Joi.Schema;
651
- if (newField.as_array && resolved) {
652
- const itemSchemas: Joi.Schema[] = [];
653
- if (_.isArray(resolved)) {
654
- for (let i = 0; i < (resolved || []).length; i++) {
655
- const { schema: itemSchema } = await recursive_schema(
656
- _.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]) as Field,
657
- data, { alter: false, relative: `${relativeKey}[${i}]`, array: true, context: options.context });
658
- 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
+ }
659
677
  }
660
- }
661
678
 
662
- calculatedSchema = getArrayFieldSchema(newField, itemSchemas);
679
+ calculatedSchema = getOrderedArrayFieldSchema(newField, itemSchemas);
680
+ } else {
681
+ const genericItemSchema = getSimpleFieldSchema(noArrayField);
682
+ calculatedSchema = getItemsArrayFieldSchema(newField, [genericItemSchema]);
683
+ }
663
684
  } else if (newField.type === `dict` && newField.items) {
664
685
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
665
686
 
@@ -710,11 +731,17 @@ const validate_internal = async (fields: Field[], data: Record<string, any>, opt
710
731
  if (error) {
711
732
  extended = {
712
733
  valid: false,
713
- errors: error.details.map((d) => ({
734
+ errors: error.details ? error.details.map((d) => ({
714
735
  type: d.type,
715
736
  path: convertPath([field.key, ...d.path]),
716
737
  message: d.message,
717
- })),
738
+ })) : [
739
+ {
740
+ type: `unknown`,
741
+ path: field.key,
742
+ message: error.message,
743
+ },
744
+ ],
718
745
  original: value,
719
746
  value: null,
720
747
  };