Skip to content

Commit

Permalink
feat: export supported locales for isAlpha, isMobilePhone, etc
Browse files Browse the repository at this point in the history
This commit exports supported locales for the following functions:
- isAlpha -> isAlphaLocales
- isAlphanumeric -> isAlphanumericLocales
- isMobilePhone -> isMobilePhoneLocales

fixes validatorjs#815
  • Loading branch information
profnandaa committed Sep 2, 2018
1 parent 6ce3060 commit 49efcf3
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ Validator | Description
***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).<br/><br/>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', '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', 'ku-IQ', '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']`) and defaults to `en-US`.
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<br/><br/>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', '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', 'ku-IQ', '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']`) and defaults to `en-US`.
**isAlpha(str [, locale])** | check if the string contains only letters (a-zA-Z).<br/><br/>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', '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', 'ku-IQ', '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']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`.
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<br/><br/>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', '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', 'ku-IQ', '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']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
**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.
Expand Down Expand Up @@ -105,12 +105,12 @@ Validator | Description
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_colons: false}`. If `no_colons` is true, the validator will allow MAC addresses without the colons.
**isMD5(str)** | check if the string is a MD5 hash.
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'es-MX','et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR defaults to 'any'. If 'any' or a falsey value is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'es-MX','et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR defaults to 'any'. If 'any' or a falsey value is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`.
**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.
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}`. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. locale list is `validator.isPostalCodeLocales`.).
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }`.
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ var validator = {
isFQDN: _isFQDN2.default,
isBoolean: _isBoolean2.default,
isAlpha: _isAlpha2.default,
isAlphaLocales: _isAlpha.locales,
isAlphanumeric: _isAlphanumeric2.default,
isAlphanumericLocales: _isAlphanumeric.locales,
isNumeric: _isNumeric2.default,
isPort: _isPort2.default,
isLowercase: _isLowercase2.default,
Expand Down Expand Up @@ -348,6 +350,7 @@ var validator = {
isISBN: _isISBN2.default,
isISSN: _isISSN2.default,
isMobilePhone: _isMobilePhone2.default,
isMobilePhoneLocales: _isMobilePhone.locales,
isPostalCode: _isPostalCode2.default,
isPostalCodeLocales: _isPostalCode.locales,
isCurrency: _isCurrency2.default,
Expand Down
4 changes: 3 additions & 1 deletion lib/isAlpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.locales = undefined;
exports.default = isAlpha;

var _assertString = require('./util/assertString');
Expand All @@ -22,4 +23,5 @@ function isAlpha(str) {
}
throw new Error('Invalid locale \'' + locale + '\'');
}
module.exports = exports['default'];

var locales = exports.locales = Object.keys(_alpha.alpha);
4 changes: 3 additions & 1 deletion lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.locales = undefined;
exports.default = isAlphanumeric;

var _assertString = require('./util/assertString');
Expand All @@ -22,4 +23,5 @@ function isAlphanumeric(str) {
}
throw new Error('Invalid locale \'' + locale + '\'');
}
module.exports = exports['default'];

var locales = exports.locales = Object.keys(_alpha.alphanumeric);
4 changes: 3 additions & 1 deletion lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.locales = undefined;
exports.default = isMobilePhone;

var _assertString = require('./util/assertString');
Expand Down Expand Up @@ -117,4 +118,5 @@ function isMobilePhone(str, locale, options) {
}
throw new Error('Invalid locale \'' + locale + '\'');
}
module.exports = exports['default'];

var locales = exports.locales = Object.keys(phones);
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import isFQDN from './lib/isFQDN';

import isBoolean from './lib/isBoolean';

import isAlpha from './lib/isAlpha';
import isAlphanumeric from './lib/isAlphanumeric';
import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha';
import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric';
import isNumeric from './lib/isNumeric';
import isPort from './lib/isPort';
import isLowercase from './lib/isLowercase';
Expand Down Expand Up @@ -63,7 +63,7 @@ import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';

import isMobilePhone from './lib/isMobilePhone';
import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone';

import isCurrency from './lib/isCurrency';

