-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (139 loc) · 5.15 KB
/
Copy pathindex.js
File metadata and controls
157 lines (139 loc) · 5.15 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const Ajv = require('ajv');
const assert=require("assert");
class Validator {
constructor(typesSchemas) {
this.createAjvInstance(typesSchemas);
this.addDefaultTypes();
}
createAjvInstance(typesSchemas) {
this.typesSchemas = {};
this.customTypeNames = new Set(); // schemaName of types with a real schema
this.defaultTypeNames = new Set(); // original name of default-schema types
this.dataTypeDirty = false;
this.compiled=false;
this.ajv = new Ajv({verbose:true});
this.ajv.addSchema(require("./ProtoDef/schemas/definitions.json"),"definitions");
this.ajv.addSchema(require("./ProtoDef/schemas/protocol_schema.json"),"protocol");
if(typesSchemas) {
Object.keys(typesSchemas).forEach(s => this.addType(s, typesSchemas[s]));
}
}
addDefaultTypes() {
this.addTypes(require("./ProtoDef/schemas/numeric.json"));
this.addTypes(require("./ProtoDef/schemas/utils.json"));
this.addTypes(require("./ProtoDef/schemas/structures.json"));
this.addTypes(require("./ProtoDef/schemas/conditional.json"));
this.addTypes(require("./ProtoDef/schemas/primitives.json"));
}
addTypes(schemas) {
Object.keys(schemas).forEach((name) => this.addType(name, schemas[name]));
}
typeToSchemaName(name) {
return name.replace('|','_');
}
addType(name,schema_arg) {
const schemaName=this.typeToSchemaName(name);
if(this.typesSchemas[schemaName] != undefined)
return;
let schema = schema_arg;
if(!schema) { // default schema
schema={
"oneOf":[
{"enum":[name]},
{
"type": "array",
"items": [
{"enum":[name]},
{"oneOf":[{"type": "object"},{"type": "array"}]}
]
}
]};
}
this.typesSchemas[schemaName]=schema;
if(schema_arg) this.customTypeNames.add(schemaName);
else this.defaultTypeNames.add(name);
// recreate ajv instance to recompile dataType (and all depending types) when adding a type
if(this.compiled)
this.createAjvInstance(this.typesSchemas);
else {
this.ajv.addSchema(schema, schemaName);
}
this.dataTypeDirty = true;
}
// dataType used to be a oneOf with one branch per known type name. All
// default-schema branches are identical apart from the name, so they collapse
// into two discriminating branches (bare name / [name, data] pair); types
// with a real schema keep their individual $ref branch. Rebuilt lazily so
// registering N types compiles it once instead of N times.
rebuildDataType() {
if(!this.dataTypeDirty) return;
this.dataTypeDirty = false;
const defaults=[...this.defaultTypeNames];
const branches=[{"enum":["native"].concat(defaults)}];
if(defaults.length)
branches.push({"type":"array","items":[{"enum":defaults},{"oneOf":[{"type":"object"},{"type":"array"}]}]});
for(const name of this.customTypeNames) branches.push({"$ref":name});
this.ajv.removeSchema("dataType");
this.ajv.addSchema({"title":"dataType","oneOf":branches},"dataType");
}
validateType(type) {
this.rebuildDataType();
let valid = this.ajv.validate("dataType",type);
this.compiled=true;
if(!valid) {
console.log(JSON.stringify(this.ajv.errors[0],null,2));
if(this.ajv.errors[0]['parentSchema']['title']=="dataType") {
this.validateTypeGoingInside(this.ajv.errors[0]['data']);
}
throw new Error("validation error");
}
}
validateTypeGoingInside(type) {
if(Array.isArray(type)) {
assert.ok(this.typesSchemas[this.typeToSchemaName(type[0])]!=undefined,type+" is an undefined type");
let valid = this.ajv.validate(type[0],type);
this.compiled=true;
if(!valid) {
console.log(JSON.stringify(this.ajv.errors[0],null,2));
if(this.ajv.errors[0]['parentSchema']['title']=="dataType") {
this.validateTypeGoingInside(this.ajv.errors[0]['data']);
}
throw new Error("validation error");
}
}
else {
if(type=="native")
return;
assert.ok(this.typesSchemas[this.typeToSchemaName(type)]!=undefined,type+" is an undefined type");
}
}
validateProtocol(protocol) {
// 1. validate with protocol schema with basic datatype def
this.rebuildDataType();
let valid = this.ajv.validate("protocol",protocol);
assert.ok(valid, JSON.stringify(this.ajv.errors,null,2));
// 2. recursively create several validator from current one and validate that
function validateTypes(p,originalValidator,path) {
const v=new Validator(originalValidator.typesSchemas);
Object.keys(p).forEach(k => {
if(k=="types") {
// 2 steps for recursive types
Object.keys(p[k]).forEach(typeName => v.addType(typeName));
Object.keys(p[k]).forEach(typeName => {
try {
v.validateType(p[k][typeName], path + "." + k + "." + typeName);
}
catch(e) {
throw new Error("Error at "+path + "." + k + "." + typeName);
}
});
}
else {
validateTypes(p[k],v,path+"."+k);
}
})
}
validateTypes(protocol,this,"root");
}
}
module.exports=Validator;