/
object.js
34 lines (30 loc) · 1.12 KB
/
object.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
import { traverse } from '../traverse';
export function sampleObject(schema, options = {}, spec) {
let res = {};
if (schema && typeof schema.properties === 'object') {
let requiredKeys = (Array.isArray(schema.required) ? schema.required : []);
let requiredKeyDict = requiredKeys.reduce((dict, key) => {
dict[key] = true;
return dict;
}, {});
Object.keys(schema.properties).forEach(propertyName => {
// skip before traverse that could be costly
if (options.skipNonRequired && !requiredKeyDict.hasOwnProperty(propertyName)) {
return;
}
const sample = traverse(schema.properties[propertyName], options, spec, { propertyName });
if (options.skipReadOnly && sample.readOnly) {
return;
}
if (options.skipWriteOnly && sample.writeOnly) {
return;
}
res[propertyName] = sample.value;
});
}
if (schema && typeof schema.additionalProperties === 'object') {
res.property1 = traverse(schema.additionalProperties, options, spec).value;
res.property2 = traverse(schema.additionalProperties, options, spec).value;
}
return res;
}