• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mokkabonna / json-schema-merge-allof / 7569508346

18 Jan 2024 11:48AM UTC coverage: 97.43% (-0.1%) from 97.566%
7569508346

push

github

mokkabonna
Extract oneOf and anyOf tests, make them unresolvable

178 of 186 branches covered (0.0%)

Branch coverage included in aggregate %.

656 of 670 relevant lines covered (97.91%)

68.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.35
/src/complex-resolvers/items.js
1
const compare = require('json-schema-compare');
10✔
2
const forEach = require('lodash/forEach');
10✔
3
const {
10✔
4
  allUniqueKeys,
10✔
5
  deleteUndefinedProps,
10✔
6
  has,
10✔
7
  isSchema,
10✔
8
  notUndefined,
10✔
9
  uniqWith
10✔
10
} = require('../common');
10✔
11

10✔
12
function removeFalseSchemasFromArray(target) {
2✔
13
  forEach(target, function (schema, index) {
2✔
14
    if (schema === false) {
4✔
15
      target.splice(index, 1);
2✔
16
    }
2✔
17
  });
2✔
18
}
2✔
19

10✔
20
function getItemSchemas(subSchemas, key) {
15✔
21
  return subSchemas.map(function (sub) {
15✔
22
    if (!sub) {
43!
23
      return undefined;
×
24
    }
×
25

43✔
26
    if (Array.isArray(sub.items)) {
43✔
27
      const schemaAtPos = sub.items[key];
25✔
28
      if (isSchema(schemaAtPos)) {
25✔
29
        return schemaAtPos;
15✔
30
      } else if (has(sub, 'additionalItems')) {
25✔
31
        return sub.additionalItems;
9✔
32
      }
9✔
33
    } else {
43✔
34
      return sub.items;
18✔
35
    }
18✔
36

1✔
37
    return undefined;
1✔
38
  });
15✔
39
}
15✔
40

10✔
41
function getAdditionalSchemas(subSchemas) {
2✔
42
  return subSchemas.map(function (sub) {
2✔
43
    if (!sub) {
6!
44
      return undefined;
×
45
    }
×
46
    if (Array.isArray(sub.items)) {
6✔
47
      return sub.additionalItems;
3✔
48
    }
3✔
49
    return sub.items;
3✔
50
  });
2✔
51
}
2✔
52

10✔
53
// Provide source when array
10✔
54
function mergeItems(group, mergeSchemas, items) {
6✔
55
  const allKeys = allUniqueKeys(items);
6✔
56
  return allKeys.reduce(function (all, key) {
6✔
57
    const schemas = getItemSchemas(group, key);
15✔
58
    const compacted = uniqWith(schemas.filter(notUndefined), compare);
15✔
59
    all[key] = mergeSchemas(compacted, key);
15✔
60
    return all;
15✔
61
  }, []);
6✔
62
}
6✔
63

10✔
64
module.exports = {
10✔
65
  keywords: ['items', 'additionalItems'],
10✔
66
  resolver(values, parents, mergers) {
10✔
67
    // const createSubMerger = groupKey => (schemas, key) => mergeSchemas(schemas, parents.concat(groupKey, key))
12✔
68
    const items = values.map((s) => s.items);
12✔
69
    const itemsCompacted = items.filter(notUndefined);
12✔
70
    const returnObject = {};
12✔
71

12✔
72
    // if all items keyword values are schemas, we can merge them as simple schemas
12✔
73
    // if not we need to merge them as mixed
12✔
74
    if (itemsCompacted.every(isSchema)) {
12✔
75
      returnObject.items = mergers.items(items);
6✔
76
    } else {
6✔
77
      returnObject.items = mergeItems(values, mergers.items, items);
6✔
78
    }
6✔
79

12✔
80
    let schemasAtLastPos;
12✔
81
    if (itemsCompacted.every(Array.isArray)) {
12✔
82
      schemasAtLastPos = values.map((s) => s.additionalItems);
4✔
83
    } else if (itemsCompacted.some(Array.isArray)) {
12✔
84
      schemasAtLastPos = getAdditionalSchemas(values);
2✔
85
    }
2✔
86

12✔
87
    if (schemasAtLastPos) {
12✔
88
      returnObject.additionalItems = mergers.additionalItems(schemasAtLastPos);
6✔
89
    }
6✔
90

12✔
91
    if (
12✔
92
      returnObject.additionalItems === false &&
12✔
93
      Array.isArray(returnObject.items)
2✔
94
    ) {
12✔
95
      removeFalseSchemasFromArray(returnObject.items);
2✔
96
    }
2✔
97

12✔
98
    return deleteUndefinedProps(returnObject);
12✔
99
  }
12✔
100
};
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc