context-handler.d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { CustomValidator } from '../base';
  2. import { Optional } from '../context';
  3. import { ContextRunner } from './context-runner';
  4. export interface BailOptions {
  5. /**
  6. * Defines the level at which to stop running further validations:
  7. * - When set to `chain`, further validations won't be run for this validation chain if there
  8. * are any errors.
  9. * - When set to `request`, no further validations on the same request will be run either if
  10. * there are any errors.
  11. *
  12. * @default 'chain'
  13. */
  14. level?: 'chain' | 'request';
  15. }
  16. export interface OptionalOptions {
  17. /**
  18. * Defines which kind of value makes a field optional.
  19. *
  20. * - `undefined`: only `undefined` values; equivalent to `value === undefined`
  21. * - `null`: only `undefined` and `null` values; equivalent to `value == null`
  22. * - `falsy`: all falsy values; equivalent to `!value`
  23. *
  24. * @default 'undefined'
  25. */
  26. values?: Exclude<Optional, false>;
  27. /**
  28. * Whether a field whose value is `null` or `undefined` is to be considered optional.
  29. * @default false
  30. * @deprecated Use `values` instead.
  31. */
  32. nullable?: boolean;
  33. /**
  34. * Whether a field whose value is falsy (that is, `0`, `false`, `null`, `undefined` or an empty
  35. * string) is to be considered optional.
  36. * @default false
  37. * @deprecated Use `values` instead.
  38. */
  39. checkFalsy?: boolean;
  40. }
  41. export interface ContextHandler<Chain> {
  42. /**
  43. * Stops running validations if any of the previous ones have failed.
  44. *
  45. * Useful to prevent a custom validator that touches a database or external API from running when
  46. * you know it will fail.
  47. *
  48. * May be used multiple times in the same validation chain if desired.
  49. *
  50. * @example
  51. * check('username')
  52. * .isEmail()
  53. * // If not an email, stop here
  54. * .bail()
  55. * .custom(checkDenylistDomain)
  56. * // If domain is not allowed, don't go check if it already exists
  57. * .bail()
  58. * .custom(checkEmailExists)
  59. *
  60. * @returns the current validation chain
  61. */
  62. bail(opts?: BailOptions): Chain;
  63. /**
  64. * Adds a condition on whether the validation should continue on a field or not.
  65. * @param condition may be either
  66. * - a custom validator-like function, which must truthy or a promise that resolves to continue
  67. * validation. If the return value is falsy, a promise that rejects, or if it throws, validation
  68. * will stop.
  69. * - a validation chain which if it would produce errors, the validation chain stops.
  70. * @example
  71. * check('newPassword')
  72. * // Only validate if the old password has been provided
  73. * .if((value, { req }) => req.body.oldPassword)
  74. * // Or, use it with a a validation chain
  75. * .if(body('oldPassword').notEmpty())
  76. * @returns the current validation chain
  77. */
  78. if(condition: CustomValidator | ContextRunner): Chain;
  79. /**
  80. * Marks the field(s) of the validation chain as optional.
  81. * By default, only fields with an `undefined` value are considered optional and will be ignored
  82. * when validating.
  83. *
  84. * @param options an object of options to customize the behavior of optional.
  85. * @returns the current validation chain
  86. */
  87. optional(options?: {
  88. values?: Optional;
  89. /**
  90. * @deprecated use `values` instead
  91. */
  92. checkFalsy?: boolean;
  93. /**
  94. * @deprecated use `values` instead
  95. */
  96. nullable?: boolean;
  97. } | boolean): Chain;
  98. /**
  99. * Hide the field's value in errors returned by `validationResult()`.
  100. *
  101. * If the value is confidential information (such as api key),
  102. * you might want to call this method to prevent exposing it.
  103. *
  104. * @param hiddenValue? String to replace the field's value with.
  105. * If it's not set, the field value is removed from errors.
  106. *
  107. * @returns the current validation chain
  108. */
  109. hide(hiddenValue?: string): Chain;
  110. }