Skip to content

Commit

Permalink
validator (v9.0): add isPort(), some options, and locales
Browse files Browse the repository at this point in the history
Added option for isFloat():
  - locale

Added options for isDecimal():
  - locale
  - force_decimal
  - decimal_digits

Added locales:
  - uk-UA (validatorjs/validator.js#731)
  • Loading branch information
builtinnya committed Feb 20, 2018
1 parent 9318ba8 commit 0b926af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
27 changes: 23 additions & 4 deletions types/validator/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for validator.js v8.2
// Type definitions for validator.js v9.0
// Project: https://github.com/chriso/validator.js
// Definitions by: tgfjt <https://github.com/tgfjt>
// Ilya Mochalov <https://github.com/chrootsu>
Expand All @@ -12,7 +12,9 @@
declare namespace ValidatorJS {
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" | "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";
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" | "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";
type MobilePhoneLocale = "ar-AE" | "ar-DZ" | "ar-EG" | "ar-JO" | "ar-SA" | "ar-SY" | "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-TZ" | "en-PK" | "en-US" | "en-CA" | "en-ZA" | "en-ZM" | "es-ES" | "fa-IR" | "fi-FI" | "fr-FR" | "he-IL" | "hu-HU" | "id-ID" | "it-IT" | "ja-JP" | "ko-KR" | "lt-LT" | "ms-MY" | "nb-NO" | "nn-NO" | "pl-PL" | "pt-PT" | "ro-RO" | "ru-RU" | "sk-SK" | "sr-RS" | "tr-TR" | "vi-VN" | "zh-CN" | "zh-HK" | "zh-TW" | "any";
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" | "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";
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" | "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";
type MobilePhoneLocale = "ar-AE" | "ar-DZ" | "ar-EG" | "ar-JO" | "ar-SA" | "ar-SY" | "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-TZ" | "en-PK" | "en-US" | "en-CA" | "en-ZA" | "en-ZM" | "es-ES" | "fa-IR" | "fi-FI" | "fr-FR" | "he-IL" | "hu-HU" | "id-ID" | "it-IT" | "ja-JP" | "ko-KR" | "lt-LT" | "ms-MY" | "nb-NO" | "nn-NO" | "pl-PL" | "pt-PT" | "ro-RO" | "ru-RU" | "sk-SK" | "sr-RS" | "tr-TR" | "uk-UA" | "vi-VN" | "zh-CN" | "zh-HK" | "zh-TW" | "any";
type PostalCodeLocale = "AT" | "AU" | "BE" | "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"
type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";

Expand Down Expand Up @@ -71,7 +73,7 @@ declare namespace ValidatorJS {
isDataURI(str: string): boolean;

// check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
isDecimal(str: string): boolean;
isDecimal(str: string, options?: IsDecimalOptions): boolean;

// check if the string is a number that's divisible by another.
isDivisibleBy(str: string, number: number): boolean;
Expand Down Expand Up @@ -152,7 +154,8 @@ declare namespace ValidatorJS {
// 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-UG', 'en-RW', 'en-TZ',
// 'en-PK', 'en-US', 'en-CA', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL',
// 'hu-HU', 'id-ID', 'it-IT', 'ja-JP', 'ko-KR', lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL',
// 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']).
// 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK',
// 'zh-TW']).
isMobilePhone(str: string, locale: MobilePhoneLocale): boolean;

// check if the string is a valid hex-encoded representation of a MongoDB ObjectId
Expand All @@ -168,6 +171,9 @@ declare namespace ValidatorJS {
// check if the string contains only numbers.
isNumeric(str: string): boolean;

// check if the string is a valid port number.
isPort(str: string): boolean;

// check if the string is a postal code, (locale is one of
// [ 'AT', 'AU', 'BE', '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',
Expand Down Expand Up @@ -281,6 +287,13 @@ declare namespace ValidatorJS {
allow_space_after_digits?: boolean;
}

// options for isDecimal
interface IsDecimalOptions {
force_decimal?: boolean;
decimal_digits?: string;
locale?: DecimalLocale;
}

// options for isEmail
interface IsEmailOptions {
allow_display_name?: boolean;
Expand All @@ -299,6 +312,7 @@ declare namespace ValidatorJS {
interface IsFloatOptions {
min?: number;
max?: number;
locale?: FloatLocale;
}

// options for IsInt
Expand Down Expand Up @@ -572,6 +586,11 @@ declare module "validator/lib/isNumeric" {
export = isNumeric;
}

declare module "validator/lib/isPort" {
const isPort: typeof validator.isPort;
export = isPort;
}

declare module "validator/lib/isSurrogatePair" {
const isSurrogatePair: typeof validator.isSurrogatePair;
export = isSurrogatePair;
Expand Down
9 changes: 9 additions & 0 deletions types/validator/validator-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import isMongoIdFunc = require('validator/lib/isMongoId');
import isMultibyteFunc = require('validator/lib/isMultibyte');
import isNullFunc = require('validator/lib/isNull');
import isNumericFunc = require('validator/lib/isNumeric');
import isPortFunc = require('validator/lib/isPort');
import isPostalCodeFunc = require('validator/lib/isPostalCode');
import isSurrogatePairFunc = require('validator/lib/isSurrogatePair');
import isURLFunc = require('validator/lib/isURL');
Expand Down Expand Up @@ -202,6 +203,9 @@ import whitelistFunc = require('validator/lib/whitelist');
let _isNumeric = validator.isNumeric;
_isNumeric = isNumericFunc;

let _isPort = validator.isPort;
_isPort = isPortFunc;

let _isPostalCode = validator.isPostalCode;
_isPostalCode = isPostalCodeFunc;

Expand Down Expand Up @@ -396,7 +400,9 @@ let any: any;

result = validator.isDataURI('sample');

let isDecimalOptions: ValidatorJS.IsDecimalOptions;
result = validator.isDecimal('sample');
result = validator.isDecimal('sample', isDecimalOptions);

result = validator.isDivisibleBy('sample', 2);

Expand Down Expand Up @@ -513,6 +519,7 @@ let any: any;
result = validator.isMobilePhone('sample', 'sr-RS');
result = validator.isMobilePhone('sample', 'sk-SK');
result = validator.isMobilePhone('sample', 'tr-TR');
result = validator.isMobilePhone('sample', 'uk-UA');
result = validator.isMobilePhone('sample', 'vi-VN');
result = validator.isMobilePhone('sample', 'zh-CN');
result = validator.isMobilePhone('sample', 'zh-HK');
Expand All @@ -527,6 +534,8 @@ let any: any;

result = validator.isNumeric('sample');

result = validator.isPort('sample');

result = validator.isPostalCode('sample', 'AT');
result = validator.isPostalCode('sample', 'AU');
result = validator.isPostalCode('sample', 'BE');
Expand Down

0 comments on commit 0b926af

Please sign in to comment.