-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (33 loc) · 1011 Bytes
/
index.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
const generators = require("./modules");
const options = {
useFaker: false,
count: 10,
};
module.exports.generate = function (schema, count = 10, useFaker = false) {
options.useFaker = useFaker;
options.count = count;
// "userName" : {schema: {type: "string", rules:[{name:"max", args: {limit: 5}}]}}
const joiFields = schema._ids._byKey;
const outputSchema = {};
const output = [];
joiFields.forEach((info, name) => {
outputSchema[name] = {
type: info.schema.type,
rules: info.schema._rules,
};
});
for (i = 0; i < options.count; i++) {
output.push(generateSample(outputSchema));
}
return output;
};
function generateSample(outputSchema) {
const outputFields = Object.entries(outputSchema);
const result = {};
// field -> ["userName",{type: "string", rules: [{name:"max", args: {limit: 5}}]}]
outputFields.forEach(
([name, info]) =>
(result[name] = generators[info.type](name, info.rules, options.useFaker))
);
return result;
}