open-learning-exchange/planet

View on GitHub
design/nations/nation-validators.json

Summary

Maintainability
Test Coverage
{"validate_doc_update":"function (newDoc, oldDoc, userCtx, secObj) {\n    function require(field) {\n      var errMessage = field + ' is required';\n      if (!newDoc[field]) {\n        throw { forbidden: errMessage };\n      }\n    }\n    function canWrite() {\n      var CAN_WRITE = false;\n      var writeAccess = ['_admin'];\n      if(secObj && secObj.admins && secObj.admins.names) {\n        CAN_WRITE = (secObj.admins.names.indexOf(userCtx.name) !== -1);\n      }\n      if(!CAN_WRITE && secObj && secObj.admins && secObj.admins.roles) {\n        writeAccess.concat(secObj.admins.roles);\n      }\n      for (var i = 0; (!CAN_WRITE && i < userCtx.roles.length); i++) {\n        if (writeAccess.indexOf(userCtx.roles[i]) !== -1) {\n          CAN_WRITE = true;\n        }\n      }\n      if(!CAN_WRITE) {\n        throw { forbidden: 'You have only read-only access' };\n      }\n    }\n    canWrite();\n    if (!newDoc) {\n      require('name');\n      require('adminName');\n      require('nationUrl');\n    }\n  }"}