index.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Common validation functions */
  2. var Joi = require('joi');
  3. /**
  4. * Validator for a new News item.
  5. * Checks that a title String and contents String was supplied.
  6. *
  7. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  8. * @param params The params to validate the schema against
  9. */
  10. exports.validateNewsItem = function (res, params) {
  11. var schema = {
  12. title: Joi.string().required(),
  13. contents: Joi.string().required()
  14. };
  15. return commonValidator(res, schema, params);
  16. };
  17. /**
  18. * Validator for a user/pass combo.
  19. * Checks that a username String and password String was supplied.
  20. *
  21. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  22. * @param params The params to validate the schema against
  23. */
  24. exports.validateUserPass = function (res, params) {
  25. var schema = {
  26. username: Joi.string().required(),
  27. password: Joi.string().required()
  28. };
  29. return commonValidator(res, schema, params);
  30. };
  31. /**
  32. * Validator for a tool id.
  33. * Checks that the tool id is a positive integer.
  34. *
  35. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  36. * @param params The params to validate the schema against
  37. */
  38. exports.validateToolId = function (res, params) {
  39. var schema = {
  40. toolId: Joi.number().integer().positive().required()
  41. };
  42. return commonValidator(res, schema, params);
  43. };
  44. /**
  45. * Validator for a tool id and tool name.
  46. * Checks that the tool id is a positive integer and that the tool name is a String.
  47. *
  48. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  49. * @param params The params to validate the schema against
  50. */
  51. exports.validateToolIdName = function (res, params) {
  52. var schema = {
  53. toolId: Joi.number().integer().positive().required(),
  54. toolName: Joi.string().required()
  55. };
  56. return commonValidator(res, schema, params);
  57. };
  58. /**
  59. * Validator for a tool id and section name.
  60. * Checks that the tool id is a positive integer and that the tool section is a String.
  61. *
  62. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  63. * @param params The params to validate the schema against
  64. */
  65. exports.validateToolAndSection = function (res, params) {
  66. var schema = {
  67. toolId: Joi.number().integer().positive().required(),
  68. sectionName: Joi.string().required()
  69. };
  70. return commonValidator(res, schema, params);
  71. };
  72. /**
  73. * Validator for a tool id, section name and item number.
  74. * Checks that the tool id is a positive integer, that the tool section is a String and that the item number is positive integer.
  75. *
  76. * @param res The response object passed over from calling method (so that an error can be sent if validation fails)
  77. * @param params The params to validate the schema against
  78. */
  79. exports.validateToolSectionItem = function (res, params) {
  80. var schema = {
  81. toolId: Joi.number().integer().positive().required(),
  82. sectionName: Joi.string().required(),
  83. itemNum: Joi.number().integer().positive().required()
  84. };
  85. return commonValidator(res, schema, params);
  86. };
  87. /**
  88. * Validator helper method. Wraps the Joi.validate() method such that params are checked against a schema.
  89. * Also sends back a generic HTTP 400 for a bad request.
  90. * @param res The response object sent from calling method so that errors can be sent back to client.
  91. * @param schema The schema to validate the params against.
  92. * @param params The params to validate the schema against.
  93. * @returns {boolean} The validity of the params (i.e. do they fulfil the requirements).
  94. */
  95. function commonValidator(res, schema, params) {
  96. var valid = false;
  97. //Run the validation
  98. Joi.validate(params, schema, function (err, value) {
  99. //If there is no error, the params are valid.
  100. if (err == null)
  101. valid = true;
  102. //Otherwise, the params are invalid, and we should send back an error to the client
  103. else {
  104. res.status(400).send({message: "Missing/Bad params.", errors: err});
  105. valid = false;
  106. }
  107. });
  108. //The caller needs to know if the validation went through successfully or not, so send back the Boolean.
  109. return valid;
  110. }