context-handler-impl.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ContextHandlerImpl = void 0;
  4. const context_items_1 = require("../context-items");
  5. const bail_1 = require("../context-items/bail");
  6. class ContextHandlerImpl {
  7. constructor(builder, chain) {
  8. this.builder = builder;
  9. this.chain = chain;
  10. }
  11. bail(opts) {
  12. if (opts?.level === 'request') {
  13. this.builder.setRequestBail();
  14. }
  15. this.builder.addItem(new bail_1.Bail());
  16. return this.chain;
  17. }
  18. if(condition) {
  19. if ('run' in condition) {
  20. this.builder.addItem(new context_items_1.ChainCondition(condition));
  21. }
  22. else if (typeof condition === 'function') {
  23. this.builder.addItem(new context_items_1.CustomCondition(condition));
  24. }
  25. else {
  26. throw new Error('express-validator: condition is not a validation chain nor a function');
  27. }
  28. return this.chain;
  29. }
  30. optional(options = true) {
  31. let value;
  32. if (typeof options === 'boolean') {
  33. value = options ? 'undefined' : false;
  34. }
  35. else {
  36. value = options.values
  37. ? options.values
  38. : options.checkFalsy
  39. ? 'falsy'
  40. : options.nullable
  41. ? 'null'
  42. : 'undefined';
  43. }
  44. this.builder.setOptional(value);
  45. return this.chain;
  46. }
  47. hide(hiddenValue) {
  48. this.builder.setHidden(true, hiddenValue);
  49. return this.chain;
  50. }
  51. }
  52. exports.ContextHandlerImpl = ContextHandlerImpl;