forked from ProtoDef-io/node-protodef-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (119 loc) · 4.11 KB
/
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
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
const Ajv = require('ajv');
const assert=require("assert");
class Validator {
constructor(typesSchemas) {
this.createAjvInstance(typesSchemas);
this.addDefaultTypes();
}
createAjvInstance(typesSchemas) {
this.typesSchemas = {};
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) {
const schemaName=this.typeToSchemaName(name);
if(this.typesSchemas[schemaName] != undefined)
return;
if(!schema) { // default schema
schema={
"oneOf":[
{"enum":[name]},
{
"type": "array",
"items": [
{"enum":[name]},
{"oneOf":[{"type": "object"},{"type": "array"}]}
]
}
]};
}
this.typesSchemas[schemaName]=schema;
// 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.ajv.removeSchema("dataType");
this.ajv.addSchema({
"title": "dataType",
"oneOf": [{"enum":["native"]}].concat(Object.keys(this.typesSchemas).map(name => ({"$ref": this.typeToSchemaName(name)})))
},"dataType");
}
validateType(type) {
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
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;