check.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const validator = require('validator');
  2. const runner = require('./runner');
  3. const { isSanitizer, isValidator } = require('../utils/filters');
  4. module.exports = (fields, locations, message) => {
  5. let optional;
  6. const validators = [];
  7. const sanitizers = [];
  8. fields = Array.isArray(fields) ? fields : [fields];
  9. const middleware = (req, res, next) => {
  10. return runner(req, middleware._context).then(errors => {
  11. req._validationContexts = (req._validationContexts || []).concat(middleware._context);
  12. req._validationErrors = (req._validationErrors || []).concat(errors);
  13. next();
  14. }, next);
  15. };
  16. Object.keys(validator)
  17. .filter(isValidator)
  18. .forEach(methodName => {
  19. const validationFn = validator[methodName];
  20. middleware[methodName] = (...options) => {
  21. validators.push({
  22. negated: middleware._context.negateNext,
  23. validator: validationFn,
  24. options
  25. });
  26. middleware._context.negateNext = false;
  27. return middleware;
  28. };
  29. });
  30. Object.keys(validator)
  31. .filter(isSanitizer)
  32. .forEach(methodName => {
  33. const sanitizerFn = validator[methodName];
  34. middleware[methodName] = (...options) => {
  35. sanitizers.push({
  36. sanitizer: sanitizerFn,
  37. options
  38. });
  39. return middleware;
  40. };
  41. });
  42. middleware.optional = (options = {}) => {
  43. optional = options;
  44. return middleware;
  45. };
  46. middleware.custom = validator => {
  47. validators.push({
  48. validator,
  49. custom: true,
  50. negated: middleware._context.negateNext,
  51. options: []
  52. });
  53. middleware._context.negateNext = false;
  54. return middleware;
  55. };
  56. middleware.customSanitizer = sanitizer => {
  57. sanitizers.push({
  58. sanitizer,
  59. custom: true,
  60. options: []
  61. });
  62. return middleware;
  63. };
  64. middleware.exists = (options = {}) => {
  65. const validator = options.checkFalsy
  66. ? existsValidatorCheckFalsy
  67. : (options.checkNull ? existsValidatorCheckNull : existsValidator);
  68. return middleware.custom(validator);
  69. };
  70. middleware.isArray = () => middleware.custom(value => Array.isArray(value));
  71. middleware.isString = () => middleware.custom(value => typeof value === 'string');
  72. middleware.withMessage = message => {
  73. const lastValidator = validators[validators.length - 1];
  74. if (lastValidator) {
  75. lastValidator.message = message;
  76. }
  77. return middleware;
  78. };
  79. middleware.not = () => {
  80. middleware._context.negateNext = true;
  81. return middleware;
  82. };
  83. middleware._context = {
  84. get optional() {
  85. return optional;
  86. },
  87. negateNext: false,
  88. message,
  89. fields,
  90. locations,
  91. sanitizers,
  92. validators
  93. };
  94. return middleware;
  95. };
  96. function existsValidator(value) {
  97. return value !== undefined;
  98. }
  99. function existsValidatorCheckNull(value) {
  100. return value != null;
  101. }
  102. function existsValidatorCheckFalsy(value) {
  103. return !!value;
  104. }