-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathresolveDependencies.js
64 lines (45 loc) · 1.9 KB
/
resolveDependencies.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
module.exports = resolveSchemaDependencies;
function resolveSchemaDependencies(schema, doc) {
const dependencies = schema.dependentSchemas || schema.dependencies;
const flatSchema = Object.create(schema);
if (dependencies) {
Object.keys(dependencies).forEach(dep => {
if (dependencies[dep].oneOf) {
dependencies[dep].oneOf.forEach(subschema => {
let propertyExists = false;
if (doc) {
const dependencyInObject = doc[dep];
const depValue = subschema.properties[dep].enum[0];
if (dependencyInObject !== depValue) return;
propertyExists = true;
}
Object.keys(subschema.properties).forEach(key => {
if (key !== dep) {
flatSchema.properties[key] = subschema.properties[key];
const isRequired = subschema.required && subschema.required.length && subschema.required.includes(key)
if (propertyExists && isRequired) {
if (!flatSchema.required) flatSchema.required = [];
if (flatSchema.required.includes(key)) return;
flatSchema.required.push(key);
}
}
})
})
} else {
if (!dependencies[dep].properties) return;
Object.keys(dependencies[dep].properties).forEach(key => {
const isRequired = dependencies[dep].required && dependencies[dep].required.length && dependencies[dep].required.includes(key);
flatSchema.properties[key] = dependencies[dep].properties[key];
if (isRequired && doc) {
const dependencyInObject = doc[dep];
if (dependencyInObject === undefined) return;
if (!flatSchema.required) flatSchema.required = [];
if (flatSchema.required.includes(key)) return;
flatSchema.required.push(key);
}
})
}
})
};
return flatSchema;
};