UNPKG

18.6 kBTypeScriptView Raw
1// Type definitions for validator.js v10.11.0
2// Project: https://github.com/chriso/validator.js
3// Definitions by: tgfjt <https://github.com/tgfjt>
4// Ilya Mochalov <https://github.com/chrootsu>
5// Ayman Nedjmeddine <https://github.com/IOAyman>
6// Louy Alakkad <https://github.com/louy>
7// Kacper Polak <https://github.com/kacepe>
8// Bonggyun Lee <https://github.com/deptno>
9// Naoto Yokoyama <https://github.com/builtinnya>
10// Philipp Katz <https://github.com/qqilihq>
11// Jace Warren <https://github.com/keatz55>
12// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
13
14declare namespace ValidatorJS {
15 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";
16 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" | "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";
17 type DecimalLocale = "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" | "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" | "sr-RS" | "sr-RS@latin" | "sv-SE" | "tr-TR" | "uk-UA";
18 type FloatLocale = "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" | "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" | "sr-RS" | "sr-RS@latin" | "sv-SE" | "tr-TR" | "uk-UA";
19 type MobilePhoneLocale = "ar-AE" | "ar-DZ" | "ar-EG" | "ar-JO" | "ar-SA" | "ar-SY" | "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-UG" | "en-RW" | "en-SG" | "en-TZ" | "en-PK" | "en-US" | "en-CA" | "en-ZA" | "en-ZM" | "es-ES" | "fa-IR" | "fi-FI" | "fo-FO" | "fr-FR" | "he-IL" | "hu-HU" | "id-ID" | "it-IT" | "ja-JP" | "kk-KZ" | "kl-GL" | "ko-KR" | "lt-LT" | "ms-MY" | "nb-NO" | "nn-NO" | "pl-PL" | "pt-PT" | "ro-RO" | "ru-RU" | "sk-SK" | "sr-RS" | "th-TH" | "tr-TR" | "uk-UA" | "vi-VN" | "zh-CN" | "zh-HK" | "zh-TW" | "any";
20 type PostalCodeLocale = "AT" | "AU" | "BE" | "BG" | "CA" | "CH" | "CZ" | "DE" | "DK" | "DZ" | "ES" | "FI" | "FR" | "GB" | "GR" | "IL" | "IN" | "IS" | "IT" | "JP" | "KE" | "LI" | "MX" | "NL" | "NO" | "PL" | "PT" | "RO" | "RU" | "SA" | "SE" | "TW" | "US" | "ZA" | "ZM" | "any"
21 type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
22
23 interface ValidatorStatic {
24
25 // **************
26 // * Validators *
27 // **************
28
29 // check if the string contains the seed.
30 contains(str: string, elem: any): boolean;
31
32 // check if the string matches the comparison.
33 equals(str: string, comparison: string): boolean;
34
35 // check if the string is a date that's after the specified date (defaults to now).
36 isAfter(str: string, date?: string): boolean;
37
38 // check if the string contains only letters (a-zA-Z). Locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG',
39 // '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',
40 // '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',
41 // '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',
42 // 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']) and defaults to en-US
43 isAlpha(str: string, locale?: AlphaLocale): boolean;
44
45 // check if the string contains only letters and numbers. Locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG',
46 // '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',
47 // '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',
48 // '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',
49 // 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']) and defaults to en-US
50 isAlphanumeric(str: string, locale?: AlphanumericLocale): boolean;
51
52 // check if the string contains ASCII chars only.
53 isAscii(str: string): boolean;
54
55 // check if a string is base64 encoded.
56 isBase64(str: string): boolean;
57
58 // check if the string is a date that's before the specified date.
59 isBefore(str: string, date?: string): boolean;
60
61 // check if a string is a boolean.
62 isBoolean(str: string): boolean;
63
64 // check if the string's length (in bytes) falls in a range.
65 isByteLength(str: string, options: IsByteLengthOptions): boolean;
66 isByteLength(str: string, min: number, max?: number): boolean;
67
68 // check if the string is a credit card.
69 isCreditCard(str: string): boolean;
70
71 // check if the string is a valid currency amount.
72 isCurrency(str: string, options?: IsCurrencyOptions): boolean;
73
74 // check if the string is a data uri format (https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs)
75 isDataURI(str: string): boolean;
76
77 // check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
78 isDecimal(str: string, options?: IsDecimalOptions): boolean;
79
80 // check if the string is a number that's divisible by another.
81 isDivisibleBy(str: string, number: number): boolean;
82
83 // check if the string is an email.
84 isEmail(str: string, options?: IsEmailOptions): boolean;
85
86 // check if the string has a length of zero.
87 isEmpty(str: string, options?: IsEmptyOptions): boolean;
88
89 // check if the string is a fully qualified domain name (e.g. domain.com).
90 isFQDN(str: string, options?: IsFQDNOptions): boolean;
91
92 // check if the string is a float.
93 isFloat(str: string, options?: IsFloatOptions): boolean;
94
95 // check if the string contains any full-width chars.
96 isFullWidth(str: string): boolean;
97
98 // check if the string contains any half-width chars.
99 isHalfWidth(str: string): boolean;
100
101 // check if the string is a hash of type algorithm.
102 // Algorithm is one of ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128',
103 // 'tiger160', 'tiger192', 'crc32', 'crc32b']
104 isHash(str: string, algorithm: HashAlgorithm): boolean;
105
106 // check if the string is a hexadecimal color.
107 isHexColor(str: string): boolean;
108
109 // check if the string is a hexadecimal number.
110 isHexadecimal(str: string): boolean;
111
112 // check if the string is an IP (version 4 or 6).
113 isIP(str: string, version?: number): boolean;
114
115 // check if the string is an ISBN (version 10 or 13).
116 isISBN(str: string, version?: number): boolean;
117
118 // check if the string is an ISSN (https://en.wikipedia.org/wiki/International_Standard_Serial_Number).
119 isISSN(str: string, options?: IsISSNOptions): boolean;
120
121 // check if the string is an ISIN (https://en.wikipedia.org/wiki/International_Securities_Identification_Number)
122 // (stock/security identifier).
123 isISIN(str: string): boolean;
124
125 // check if the string is a valid ISO 8601 (https://en.wikipedia.org/wiki/ISO_8601) date.
126 isISO8601(str: string, options?: IsISO8601Options): boolean;
127
128 // check if the string is a valid ISO 3166-1 alpha-2 (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned
129 // country code.
130 isISO31661Alpha2(str: string): boolean;
131
132 // check if the string is a valid ISO 3166-1 alpha-3 (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned
133 // country code.
134 isISO31661Alpha3(str: string): boolean;
135
136 // check if the string is a ISRC (https://en.wikipedia.org/wiki/International_Standard_Recording_Code).
137 isISRC(str: string): boolean;
138
139 // check if the string is in a array of allowed values.
140 isIn(str: string, values: any[]): boolean;
141
142 // check if the string is an integer.
143 isInt(str: string, options?: IsIntOptions): boolean;
144
145 // check if the string is valid JSON (note: uses JSON.parse).
146 isJSON(str: string): boolean;
147
148 // check if the string is valid JWT token.
149 isJWT(str: string): boolean;
150
151 // check if the string is a valid latitude-longitude coordinate in the format lat,long or lat, long.
152 isLatLong(str: string): boolean;
153
154 // check if the string's length falls in a range.
155 // Note: this function takes into account surrogate pairs.
156 isLength(str: string, options: IsLengthOptions): boolean;
157 isLength(str: string, min: number, max?: number): boolean;
158
159 // check if the string is lowercase.
160 isLowercase(str: string): boolean;
161
162 // check if the string is a MAC address.
163 isMACAddress(str: string): boolean;
164
165 // check if the string is a MD5 hash.
166 isMD5(str: string): boolean;
167
168 // check if the string matches to a valid MIME type (https://en.wikipedia.org/wiki/Media_type) format
169 isMimeType(str: string): boolean;
170
171 // check if the string is a mobile phone number, (locale is one of
172 // ['ar-AE', ar-DZ', 'ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'be-BY', 'bg-BG', 'cs-CZ', 'de-DE',
173 // 'da-DK', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-UG',
174 // 'en-RW', 'en-SG', 'en-TZ', 'en-PK', 'en-US', 'en-CA', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR',
175 // 'fi-FI', 'fo-FO', 'fr-FR', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'ja-JP', 'kk-KZ', 'kl-GL',
176 // 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK',
177 // 'sr-RS', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']).
178 isMobilePhone(str: string, locale: MobilePhoneLocale, options?: IsMobilePhoneOptions): boolean;
179
180 // check if the string is a valid hex-encoded representation of a MongoDB ObjectId
181 // (http://docs.mongodb.org/manual/reference/object-id/).
182 isMongoId(str: string): boolean;
183
184 // check if the string contains one or more multibyte chars.
185 isMultibyte(str: string): boolean;
186
187 // check if the string contains only numbers.
188 isNumeric(str: string, options?: IsNumericOptions): boolean;
189
190 // check if the string is a valid port number.
191 isPort(str: string): boolean;
192
193 // check if the string is a postal code, (locale is one of
194 // [ 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'ES', 'FI', 'FR', 'GB', 'GR',
195 // 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA',
196 // 'SE', 'TW', 'US', 'ZA', 'ZM' ]) OR 'any'. If 'any' is used, function will check if any of the
197 // locales match).
198 isPostalCode(str: string, locale: PostalCodeLocale): boolean;
199
200 // check if the string contains any surrogate pairs chars.
201 isSurrogatePair(str: string): boolean;
202
203 // check if the string is an URL.
204 isURL(str: string, options?: IsURLOptions): boolean;
205
206 // check if the string is a UUID. Must be one of ['3', '4', '5', 'all'], default is all.
207 isUUID(str: string, version?: 3|4|5|"3"|"4"|"5"|"all"): boolean;
208
209 // check if the string is uppercase.
210 isUppercase(str: string): boolean;
211
212 // check if the string contains a mixture of full and half-width chars.
213 isVariableWidth(str: string): boolean;
214
215 // checks characters if they appear in the whitelist.
216 isWhitelisted(str: string, chars: string | string[]): boolean;
217
218 // check if string matches the pattern.
219 matches(str: string, pattern: RegExp | string, modifiers?: string): boolean;
220
221 // **************
222 // * Sanitizers *
223 // **************
224
225 // remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need
226 // to escape some chars, e.g. blacklist(input, '\\[\\]').
227 blacklist(input: string, chars: string): string;
228
229 // replace <, >, &, ', " and / with HTML entities.
230 escape(input: string): string;
231
232 // replaces HTML encoded entities with <, >, &, ', " and /.
233 unescape(input: string): string;
234
235 // trim characters from the left-side of the input.
236 ltrim(input: string, chars?: string): string;
237
238 // canonicalize an email address.
239 normalizeEmail(email: string, options?: NormalizeEmailOptions): string | false;
240
241 // trim characters from the right-side of the input.
242 rtrim(input: string, chars?: string): string;
243
244 // remove characters with a numerical value < 32 and 127, mostly control characters. If keep_new_lines is true,
245 // newline characters are preserved (\n and \r, hex 0xA and 0xD). Unicode-safe in JavaScript.
246 stripLow(input: string, keep_new_lines?: boolean): string;
247
248 // convert the input to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1'
249 // and 'true' return true.
250 toBoolean(input: string, strict?: boolean): boolean;
251
252 // convert the input to a date, or null if the input is not a date.
253 toDate(input: string): Date; // Date or null
254
255 // convert the input to a float, or NaN if the input is not a float.
256 toFloat(input: string): number; // number or NaN
257
258 // convert the input to an integer, or NaN if the input is not an integer.
259 toInt(input: string, radix?: number): number; // number or NaN
260
261 // trim characters (whitespace by default) from both sides of the input.
262 trim(input: string, chars?: string): string;
263
264 // remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will
265 // need to escape some chars, e.g. whitelist(input, '\\[\\]').
266 whitelist(input: string, chars: string): string;
267
268 toString(input: any | any[]): string;
269
270 version: string;
271
272 // **************
273 // * Extensions *
274 // **************
275
276 // add your own validators.
277 // Note: that the first argument will be automatically coerced to a string.
278 extend<T extends Function>(name: string, fn: T): void;
279 }
280
281 // options for IsByteLength
282 interface IsByteLengthOptions {
283 min?: number;
284 max?: number;
285 }
286
287 // options for IsCurrency
288 interface IsCurrencyOptions {
289 symbol?: string;
290 require_symbol?: boolean;
291 allow_space_after_symbol?: boolean;
292 symbol_after_digits?: boolean;
293 allow_negatives?: boolean;
294 parens_for_negatives?: boolean;
295 negative_sign_before_digits?: boolean;
296 negative_sign_after_digits?: boolean;
297 allow_negative_sign_placeholder?: boolean;
298 thousands_separator?: string;
299 decimal_separator?: string;
300 allow_decimal?: boolean;
301 require_decimal?: boolean;
302 digits_after_decimal?: number[];
303 allow_space_after_digits?: boolean;
304 }
305
306 // options for isDecimal
307 interface IsDecimalOptions {
308 force_decimal?: boolean;
309 decimal_digits?: string;
310 locale?: DecimalLocale;
311 }
312
313 // options for isEmail
314 interface IsEmailOptions {
315 allow_display_name?: boolean;
316 require_display_name?: boolean;
317 allow_utf8_local_part?: boolean;
318 require_tld?: boolean;
319 }
320
321 // options for isEmpty
322 interface IsEmptyOptions {
323 ignore_whitespace?: boolean;
324 }
325
326 // options for isFQDN
327 interface IsFQDNOptions {
328 require_tld?: boolean;
329 allow_underscores?: boolean;
330 allow_trailing_dot?: boolean;
331 }
332
333 // options for IsFloat
334 interface IsFloatOptions {
335 min?: number;
336 max?: number;
337 gt?: number;
338 lt?: number;
339 locale?: FloatLocale;
340 }
341
342 // options for isISSN
343 interface IsISSNOptions {
344 case_sensitive?: boolean;
345 require_hyphen?: boolean;
346 }
347
348 // options for IsInt
349 interface IsIntOptions {
350 min?: number;
351 max?: number;
352 allow_leading_zeroes?: boolean;
353 lt?: number;
354 gt?: number;
355 }
356
357 // options for isISO8601
358 interface IsISO8601Options {
359 strict?: boolean;
360 }
361
362 // options for IsLength
363 interface IsLengthOptions {
364 min?: number;
365 max?: number;
366 }
367
368 // options for isMobilePhone
369 interface IsMobilePhoneOptions {
370 strictMode?: boolean;
371 }
372
373 // options for isURL
374 interface IsURLOptions {
375 protocols?: string[];
376 require_tld?: boolean;
377 require_protocol?: boolean;
378 require_host?: boolean;
379 require_valid_protocol?: boolean;
380 allow_underscores?: boolean;
381 host_whitelist?: (string | RegExp)[];
382 host_blacklist?: (string | RegExp)[];
383 allow_trailing_dot?: boolean;
384 allow_protocol_relative_urls?: boolean;
385 disallow_auth?: boolean;
386 }
387
388 // options for normalizeEmail
389 interface NormalizeEmailOptions {
390 all_lowercase?: boolean;
391 gmail_lowercase?: boolean;
392 gmail_remove_dots?: boolean;
393 gmail_remove_subaddress?: boolean;
394 gmail_convert_googlemaildotcom?: boolean;
395 outlookdotcom_lowercase?: boolean;
396 outlookdotcom_remove_subaddress?: boolean;
397 yahoo_lowercase?: boolean;
398 yahoo_remove_subaddress?: boolean;
399 icloud_lowercase?: boolean;
400 icloud_remove_subaddress?: boolean;
401 }
402
403 /**
404 * Options for isNumeric
405 */
406 interface IsNumericOptions {
407 no_symbols?: boolean;
408 }
409}
410
411/**
412 * MODULES
413 */
414declare var validator: ValidatorJS.ValidatorStatic;
415
416declare module "validator" {
417 export = validator;
418}
419
420// deprecated interfaces for backward compatibility, please use ValidatorJS.* instead the ones
421interface IValidatorStatic extends ValidatorJS.ValidatorStatic { }
422interface IURLoptions extends ValidatorJS.IsURLOptions { }
423interface IFQDNoptions extends ValidatorJS.IsFQDNOptions { }
424interface IEmailoptions extends ValidatorJS.NormalizeEmailOptions { }