check.d.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import * as express from 'express';
  2. import { Sanitizer } from '../filter';
  3. import { Location } from './location';
  4. export type URLProtocol = 'http' | 'https' | 'ftp'
  5. export type UUIDVersion = 3 | 4 | 5 | 'all'
  6. export type IPVersion = 4 | 6
  7. export type AlphaLocale = 'ar' | 'ar-AE' | 'ar-BH' | 'ar-DZ' | 'ar-EG' | 'ar-IQ' | 'ar-JO' | 'ar-KW' | 'ar-LB' | 'ar-LY' | 'ar-MA' | 'ar-QA' | 'ar-QM' | 'ar-SA' | 'ar-SD' | 'ar-SY' | 'ar-TN' | 'ar-YE' | 'bg-BG' | 'cs-CZ' | 'da-DK' | 'de-DE' | 'el-GR' | 'en-AU' | 'en-GB' | 'en-HK' | 'en-IN' | 'en-NZ' | 'en-US' | 'en-ZA' | 'en-ZM' | 'es-ES' | 'fr-FR' | 'hu-HU' | 'it-IT' | 'nb-NO' | 'nl-NL' | 'nn-NO' | 'pl-PL' | 'pt-BR' | 'pt-PT' | 'ru-RU' | 'sk-SK' | 'sr-RS' | 'sr-RS@latin' | 'sv-SE' | 'tr-TR' | 'uk-UA'
  8. export type AlphanumericLocale = 'ar' | 'ar-AE' | 'ar-BH' | 'ar-DZ' | 'ar-EG' | 'ar-IQ' | 'ar-JO' | 'ar-KW' | 'ar-LB' | 'ar-LY' | 'ar-MA' | 'ar-QA' | 'ar-QM' | 'ar-SA' | 'ar-SD' | 'ar-SY' | 'ar-TN' | 'ar-YE' | 'bg-BG' | 'cs-CZ' | 'da-DK' | 'de-DE' | 'el-GR' | 'en-AU' | 'en-GB' | 'en-HK' | 'en-IN' | 'en-NZ' | 'en-US' | 'en-ZA' | 'en-ZM' | 'es-ES' | 'fr-FR' | 'fr-BE' | 'hu-HU' | 'it-IT' | 'nb-NO' | 'nl-BE' | 'nl-NL' | 'nn-NO' | 'pl-PL' | 'pt-BR' | 'pt-PT' | 'ru-RU' | 'sk-SK' | 'sr-RS' | 'sr-RS@latin' | 'sv-SE' | 'tr-TR' | 'uk-UA'
  9. export type MobilePhoneLocal = 'any' | 'ar-AE' | 'ar-DZ' | 'ar-EG' | 'ar-JO' | 'ar-KW' | 'ar-SA' | 'ar-SY' | 'ar-TN' | 'be-BY' | 'bg-BG' | 'cs-CZ' | 'de-DE' | 'da-DK' | 'el-GR' | 'en-AU' | 'en-GB' | 'en-HK' | 'en-IN' | 'en-KE' | 'en-NG' | 'en-NZ' | 'en-PK' | 'en-RW' | 'en-SG' | 'en-TZ' | 'en-UG' | 'en-US' | 'en-CA' | 'en-ZA' | 'en-ZM' | 'es-ES' | 'et-EE' | 'fa-IR' | 'fi-FI' | 'fo-FO' | 'fr-FR' | 'he-IL' | 'hu-HU' | 'id-ID' | 'it-IT' | 'ja-JP' | 'kk-KZ' | 'kl-GL' | 'lt-LT' | 'ms-MY' | 'nb-NO' | 'nn-NO' | 'pl-PL' | 'pt-PT' | 'ro-RO' | 'ru-RU' | 'sk-SK' | 'sr-RS' | 'sv-SE' | 'th-TH' | 'tr-TR' | 'uk-UA' | 'vi-VN' | 'zh-CN' | 'zh-HK' | 'zh-TW'
  10. export type PostalCodeLocale = 'any' | 'AT' | 'AU' | 'BE' | 'BG' | 'CA' | 'CH' | 'CZ' | 'DE' | 'DK' | 'DZ' | 'EE' | 'ES' | 'FI' | 'FR' | 'GB' | 'GR' | 'HR' | 'HU' | 'IL' | 'IN' | 'IS' | 'IT' | 'JP' | 'KE' | 'LI' | 'LT' | 'LU' | 'LV' | 'MX' | 'NL' | 'NO' | 'PL' | 'PT' | 'RO' | 'RU' | 'SA' | 'SE' | 'SI' | 'TN' | 'TW' | 'US' | 'ZA' | 'ZM';
  11. export type HashAlgorithm = 'md4' | 'md5' | 'sha1' | 'sha256'| 'sha384'| 'sha512'| 'ripemd128'| 'ripemd160'| 'tiger128'| 'tiger160'| 'tiger192'| 'crc32'| 'crc32b';
  12. export interface Validator {
  13. isAfter(date?: string): this;
  14. isAlpha(locale?: AlphaLocale): this;
  15. isAlphanumeric(locale?: AlphanumericLocale): this;
  16. isArray(): this;
  17. isAscii(): this;
  18. isBase64(): this;
  19. isBefore(date?: string): this;
  20. isBoolean(): this;
  21. isByteLength(options: ValidatorOptions.MinMaxOptions): this;
  22. isCreditCard(): this;
  23. isCurrency(options: ValidatorOptions.IsCurrencyOptions): this;
  24. isDataURI(): this;
  25. isDecimal(options?: ValidatorOptions.IsDecimalOptions): this;
  26. isDivisibleBy(num: number): this;
  27. isEmail(options?: ValidatorOptions.IsEmailOptions): this;
  28. isEmpty(): this;
  29. isFloat(options?: ValidatorOptions.IsFloatOptions): this;
  30. isFQDN(options?: ValidatorOptions.IsFQDNOptions): this;
  31. isFullWidth(): this;
  32. isHalfWidth(): this;
  33. isHash(algorithm: HashAlgorithm): this;
  34. isHexadecimal(): this;
  35. isHexColor(): this;
  36. isIn(options: string | string[]): this;
  37. isInt(options?: ValidatorOptions.IsIntOptions): this;
  38. isIP(version?: IPVersion): this;
  39. isIPRange(): this;
  40. isISIN(): this;
  41. isISO31661Alpha2(): this;
  42. isISO31661Alpha3(): this;
  43. isISO8601(): this;
  44. isISRC(): this;
  45. isISBN(version?: number): this;
  46. isISSN(options?: ValidatorOptions.IsISSNOptions): this;
  47. isJSON(): this;
  48. isLatLong(): this;
  49. isLength(options: ValidatorOptions.MinMaxOptions): this;
  50. isLowercase(): this;
  51. isMACAddress(): this;
  52. isMD5(): this;
  53. isMimeType(): this;
  54. isMongoId(): this;
  55. isMobilePhone(locale: MobilePhoneLocal | MobilePhoneLocal[], options?: ValidatorOptions.IsMobilePhoneOptions): this;
  56. isMultibyte(): this;
  57. isNumeric(): this;
  58. isPostalCode(locale: PostalCodeLocale): this;
  59. isPort(): this;
  60. isString(): this;
  61. isSurrogatePair(): this;
  62. isUppercase(): this;
  63. isURL(options?: ValidatorOptions.IsURLOptions): this;
  64. isUUID(version?: UUIDVersion): this;
  65. isVariableWidth(): this;
  66. isWhitelisted(chars: string | string[]): this;
  67. equals(equals: any): this;
  68. contains(str: string): this;
  69. matches(pattern: RegExp | string, modifiers?: string): this;
  70. // Additional validator methods
  71. not(): this;
  72. exists(options?: ValidatorOptions.ExistsOptions): this;
  73. optional(options?: ValidatorOptions.OptionalOptions): this;
  74. withMessage(message: CustomMessageBuilder): this;
  75. withMessage(message: any): this;
  76. }
  77. export interface ValidationChain extends express.RequestHandler, Validator, Sanitizer {
  78. custom(validator: CustomValidator): this;
  79. }
  80. export interface CustomValidator {
  81. (value: any, options: { req: express.Request, location: string, path: string }): any;
  82. }
  83. export interface CustomMessageBuilder {
  84. (value: any, options: { req: express.Request, location: Location, path: string }): any;
  85. }
  86. export namespace ValidatorOptions {
  87. interface MinMaxOptions {
  88. min?: number;
  89. max?: number;
  90. }
  91. interface MinMaxExtendedOptions extends MinMaxOptions {
  92. lt?: number;
  93. gt?: number;
  94. }
  95. interface IsFloatOptions extends MinMaxExtendedOptions {
  96. locale?: AlphanumericLocale;
  97. }
  98. interface IsIntOptions extends MinMaxExtendedOptions {
  99. allow_leading_zeroes?: boolean;
  100. }
  101. interface IsDecimalOptions {
  102. decimal_digits?: string;
  103. force_decimal?: boolean;
  104. locale?: AlphanumericLocale;
  105. }
  106. /**
  107. * defaults to
  108. * {
  109. * allow_display_name: false,
  110. * require_display_name: false,
  111. * allow_utf8_local_part: true,
  112. * require_tld: true
  113. * }
  114. */
  115. interface IsEmailOptions {
  116. allow_display_name?: boolean;
  117. allow_utf8_local_part?: boolean;
  118. require_tld?: boolean;
  119. }
  120. interface IsMobilePhoneOptions {
  121. strictMode?: boolean;
  122. }
  123. /**
  124. * defaults to
  125. * {
  126. * protocols: ['http','https','ftp'],
  127. * require_tld: true,
  128. * require_protocol: false,
  129. * require_host: true,
  130. * require_valid_protocol: true,
  131. * allow_underscores: false,
  132. * host_whitelist: false,
  133. * host_blacklist: false,
  134. * allow_trailing_dot: false,
  135. * allow_protocol_relative_urls: false
  136. * }
  137. */
  138. interface IsURLOptions {
  139. protocols?: URLProtocol[];
  140. require_tld?: boolean;
  141. require_protocol?: boolean;
  142. require_host?: boolean;
  143. require_valid_protocol?: boolean;
  144. allow_underscores?: boolean;
  145. host_whitelist?: (string | RegExp)[];
  146. host_blacklist?: (string | RegExp)[];
  147. allow_trailing_dot?: boolean;
  148. allow_protocol_relative_urls?: boolean;
  149. }
  150. /**
  151. * defaults to
  152. * {
  153. * require_tld: true,
  154. * allow_underscores: false,
  155. * allow_trailing_dot: false
  156. * }
  157. */
  158. interface IsFQDNOptions {
  159. require_tld?: boolean;
  160. allow_underscores?: boolean;
  161. allow_trailing_dot?: boolean;
  162. }
  163. /**
  164. * defaults to
  165. * {
  166. * case_sensitive: false,
  167. * require_hyphen: false
  168. * }
  169. */
  170. interface IsISSNOptions {
  171. case_sensitive?: boolean
  172. require_hyphen?: boolean
  173. }
  174. /**
  175. * defaults to
  176. * {
  177. * symbol: '$',
  178. * require_symbol: false,
  179. * allow_space_after_symbol: false,
  180. * symbol_after_digits: false,
  181. * allow_negatives: true,
  182. * parens_for_negatives: false,
  183. * negative_sign_before_digits: false,
  184. * negative_sign_after_digits: false,
  185. * allow_negative_sign_placeholder: false,
  186. * thousands_separator: ',',
  187. * decimal_separator: '.',
  188. * allow_space_after_digits: false
  189. * }
  190. */
  191. interface IsCurrencyOptions {
  192. symbol?: string;
  193. require_symbol?: boolean;
  194. allow_space_after_symbol?: boolean;
  195. symbol_after_digits?: boolean;
  196. allow_negatives?: boolean;
  197. parens_for_negatives?: boolean;
  198. negative_sign_before_digits?: boolean;
  199. negative_sign_after_digits?: boolean;
  200. allow_negative_sign_placeholder?: boolean;
  201. thousands_separator?: string;
  202. decimal_separator?: string;
  203. allow_decimal?: boolean;
  204. require_decimal?: boolean;
  205. digits_after_decimal?: number[];
  206. allow_space_after_digits?: boolean;
  207. }
  208. interface OptionalOptions {
  209. checkFalsy?: boolean;
  210. nullable?: boolean;
  211. }
  212. /**
  213. * defaults to
  214. * {
  215. * checkNull: false,
  216. * checkFalsy: false
  217. * }
  218. */
  219. interface ExistsOptions {
  220. checkNull?: boolean;
  221. checkFalsy?: boolean;
  222. }
  223. }