From 0b926af02dc124f610d5ff592ad47aaec1320eaa Mon Sep 17 00:00:00 2001 From: Naoto Yokoyama Date: Tue, 20 Feb 2018 22:11:22 +0900 Subject: [PATCH] validator (v9.0): add isPort(), some options, and locales Added option for isFloat(): - locale Added options for isDecimal(): - locale - force_decimal - decimal_digits Added locales: - uk-UA (https://github.com/chriso/validator.js/pull/731) --- types/validator/index.d.ts | 27 +++++++++++++++++++++++---- types/validator/validator-tests.ts | 9 +++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/types/validator/index.d.ts b/types/validator/index.d.ts index 6588570a1a..c4dd7a59c3 100644 --- a/types/validator/index.d.ts +++ b/types/validator/index.d.ts @@ -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 // Ilya Mochalov @@ -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"; @@ -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; @@ -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 @@ -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', @@ -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; @@ -299,6 +312,7 @@ declare namespace ValidatorJS { interface IsFloatOptions { min?: number; max?: number; + locale?: FloatLocale; } // options for IsInt @@ -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; diff --git a/types/validator/validator-tests.ts b/types/validator/validator-tests.ts index bf3d74cb78..02d0510be2 100644 --- a/types/validator/validator-tests.ts +++ b/types/validator/validator-tests.ts @@ -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'); @@ -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; @@ -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); @@ -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'); @@ -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');