forked from Language-Research-Technology/ldac-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
37 lines (34 loc) · 881 Bytes
/
validator.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
const levels = {
error: 'errors',
warn: 'warnings',
info: 'info'
};
function validateEntity(ruleSet, entity, crate, res = {}) {
const wrapper = { results: res };
for (const key in levels) {
if (!res[levels[key]]) res[levels[key]] = [];
}
for (let name in ruleSet) {
const clause = ruleSet[name].clause;
for (const key in levels) {
wrapper[key] = function (message = clause) {
res[levels[key]].push({
entity: entity['@id'],
property: name,
message,
clause
});
};
}
ruleSet[name].validate(wrapper, entity[name], entity, crate);
}
return res;
};
function resolveType(types, entity) {
if (!entity['@type']) return;
const typeset = new Set(types);
for (const t of entity['@type']) {
if (typeset.has(t)) return t;
}
}
module.exports = {validateEntity, resolveType};