From 30a381e2eeb120a301cdc6bf54393bf2ac1c234d Mon Sep 17 00:00:00 2001 From: iwllyu Date: Tue, 26 Sep 2017 14:03:43 -0400 Subject: [PATCH] add isPostalCode (#20042) --- types/validator/index.d.ts | 15 ++++++++++- types/validator/validator-tests.ts | 40 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/types/validator/index.d.ts b/types/validator/index.d.ts index 73ca4f0860..e27b1e70c0 100644 --- a/types/validator/index.d.ts +++ b/types/validator/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for validator.js v6.2 +// Type definitions for validator.js v6.3 // Project: https://github.com/chriso/validator.js // Definitions by: tgfjt // Ilya Mochalov @@ -12,6 +12,7 @@ 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" | "de-DE" | "en-AU" | "en-GB" | "en-HK" | "en-IN" | "en-NZ" | "en-US" | "en-ZA" | "en-ZM" | "es-ES" | "fr-FR" | "hu-HU" | "nl-NL" | "pl-PL" | "pt-BR" | "pt-PT" | "ru-RU" | "sr-RS" | "sr-RS@latin" | "tr-TR"; 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" | "de-DE" | "en-AU" | "en-GB" | "en-HK" | "en-IN" | "en-NZ" | "en-US" | "en-ZA" | "en-ZM" | "es-ES" | "fr-FR" | "fr-BE" | "hu-HU" | "nl-BE" | "nl-NL" | "pl-PL" | "pt-BR" | "pt-PT" | "ru-RU" | "sr-RS" | "sr-RS@latin" | "tr-TR"; type MobilePhoneLocale = "ar-DZ" | "ar-SA" | "ar-SY" | "cs-CZ" | "de-DE" | "da-DK" | "el-GR" | "en-AU" | "en-GB" | "en-HK" | "en-IN" | "en-NZ" | "en-US" | "en-CA" | "en-ZA" | "en-ZM" | "es-ES" | "fi-FI" | "fr-FR" | "hu-HU" | "it-IT" | "ja-JP" | "ko-KR" | "ms-MY" | "nb-NO" | "nn-NO" | "pl-PL" | "pt-PT" | "ru-RU" | "sr-RS" | "tr-TR" | "vi-VN" | "zh-CN" | "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" interface ValidatorStatic { @@ -154,6 +155,13 @@ declare namespace ValidatorJS { // check if the string contains only numbers. isNumeric(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', + // 'TW', 'US', 'ZA', 'ZM' ]) OR 'any'. If 'any' is used, function will check if any of the + // locales match). + isPostalCode(str: string, locale: PostalCodeLocale): boolean; + // check if the string contains any surrogate pairs chars. isSurrogatePair(str: string): boolean; @@ -513,6 +521,11 @@ declare module "validator/lib/isMobilePhone" { export = isMobilePhone; } +declare module "validator/lib/isPostalCode" { + const isPostalCode: typeof validator.isPostalCode; + export = isPostalCode; +} + declare module "validator/lib/isMongoId" { const isMongoId: typeof validator.isMongoId; export = isMongoId; diff --git a/types/validator/validator-tests.ts b/types/validator/validator-tests.ts index 353f3a31b7..2bda229ea6 100644 --- a/types/validator/validator-tests.ts +++ b/types/validator/validator-tests.ts @@ -47,6 +47,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 isPostalCodeFunc = require('validator/lib/isPostalCode'); import isSurrogatePairFunc = require('validator/lib/isSurrogatePair'); import isURLFunc = require('validator/lib/isURL'); import isUUIDFunc = require('validator/lib/isUUID'); @@ -193,6 +194,9 @@ import whitelistFunc = require('validator/lib/whitelist'); let _isNumeric = validator.isNumeric; _isNumeric = isNumericFunc; + let _isPostalCode = validator.isPostalCode; + _isPostalCode = isPostalCodeFunc; + let _isSurrogatePair = validator.isSurrogatePair; _isSurrogatePair = isSurrogatePairFunc; @@ -472,6 +476,42 @@ let any: any; result = validator.isNumeric('sample'); + result = validator.isPostalCode('sample', 'AT'); + result = validator.isPostalCode('sample', 'AU'); + result = validator.isPostalCode('sample', 'BE'); + result = validator.isPostalCode('sample', 'CA'); + result = validator.isPostalCode('sample', 'CH'); + result = validator.isPostalCode('sample', 'CZ'); + result = validator.isPostalCode('sample', 'DE'); + result = validator.isPostalCode('sample', 'DK'); + result = validator.isPostalCode('sample', 'DZ'); + result = validator.isPostalCode('sample', 'ES'); + result = validator.isPostalCode('sample', 'FI'); + result = validator.isPostalCode('sample', 'FR'); + result = validator.isPostalCode('sample', 'GB'); + result = validator.isPostalCode('sample', 'GR'); + result = validator.isPostalCode('sample', 'IL'); + result = validator.isPostalCode('sample', 'IN'); + result = validator.isPostalCode('sample', 'IS'); + result = validator.isPostalCode('sample', 'IT'); + result = validator.isPostalCode('sample', 'JP'); + result = validator.isPostalCode('sample', 'KE'); + result = validator.isPostalCode('sample', 'LI'); + result = validator.isPostalCode('sample', 'MX'); + result = validator.isPostalCode('sample', 'NL'); + result = validator.isPostalCode('sample', 'NO'); + result = validator.isPostalCode('sample', 'PL'); + result = validator.isPostalCode('sample', 'PT'); + result = validator.isPostalCode('sample', 'RO'); + result = validator.isPostalCode('sample', 'RU'); + result = validator.isPostalCode('sample', 'SA'); + result = validator.isPostalCode('sample', 'SE'); + result = validator.isPostalCode('sample', 'TW'); + result = validator.isPostalCode('sample', 'US'); + result = validator.isPostalCode('sample', 'ZA'); + result = validator.isPostalCode('sample', 'ZM'); + result = validator.isPostalCode('sample', 'any'); + result = validator.isSurrogatePair('sample'); let isURLOptions: ValidatorJS.IsURLOptions;