-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync.js
54 lines (45 loc) · 1.26 KB
/
async.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
async function vamlid (schema, data) {
const result = {
messages: [],
fields: {}
};
Object
.keys(data)
.filter(key => !schema[key])
.forEach(key => {
result.fields[key] = result.fields[key] || [];
result.fields[key].push('is not a valid key');
result.messages.push(key + ' is not a valid key');
});
const validValues = Object
.keys(schema)
.map(async key => {
const schemaCheckResultPromises = schema[key]
.map(validator => {
return validator(data[key], data);
});
const schemaCheckResults = await Promise.all(schemaCheckResultPromises);
schemaCheckResults
.filter(key => !!key)
.filter(key => {
if (!Array.isArray(key)) {
return true;
}
const arrayIsEmpty = key.filter(item => !!item).length === 0;
if (arrayIsEmpty) {
return false;
}
return true;
})
.forEach(message => {
result.fields[key] = result.fields[key] || [];
result.fields[key].push(message);
});
})
.flat();
await Promise.all(validValues);
if (result.messages.length > 0 || Object.keys(result.fields).length > 0) {
return result;
}
}
export default vamlid;