-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (67 loc) · 2.06 KB
/
index.js
File metadata and controls
78 lines (67 loc) · 2.06 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function(){
var fs = require('fs');
var path = require('path');
var JsonSchema = require('jsonschema');
var validator = new JsonSchema.Validator();
var baseSchemaFile = 'Schema.json';
var rootDir = __dirname;
var definitionsDir = path.join(rootDir, 'definitions');
var entities = fs.readdirSync(definitionsDir);
var schemaPattern = /\.json$/;
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
if (schemaPattern.test(entity)) {
var json = fs.readFileSync(path.join(definitionsDir, entity));
validator.addSchema(JSON.parse(json));
}
}
var schemaJson = fs.readFileSync(path.join(rootDir, baseSchemaFile));
var schema = JSON.parse(schemaJson);
module.exports = {
validateSchema: function validate(sceneDto) {
return validator.validate(sceneDto, schema);
},
validateHierarchy: function validate(sceneDto) {
var map = new Map();
for (let i = 0; i < sceneDto.scene.length; i++) {
var entity = sceneDto.scene[i];
map.set(entity.id, entity);
}
var validity = true;
map.forEach(function(entity, id){
if (!validity) {
return;
}
var parentId = entity.transform.parent;
var childrenIds = entity.transform.children;
// check parent relation
if (parentId) {
var parent = map.get(parentId);
if (!parent) {
validity = false;
return;
}
if (!parent.transform.children || parent.transform.children.indexOf(id) === -1) {
validity = false;
return;
}
}
// check children relation
if (childrenIds) {
for (var i = 0; i < childrenIds.length; i++) {
var child = map.get(childrenIds[i]);
if (!child) {
validity = false;
return;
}
if (child.transform.parent !== id) {
validity = false;
return;
}
}
}
});
return validity;
},
};
}());