This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseComponentsObject.js
74 lines (61 loc) · 2.63 KB
/
parseComponentsObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const R = require('ramda');
const {
isObject, isAnnotation, hasKey, isExtension, getValue,
} = require('../../predicates');
const {
createWarning,
createUnsupportedMemberWarning,
createInvalidMemberWarning,
} = require('../annotations');
const parseObject = require('../parseObject');
const pipeParseResult = require('../../pipeParseResult');
const parseSchemaObject = require('./parseSchemaObject');
const parseParameterObject = require('./parseParameterObject');
const name = 'Components Object';
const unsupportedKeys = ['responses', 'examples', 'requestBodies', 'headers', 'securitySchemes', 'links', 'callbacks'];
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));
/**
* Parse Components Object
*
* @param namespace {Namespace}
* @param element {Element}
* @returns ParseResult
*
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#componentsObject
*/
function parseComponentsObject(context, element) {
const { namespace } = context;
const validateIsObject = key => R.unless(isObject,
createWarning(namespace, `'${name}' '${key}' is not an object`));
const parseSchemaMember = member => pipeParseResult(namespace,
R.compose(parseSchemaObject(context), getValue),
(dataStructure) => {
// eslint-disable-next-line no-param-reassign
dataStructure.content.id = member.key.clone();
return dataStructure;
})(member);
const parseSchemasObject = pipeParseResult(namespace,
validateIsObject('schemas'),
parseObject(context, name, parseSchemaMember));
const parseParametersObjectMember = (member) => {
// Create a Member Element with `member.key` as the key
const Member = R.constructN(2, namespace.elements.Member)(member.key);
const parseResult = parseParameterObject(context, member.value);
// Wrap non-annotation elements in member element
return R.map(R.unless(isAnnotation, Member), parseResult);
};
const parseParametersObject = pipeParseResult(namespace,
validateIsObject('parameters'),
parseObject(context, name, parseParametersObjectMember));
const parseMember = R.cond([
[hasKey('schemas'), R.compose(parseSchemasObject, getValue)],
[hasKey('parameters'), R.compose(parseParametersObject, getValue)],
[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
// FIXME Support exposing extensions into parse result
[isExtension, () => new namespace.elements.ParseResult()],
// Return a warning for additional properties
[R.T, createInvalidMemberWarning(namespace, name)],
]);
return parseObject(context, name, parseMember, element);
}
module.exports = R.curry(parseComponentsObject);