From d161e65cb21d9442a7a08aac9cbcbbe356560062 Mon Sep 17 00:00:00 2001 From: saxicek Date: Mon, 4 Apr 2016 13:06:15 +0200 Subject: [PATCH 1/2] add czech localization --- README.md | 6 ++--- src/lib/alpha.js | 2 ++ src/lib/isMobilePhone.js | 1 + test/validators.js | 51 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fefea2000..91c52cf9d 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ Passing anything other than a string is an error. - **contains(str, seed)** - check if the string contains the seed. - **equals(str, comparison)** - check if the string matches the comparison. - **isAfter(str [, date])** - check if the string is a date that's after the specified date (defaults to now). -- **isAlpha(str [, locale])** - check if the string contains only letters (a-zA-Z). Locale is one of `['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', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']`) and defaults to `en-US`. -- **isAlphanumeric(str [, locale])** - check if the string contains only letters and numbers. Locale is one of `['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', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']`) and defaults to `en-US`. +- **isAlpha(str [, locale])** - check if the string contains only letters (a-zA-Z). Locale is one of `['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', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']`) and defaults to `en-US`. +- **isAlphanumeric(str [, locale])** - check if the string contains only letters and numbers. Locale is one of `['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', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']`) and defaults to `en-US`. - **isAscii(str)** - check if the string contains ASCII chars only. - **isBase64(str)** - check if a string is base64 encoded. - **isBefore(str [, date])** - check if the string is a date that's before the specified date. @@ -85,7 +85,7 @@ Passing anything other than a string is an error. - **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. - **isLowercase(str)** - check if the string is lowercase. - **isMACAddress(str)** - check if the string is a MAC address. -- **isMobilePhone(str, locale)** - check if the string is a mobile phone number, (locale is one of `['ar-SY', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fi-FI', 'fr-FR', 'ms-MY', 'nb-NO', 'nn-NO', 'pt-PT', 'ru-RU', 'tr-TR', 'vi-VN', 'zh-CN', 'zh-TW']`). +- **isMobilePhone(str, locale)** - check if the string is a mobile phone number, (locale is one of `['ar-SY', 'cs-CZ', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fi-FI', 'fr-FR', 'ms-MY', 'nb-NO', 'nn-NO', 'pt-PT', 'ru-RU', 'tr-TR', 'vi-VN', 'zh-CN', 'zh-TW']`). - **isMongoId(str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid]. - **isMultibyte(str)** - check if the string contains one or more multibyte chars. - **isNull(str)** - check if the string is null (has a length of zero). diff --git a/src/lib/alpha.js b/src/lib/alpha.js index 934667313..a340bf4b3 100644 --- a/src/lib/alpha.js +++ b/src/lib/alpha.js @@ -1,5 +1,6 @@ export const alpha = { 'en-US': /^[A-Z]+$/i, + 'cs-CZ': /^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i, 'de-DE': /^[A-ZÄÖÜß]+$/i, 'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i, 'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i, @@ -13,6 +14,7 @@ export const alpha = { export const alphanumeric = { 'en-US': /^[0-9A-Z]+$/i, + 'cs-CZ': /^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i, 'de-DE': /^[0-9A-ZÄÖÜß]+$/i, 'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i, 'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i, diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index c96af2f8e..051a1517d 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -4,6 +4,7 @@ import assertString from './util/assertString'; const phones = { 'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, 'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/, + 'cs-CZ': /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/, 'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/, 'el-GR': /^(\+?30)?(69\d{8})$/, 'en-AU': /^(\+?61|0)4\d{8}$/, diff --git a/test/validators.js b/test/validators.js index ccd99033a..273b98223 100644 --- a/test/validators.js +++ b/test/validators.js @@ -559,6 +559,25 @@ describe('Validators', function () { }); }); + it('should validate czech alpha strings', function () { + test({ + validator: 'isAlpha', + args: ['cs-CZ'], + valid: [ + 'žluťoučký', + 'KŮŇ', + 'Pěl', + 'Ďábelské', + 'ódy' + ], + invalid: [ + 'ábc1', + ' fůj ', + '', + ], + }); + }); + it('should validate german alpha strings', function () { test({ validator: 'isAlpha', @@ -577,7 +596,6 @@ describe('Validators', function () { }); }); - it('should validate arabic alpha strings', function () { test({ validator: 'isAlpha', @@ -676,6 +694,21 @@ describe('Validators', function () { }); }); + it('should validate czech alphanumeric strings', function () { + test({ + validator: 'isAlphanumeric', + args: ['cs-CZ'], + valid: [ + 'řiť123', + 'KŮŇ11', + ], + invalid: [ + 'řiď ', + 'blé!!', + ], + }); + }); + it('should validate german alphanumeric strings', function () { test({ validator: 'isAlphanumeric', @@ -1723,6 +1756,22 @@ describe('Validators', function () { args: ['ar-SY'], }); + test({ + validator: 'isMobilePhone', + valid: [ + '+420 123 456 789', + '+420 123456789', + '+420123456789', + '123 456 789', + '123456789', + ], + invalid: [ + '', + '+42012345678', + ], + args: ['cs-CZ'], + }); + test({ validator: 'isMobilePhone', valid: [ From c698274494ea8ffc5cdb85d491068767751dc846 Mon Sep 17 00:00:00 2001 From: saxicek Date: Mon, 4 Apr 2016 13:18:33 +0200 Subject: [PATCH 2/2] fix missing comma --- test/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/validators.js b/test/validators.js index 273b98223..d933ca146 100644 --- a/test/validators.js +++ b/test/validators.js @@ -568,7 +568,7 @@ describe('Validators', function () { 'KŮŇ', 'Pěl', 'Ďábelské', - 'ódy' + 'ódy', ], invalid: [ 'ábc1',