validation-chain-builders.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.query = exports.param = exports.header = exports.cookie = exports.body = exports.check = void 0;
  4. exports.buildCheckFunction = buildCheckFunction;
  5. const check_1 = require("./check");
  6. /**
  7. * Creates a variant of `check()` that checks the given request locations.
  8. *
  9. * @example
  10. * const checkBodyAndQuery = buildCheckFunction(['body', 'query']);
  11. */
  12. function buildCheckFunction(locations) {
  13. return (fields, message) => (0, check_1.check)(fields, locations, message);
  14. }
  15. /**
  16. * Creates a middleware/validation chain for one or more fields that may be located in
  17. * any of the following:
  18. *
  19. * - `req.body`
  20. * - `req.cookies`
  21. * - `req.headers`
  22. * - `req.params`
  23. * - `req.query`
  24. *
  25. * @param fields a string or array of field names to validate/sanitize
  26. * @param message an error message to use when failed validations don't specify a custom message.
  27. * Defaults to `Invalid Value`.
  28. */
  29. exports.check = buildCheckFunction(['body', 'cookies', 'headers', 'params', 'query']);
  30. /**
  31. * Same as {@link check()}, but only validates `req.body`.
  32. */
  33. exports.body = buildCheckFunction(['body']);
  34. /**
  35. * Same as {@link check()}, but only validates `req.cookies`.
  36. */
  37. exports.cookie = buildCheckFunction(['cookies']);
  38. /**
  39. * Same as {@link check()}, but only validates `req.headers`.
  40. */
  41. exports.header = buildCheckFunction(['headers']);
  42. /**
  43. * Same as {@link check()}, but only validates `req.params`.
  44. */
  45. exports.param = buildCheckFunction(['params']);
  46. /**
  47. * Same as {@link check()}, but only validates `req.query`.
  48. */
  49. exports.query = buildCheckFunction(['query']);