Expand Down Expand Up @@ -114,7 +114,9 @@ const validator = {
isFQDN,
isBoolean,
isAlpha,
isAlphaLocales,
isAlphanumeric,
isAlphanumericLocales,
isNumeric,
isPort,
isLowercase,
Expand Down Expand Up @@ -149,6 +151,7 @@ const validator = {
isISBN,
isISSN,
isMobilePhone,
isMobilePhoneLocales,
isPostalCode,
isPostalCodeLocales,
isCurrency,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/isAlpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export default function isAlpha(str, locale = 'en-US') {
}
throw new Error(`Invalid locale '${locale}'`);
}

export const locales = Object.keys(alpha);
2 changes: 2 additions & 0 deletions src/lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export default function isAlphanumeric(str, locale = 'en-US') {
}
throw new Error(`Invalid locale '${locale}'`);
}

export const locales = Object.keys(alphanumeric);
2 changes: 2 additions & 0 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ export default function isMobilePhone(str, locale, options) {
}
throw new Error(`Invalid locale '${locale}'`);
}

export const locales = Object.keys(phones);
18 changes: 18 additions & 0 deletions test/exports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
let assert = require('assert');
let validator = require('../index');
let isPostalCodeLocales = require('../lib/isPostalCode').locales;
const isAlphaLocales = require('../lib/isAlpha').locales;
const isAlphanumericLocales = require('../lib/isAlphanumeric').locales;
const isMobilePhoneLocales = require('../lib/isMobilePhone').locales;

describe('Exports', () => {
it('should export validators', () => {
Expand All @@ -26,4 +29,19 @@ describe('Exports', () => {
assert.ok(isPostalCodeLocales instanceof Array);
assert.ok(validator.isPostalCodeLocales instanceof Array);
});

it('should export isAlpha\'s supported locales', () => {
assert.ok(isAlphaLocales instanceof Array);
assert.ok(validator.isAlphaLocales instanceof Array);
});

it('should export isAlphanumeric\'s supported locales', () => {
assert.ok(isAlphanumericLocales instanceof Array);
assert.ok(validator.isAlphanumericLocales instanceof Array);
});

it('should export isMobilePhone\'s supported locales', () => {
assert.ok(isMobilePhoneLocales instanceof Array);
assert.ok(validator.isMobilePhoneLocales instanceof Array);
});
});
13 changes: 11 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ function isAlpha(str) {
throw new Error('Invalid locale \'' + locale + '\'');
}

var locales = Object.keys(alpha);

function isAlphanumeric(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';

Expand All @@ -626,6 +628,8 @@ function isAlphanumeric(str) {
throw new Error('Invalid locale \'' + locale + '\'');
}

var locales$1 = Object.keys(alphanumeric);

var numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
var numericNoSymbols = /^[0-9]+$/;

Expand Down Expand Up @@ -1151,6 +1155,8 @@ function isMobilePhone(str, locale, options) {
throw new Error('Invalid locale \'' + locale + '\'');
}

var locales$2 = Object.keys(phones);

function currencyRegex(options) {
var decimal_digits = '\\d{' + options.digits_after_decimal[0] + '}';
options.digits_after_decimal.forEach(function (digit, index) {
Expand Down Expand Up @@ -1436,7 +1442,7 @@ var patterns = {
ZM: fiveDigit
};

var locales = Object.keys(patterns);
var locales$3 = Object.keys(patterns);

var isPostalCode = function (str, locale) {
assertString(str);
Expand Down Expand Up @@ -1668,7 +1674,9 @@ var validator = {
isFQDN: isFQDN,
isBoolean: isBoolean,
isAlpha: isAlpha,
isAlphaLocales: locales,
isAlphanumeric: isAlphanumeric,
isAlphanumericLocales: locales$1,
isNumeric: isNumeric,
isPort: isPort,
isLowercase: isLowercase,
Expand Down Expand Up @@ -1703,8 +1711,9 @@ var validator = {
isISBN: isISBN,
isISSN: isISSN,
isMobilePhone: isMobilePhone,
isMobilePhoneLocales: locales$2,
isPostalCode: isPostalCode,
isPostalCodeLocales: locales,
isPostalCodeLocales: locales$3,
isCurrency: isCurrency,
isISO8601: isISO8601,
isRFC3339: isRFC3339,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 49efcf3

Please sign in to comment.