-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy patharray.js
More file actions
32 lines (26 loc) · 957 Bytes
/
array.js
File metadata and controls
32 lines (26 loc) · 957 Bytes
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
import { traverse } from '../traverse';
export function sampleArray(schema, options = {}, spec, context) {
const depth = (context && context.depth || 1);
let arrayLength = Math.min(schema.maxItems != undefined ? schema.maxItems : Infinity, schema.minItems || 1);
// for the sake of simplicity, we're treating `contains` in a similar way to `items`
const items = schema.items || schema.contains;
if (Array.isArray(items)) {
arrayLength = Math.max(arrayLength, items.length);
}
const itemSchemaGetter = itemNumber => {
if (Array.isArray(schema.items)) {
return items[itemNumber] || {};
}
return items || {};
};
let res = [];
if (!items) return res;
for (let i = 0; i < arrayLength; i++) {
let { value: sample } = traverse(itemSchemaGetter(i), options, spec, {depth: depth + 1});
res.push(sample);
}
if (!options.omissible || res.some(item => item !== null)) {
return res;
}
return null;
}