X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Futils%2Fvalidation.js;h=43bdd954c71cf96b3c98ea301172cb75f241b34d;hb=ea8b41a9134cc20f8bd6e70326370319cab851ae;hp=560bd5cf596b606f3c7b549be062f4afcfcf4486;hpb=e99c53fb3be56eb4c685dd061eef0e5b5bf22b73;p=qomet.git diff --git a/public/javascripts/utils/validation.js b/public/javascripts/utils/validation.js index 560bd5c..43bdd95 100644 --- a/public/javascripts/utils/validation.js +++ b/public/javascripts/utils/validation.js @@ -1,19 +1,21 @@ -try { var _ = require("underscore"); } catch (err) {} //for server +try { var _ = require("underscore"); } catch (err) { } //for server let Validator = { }; -// Cell in assessment.questions array +// Cell in evaluation.questions array Validator.Question = { "index": "section", //"2.2.1", "3.2", "1" ...etc "wording": "string", "options": "stringArray", //only for quiz "fixed": "boolean", - "answer": "string", //both this and next are mutually exclusive - "choice": "integerArray", - "active": "boolean", "points": "number", }; +Validator.Answer = { + "index": "section", + "value": "stringOrIntegerArray", +}; + Validator.Input = { "index": "section", "input": "stringOrIntegerArray", @@ -22,14 +24,16 @@ Validator.Input = { // One student response to an exam Validator.Paper = { "number": "code", - // (array of) strings for open questions, arrays of integers for quizzes: "inputs": Validator.Input, "startTime": "positiveInteger", - "endTime": "positiveInteger", + "current": "positiveInteger", + "discoTime": "positiveInteger", + "discoCount": "positiveInteger", + "totalDisco": "positiveInteger", "password": "password", }; -Validator.Assessment = { +Validator.Evaluation = { "_id": "bson", "cid": "bson", "name": "code", @@ -39,26 +43,23 @@ Validator.Assessment = { "display": "alphanumeric", //"one" or "all" "time": "integer", "introduction": "string", - "conclusion": "string", "coefficient": "number", "questions": Validator.Question, + "answers": Validator.Answer, "papers": Validator.Paper, }; Validator.User = { "_id": "bson", "email": "email", - "forename": "name", "name": "name", - "initials": "unchecked", //not a user input - "loginToken": "unchecked", - "sessionTokens": "unchecked", - "token": "alphanumeric", //exception: for the purpose of user registration + "initials": "alphanumeric", + "loginToken": "alphanumeric", + "sessionTokens": "alphanumericArray", }; Validator.Student = { "number": "code", - "forename": "name", "name": "name", "group": "positiveInteger", }; @@ -81,8 +82,6 @@ Object.assign(Validator, { if (!model[key]) return "Unknown field"; - if (model[key] == "unchecked") //not a user input (ignored) - continue; if (_.isObject(model[key])) { // TODO: next loop seems too heavy... (only a concern if big class import?) @@ -111,6 +110,8 @@ Object.assign(Validator, "check_string": function(arg) { + if (!_.isString(arg)) + return "not a string"; return ""; //strings are unchecked, but sanitized }, @@ -123,11 +124,6 @@ Object.assign(Validator, return ""; }, - "check_stringArray": function(arg) - { - return !_.isArray(arg) ? "not an array" : ""; - }, - "check_alphanumeric": function(arg) { return arg.match(/^[\w]{1,32}$/) === null ? "[1,32] alphanumerics" : ""; @@ -203,7 +199,7 @@ Object.assign(Validator, if (!_.isString(arg)) return "not a string"; if (!/^[\x21-\x7E]{1,16}$/.test(arg)) - return "[1,16] ASCII characters with code in [33,126]"; + return "{1,16} ASCII characters with code in [33,126]"; return ""; }, @@ -230,6 +226,31 @@ Object.assign(Validator, return ""; }, + "check_stringArray": function(arg) + { + if (!_.isArray(arg)) + return "not an array"; + for (let i=0; i 0) + return error; + } + return ""; + }, + "check_stringOrIntegerArray": function(arg) { if (!_.isString(arg)) @@ -238,4 +259,4 @@ Object.assign(Validator, }, }); -try { module.exports = Validator.checkObject; } catch (err) {} //for server +try { module.exports = Validator.checkObject; } catch (err) { } //for server