43bdd954c71cf96b3c98ea301172cb75f241b34d
1 try { var _
= require("underscore"); } catch (err
) { } //for server
5 // Cell in evaluation.questions array
7 "index": "section", //"2.2.1", "3.2", "1" ...etc
9 "options": "stringArray", //only for quiz
16 "value": "stringOrIntegerArray",
21 "input": "stringOrIntegerArray",
24 // One student response to an exam
27 "inputs": Validator
.Input
,
28 "startTime": "positiveInteger",
29 "current": "positiveInteger",
30 "discoTime": "positiveInteger",
31 "discoCount": "positiveInteger",
32 "totalDisco": "positiveInteger",
33 "password": "password",
36 Validator
.Evaluation
= {
40 "mode": "alphanumeric", //"open" or "exam", but alphanumeric is good enough
43 "display": "alphanumeric", //"one" or "all"
45 "introduction": "string",
46 "coefficient": "number",
47 "questions": Validator
.Question
,
48 "answers": Validator
.Answer
,
49 "papers": Validator
.Paper
,
56 "initials": "alphanumeric",
57 "loginToken": "alphanumeric",
58 "sessionTokens": "alphanumericArray",
64 "group": "positiveInteger",
71 "description": "string",
73 "students": Validator
.Student
,
76 Object
.assign(Validator
,
78 // Recurse into sub-documents
79 checkObject_aux: function(obj
, model
)
81 for (let key
of Object
.keys(obj
))
84 return "Unknown field";
85 if (_
.isObject(model
[key
]))
87 // TODO: next loop seems too heavy... (only a concern if big class import?)
88 for (let item
of obj
[key
])
90 let error
= Validator
.checkObject_aux(item
, model
[key
]);
97 let error
= Validator
[ "check_" + model
[key
] ](obj
[key
]);
99 return key
+ ": " + error
;
105 // Always check top-level object
106 checkObject: function(obj
, name
)
108 return Validator
.checkObject_aux(obj
, Validator
[name
]);
111 "check_string": function(arg
)
113 if (!_
.isString(arg
))
114 return "not a string";
115 return ""; //strings are unchecked, but sanitized
118 "check_section": function(arg
)
120 if (!_
.isString(arg
))
121 return "not a string";
122 if (!/^[0-9.]+$/.test(arg
))
123 return "digits and dot only";
127 "check_alphanumeric": function(arg
)
129 return arg
.match(/^[\w]{1,32}$/) === null ? "[1,32] alphanumerics" : "";
132 "check_bson": function(arg
)
134 return arg
.match(/^[a-z0-9]{24}$/) === null ? "not a BSON id" : "";
137 "check_name": function(arg
)
139 if (!_
.isString(arg
))
140 return "not a string";
141 if (!/^[a
-zA
-Z
\u00C0-\u024F -]{1,32}$/.test(arg
))
142 return "[1,32] letters + hyphen/space";
146 "check_email": function(arg
)
148 if (!_
.isString(arg
))
149 return "not a string";
151 return "string too long: max. 64 characters";
152 // Regexp used in "type='email'" inputs ( http://emailregex.com/ )
153 if (!/^[a-zA-Z0-9.!#$%&’*+/=?^_
`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(arg))
154 return "X@Y, alphanumerics and . _ - +(X)";
158 "check_code": function(arg)
160 if (!_.isString(arg))
161 return "not a string";
162 if (!/^[\w.-]{1,16}$/.test(arg))
163 return "[1,16] alphanumerics and . _ -";
167 "check_number": function(arg)
169 if (!_.isNumber(arg))
170 arg = parseFloat(arg);
172 return "not a number";
176 "check_integer": function(arg)
178 if (!_.isNumber(arg))
180 if (isNaN(arg) || arg % 1 != 0)
181 return "not an integer";
185 "check_positiveInteger": function(arg)
187 return Validator["check_integer"](arg) || (arg<0 ? "not positive" : "");
190 "check_boolean": function(arg)
192 if (!_.isBoolean(arg))
193 return "not a boolean";
197 "check_password": function(arg)
199 if (!_.isString(arg))
200 return "not a string";
201 if (!/^[\x21-\x7E]{1,16}$/.test(arg))
202 return "{1,16} ASCII characters with code in [33,126]";
206 // Sha-1 hash: length 40, hexadecimal
207 "check_hash": function(arg)
209 if (!_.isString(arg))
210 return "not a string";
211 if (!/^[a-f0-9]{40}$/.test(arg))
212 return "not a sha-1 hash";
216 "check_integerArray": function(arg)
219 return "not an array";
220 for (let i=0; i<arg.length; i++)
222 let error = Validator["check_integer"](arg[i]);
223 if (error.length > 0)
229 "check_stringArray": function(arg)
232 return "not an array";
233 for (let i=0; i<arg.length; i++)
235 if (!_.isString(arg[i]));
236 return "not a string";
241 "check_alphanumericArray": function(arg)
244 return "not an array";
245 for (let i=0; i<arg.length; i++)
247 let error = Validator["check_alphanumeric"](arg[i]);
248 if (error.length > 0)
254 "check_stringOrIntegerArray": function(arg)
256 if (!_.isString(arg))
257 return Validator["check_integerArray"](arg);
262 try { module.exports = Validator.checkObject; } catch (err) { } //for server