-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_schema.js
More file actions
312 lines (269 loc) · 10.5 KB
/
validate_schema.js
File metadata and controls
312 lines (269 loc) · 10.5 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
module.exports.validator = validateSchema;
var _ = require('underscore');
var validTypes = ["string", "text", /*"integer",*/ "float", /*"date", "time",*/ "datetime", "boolean", "binary", "point"];
module.exports.validTypes = validTypes;
// schema - JSON representing schema
// return object with fields
// valid - boolean
// warnings - array of strings
// var v = validateSchema([
// {
// "name": "r",
// "fields": {
// "name": {
// "type": "string"
// },
// "location": {
// "type": "point",
// "defaultValue": [34.0, 3]
// },
// }
// },
// { name: '_User',
// fields:
// { email: { type: 'string' },
// emailVerified: { type: 'boolean' },
// password: { type: 'string' },
// username: { type: 'string' },
// _Role_users: { object: '_Role', rename: 'kuku' } } },
// { name: '_Role',
// fields:
// { name: { type: 'string' },
// roles: { collection: '_Role', via: '_Role_roles' },
// users: { collection: '_User', via: '_Role_users' },
// _Role_roles: { object: '_Role' } } }
// ]);
// console.log(v);
function validateSchema(schema){
try{
if (!Array.isArray(schema)){
return { valid: false, warnings: ["should be array of table definitions"]};
}
var relationships = [];
var result = _.reduce(schema, function(memo, value){
var currentResult = validRelation(value);
memo.warnings.push(currentResult.warnings);
var relationshipsCheck = getRelationships(value);
relationships.push(relationshipsCheck.relationships);
memo.warnings.push(relationshipsCheck.warnings);
return { valid: memo.valid && currentResult.valid && relationshipsCheck.valid, warnings: _.flatten(memo.warnings) };
}, { valid: true, warnings: [] });
var relationships = _.flatten(relationships);
// validate the two sides of a relationship
_.each(relationships,
function(r){
if (r.type == "n"){
var fn = _.filter(relationships, function(o){
return r.collection == o.relation && o.collection == r.relation && o.via == r.attribute && r.via == o.attribute && o.type == "n";
});
var otherSideM = fn.length > 0 ? _.first(fn) : null;
if (!otherSideM){
var fone = _.filter(relationships, function(o){
return o.object == r.relation && r.collection == o.relation && r.via == o.attribute && o.type == "one";
})
var otherSide1 = fone.length > 0 ? _.first(fone) : null;
if (!otherSide1){
// result.warnings.push("multi select relationship of relation " + r.relation + " attribute " + r.attribute + " has no other side");
result.warnings.push("missing one side of one-to-many relationship: relation " + r.relation + " attribute " + r.attribute);
result.valid = false;
}
}
else{
result.valid = false;
result.warnings.push("multi select relationship are not allowed: relation " + r.relation + " attribute " + r.attribute + " and relation " + otherSideM.relation + " attribute " + otherSideM.attribute);
}
}
else if (r.type == "one"){
var fone = _.filter(relationships, function(o){
return r.object == o.relation && o.collection == r.relation && o.via == r.attribute && o.type == "n";
});
var otherSide = fone.length > 0 ? _.first(fone) : null;
if (!otherSide){
result.warnings.push("single select relationship of relation " + r.relation + " attribute " + r.attribute + " has no other side");
result.valid = false;
}
}
}
);
return result;
}
catch(exp){
console.log(exp);
return { error: exp };
}
}
function validRelation(relation){
var warnings = [];
var valid = true;
if (!(_.isObject(relation) && !_.isArray(relation))){
return { valid: false, warnings: ["relation definition should be an object"]};
}
var relationName = "";
if (!_.has(relation, "name")){
valid = false;
warnings.push("relations should have name");
}
else if (!_.isString(relation.name) ){
valid = false;
warnings.push("relation name should be a string");
}
else if (!(/^\w+$/).test(relation.name)){
warnings.push("relation name should contain only alphanumeric characters and underscore");
}
else{
relationName = relation.name;
if (relationName.length > 32){
valid = false;
warnings.push(relationName + " : relation name should be no longer than 32 characters");
}
}
if (!_.has(relation, "fields")){
valid = false;
warnings.push("relations should have fields:" + relationName);
}
else if (!(_.isObject(relation.fields) && !_.isArray(relation.fields))){
valid = false;
warnings.push("relation fields is an object:" + relationName);
}
else{
_.each(relation.fields, function(value, key){
if (key.length > 32){
valid = false;
warnings.push("relation: " + relationName + " column:" + key + " - column name should be no longer than 32 characters");
}
if (!(/^\w+$/).test(key)){
valid = false;
warnings.push("relation: " + relationName + " column:" + key + " - column name should contain only alphanumeric characters and underscore");
}
if (value.rename){
if (!_.has(value, "type")){
warnings.push("relation: " + relationName + " column:" + key + " cannot rename a relationship column");
}
if (value.rename.length > 32){
valid = false;
warnings.push("relation: " + relationName + " column:" + key + " cannot be renamed to " + value.rename + " - column name should be no longer than 32 characters");
}
if (!(/^\w+$/).test(value.rename)){
valid = false;
warnings.push("relation: " + relationName + " column:" + key + " cannot be renamed to " + value.rename + " - column name should contain only alphanumeric characters and underscore");
}
}
if (!_.has(value, "type")){
if (_.has(value, "collection") && _.has(value, "via")){
if (_.has(value, "object")){
valid = false;
warnings.push("column cannot be both 1 and many side of relationship:" + relationName + " " + key);
}
}
else if (_.has(value, "object")){
if (_.has(value, "collection") || _.has(value, "via")){
valid = false;
warnings.push("column cannot be both 1 and many side of relationship:" + relationName + " " + key);
}
}
else if (_.has(value, "collection") && !_.has(value, "via")){
valid = false;
warnings.push("column on many side of relationship should have both collection and via fields:" + relationName + " " + key);
}
else if (!_.has(value, "collection") && _.has(value, "via")){
valid = false;
warnings.push("column on many side of relationship should have both collection and via fields:" + relationName + " " + key);
}
else{
valid = false;
warnings.push("column should include a type:" + relationName + " " + key);
}
if (_.has(value, "collection") && _.has(value, "cascade") && value.cascade != true && value.cascade != false){
valid = false;
warnings.push("cascade property of column on many side of relationship should be either true or false:" + relationName + " " + key);
}
if (_.has(value, "collection") || _.has(value, "via") || _.has(value, "object")){
if (_.has(value, "required")){
valid = false;
warnings.push("a relationship column cannot be required to be set or not set:" + relationName + " " + key);
}
if (_.has(value, "defaultValue")){
valid = false;
warnings.push("a relationship column cannot have a default value:" + relationName + " " + key);
}
}
}
else if (!_.contains(validTypes, value.type)){
valid = false;
warnings.push("column type is invalid:" + relationName + " " + key);
}
if (_.has(value, "required") && !_.isBoolean(value.required)){
valid = false;
warnings.push("column required property should be boolean:" + relationName + " " + key);
}
if (_.has(value, "defaultValue")){
switch(value.type)
{
case "string":
case "text":
if (!_.isString(value.defaultValue)){
valid = false;
warnings.push("column default value should be a string:" + relationName + " " + key);
}
break;
case "float":
if (!_.isNumber(value.defaultValue) || _.isNaN(value.defaultValue)){
valid = false;
warnings.push("column default value should be a float:" + relationName + " " + key);
}
break;
case "binary":
break;
case "datetime":
if (!_.isDate(new Date(value.defaultValue))){
valid = false;
warnings.push("column default value should be a datetime:" + relationName + " " + key);
}
break;
case "boolean":
if (!_.isBoolean(value.defaultValue)){
valid = false;
warnings.push("column default value should be a boolean:" + relationName + " " + key);
}
break;
case "point":
if (!_.isArray(value.defaultValue) || value.defaultValue.length != 2 || !_.every(value.defaultValue, function(n){ return _.isNumber(n) && !_.isNaN(n); })){
valid = false;
warnings.push("column default value should be a [float, float]:" + relationName + " " + key);
}
break;
default:
break;
}
}
});
}
return { valid: valid, warnings: warnings };
}
// fetch relationships on a relation
// determine validity of fields involved in relationship
// compute warnings for relationship validity
// returns object :
// { valid: <boolean>, warnings: <array of string>, relationships: <array of describing relationship fields> }
// relationship attribute:
// for one side
// { relation: <relation name>, attribute:<name of attribute>, object: <attribute object field>, type: "one" }
// for many side
// { relation: <relation name>, attribute:<name of attribute>, collection: <attribute collection field>, via: <attribute via field>, type: "n" }
function getRelationships(relation){
// either structure is invalid, or cannot determine relationships
if (!(_.isObject(relation) && !_.isArray(relation) && _.has(relation, "name") && _.has(relation, "fields") && _.isObject(relation.fields) && !_.isArray(relation.fields)))
return { valid: true, relationships: null, warnings: [], relationships:[] };
var valid = true;
var warnings = [];
var relationships = [];
_.each(relation.fields, function(value, key){
if (_.has(value, "collection") && _.has(value, "via")){
relationships.push(_.extend(_.clone(value), { relation: relation.name, attribute: key, type: "n" }));
}
else if (_.has(value, "object")){
relationships.push({ relation: relation.name, attribute: key, object: value.object, type: "one" });
}
});
return { valid: valid, warnings: warnings, relationships: relationships };
}