diff --git a/README.md b/README.md index e8b7aedd9..cce8b1efa 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ Validator | Description **isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-AM', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-LB', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LY', 'ar-MA', 'ar-OM', 'ar-PS', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'az-AZ', 'ar-QA', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'el-CY', 'en-AI', 'en-AU', 'en-AG', 'en-BM', 'en-BS', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'fr-CF', 'en-SS', 'en-KI', 'en-KN', 'en-LS', 'en-MT', 'en-MU', 'en-MW', 'en-NA', 'en-NG', 'en-NZ', 'en-PG', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'en-BW', 'es-AR', 'es-BO', 'es-CO', 'es-CL', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-EC', 'es-ES', 'es-GT', 'es-PE', 'es-MX', 'es-NI', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CM', 'fr-DJ', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'lv-LV', 'mg-MG', 'mn-MN', 'my-MM', 'ms-MY', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nl-AW', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-MD', 'ro-RO', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tr-TR', 'tk-TM', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-TW', 'dz-BT', 'ar-YE', 'ar-EH', 'fa-AF', 'mk-MK', 'en-CA', 'fr-CA', 'fr-BE', 'zh-HK', 'zh-MO', 'ga-IE', 'fr-CH', 'it-CH', 'en-CM']` OR defaults to `'any'`. If `any` or a falsey value is used, the function will check if any of the locales match.

`options` is an optional object that can be supplied with the following key: `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. +**isNanoID(str [, options])** | check if the string is a valid Nano ID. `options` is an object which defaults to `{ alphabets: 'A-Za-z0-9', length: '1,', hyphen: true, underscore: true }`. `alphabets` specifies the allowed characters, `length` specifies the allowed length or length range, and `hyphen` and `underscore` control whether hyphens and underscores are allowed. **isNumeric(str [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

`locale` determines the decimal separator and 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', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. **isOctal(str)** | check if the string is a valid octal number. **isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']`. Locale list is `validator.passportNumberLocales`. @@ -268,4 +269,3 @@ This project is licensed under the [MIT](LICENSE). See the [LICENSE](LICENSE) fi [mongoid]: http://docs.mongodb.org/manual/reference/object-id/ [RFC 3339]: https://tools.ietf.org/html/rfc3339 [VAT Number]: https://en.wikipedia.org/wiki/VAT_identification_number - diff --git a/src/index.js b/src/index.js index 5e2cd78a7..7a597ee97 100644 --- a/src/index.js +++ b/src/index.js @@ -130,6 +130,7 @@ import isLicensePlate from './lib/isLicensePlate'; import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; +import isNanoID from './lib/isNanoID'; const version = '13.15.35'; @@ -246,6 +247,7 @@ const validator = { isLicensePlate, isVAT, ibanLocales, + isNanoID, }; export default validator; diff --git a/src/lib/isNanoID.js b/src/lib/isNanoID.js new file mode 100644 index 000000000..a4a9f92f0 --- /dev/null +++ b/src/lib/isNanoID.js @@ -0,0 +1,26 @@ +import assertString from './util/assertString'; +import merge from './util/merge'; + +function regexExp(options) { + const alphabets = options.alphabets; + const underscore = options.underscore ? '_' : ''; + const hyphen = options.hyphen ? '-' : ''; + const charClass = `${alphabets}${underscore}${hyphen}`; + const regExp = new RegExp(`^[${charClass}]{${options.length}}$`); + return regExp; +} + +const default_options = { + alphabets: 'A-Za-z0-9', + length: '1,', + hyphen: true, + underscore: true, +}; + +export default function isNanoID(str, options) { + assertString(str); + options = merge(options, default_options); + return regexExp(options).test(str); +} + +isNanoID('ABC456', { alphabets: 'ABCDEF123456' }); diff --git a/test/validators/isDecimal.test.js b/test/validators/isDecimal.test.js new file mode 100644 index 000000000..8da276cd6 --- /dev/null +++ b/test/validators/isDecimal.test.js @@ -0,0 +1,46 @@ +import test from '../testFunctions'; + +describe('isDecimal', () => { + it('Checks whether a number is Decimal or not', () => { + test({ + validator: 'isDecimal', + valid: ['1.2', '+2.4', '-122.5'], + invalid: ['+-2.4'], + }); + test({ + validator: 'isDecimal', + args: [{ locale: 'ar', force_decimal: false, decimal_digits: '1' }], + valid: ['1٫2', '+2٫4', '-122٫5'], + invalid: ['+-2٫4', '2.4', '12٫8979', '1,2'], + }); + test({ + validator: 'isDecimal', + valid: ['1.2', '+2.4', '-122.5', '.5', '+.5', '-.5', '0.123', '1.234'], + invalid: ['', ' ', '+', '-', '1,2', '1.', ' 1.2', '1.2 ', '+-2.4'], + }); + test({ + validator: 'isDecimal', + args: [{ force_decimal: true }], + valid: ['1.0', '+.5', '-.5', '0.12', '.1'], + invalid: ['1', '2', '', '1.', ' 1.0'], + }); + test({ + validator: 'isDecimal', + args: [{ force_decimal: true, decimal_digits: '2' }], + valid: ['1.23', '+.12', '-0.12', '0.00'], + invalid: ['1.2', '1.234', '1', '.1', '.123'], + }); + test({ + validator: 'isDecimal', + args: [{ decimal_digits: '1,3' }], + valid: ['1.2', '1.23', '1.234', '.1', '.12', '.123'], + invalid: ['1.', '1.2345', '.1234'], + }); + test({ + validator: 'isDecimal', + args: [{ locale: 'ar' }], + valid: ['1٫2', '٫5', '+٫5', '-٫5'], + invalid: ['1.2', '1,2', '1٫', ' 1٫2'], + }); + }); +}); diff --git a/test/validators/isNanoID.test.js b/test/validators/isNanoID.test.js new file mode 100644 index 000000000..1703e7ce0 --- /dev/null +++ b/test/validators/isNanoID.test.js @@ -0,0 +1,56 @@ +import test from '../testFunctions'; + +describe('isNanoID', () => { + it('default options', () => { + test({ + validator: 'isNanoID', + valid: ['Aiugiu645654sdf', 'D87D', '_-jj', 'A_B-C'], + invalid: ['+999', 'RGRY$', ' ', '*abc'], + }); + }); + + it('custom alphabets', () => { + test({ + validator: 'isNanoID', + args: [{ alphabets: 'ABCDEF123' }], + valid: ['ACABE', '123123', 'A1_C', 'F-1'], + invalid: [ + '+999', + 'RGRY$', + ' ', + 'Aiugiu645654sdf', + 'D87D', + 'aoihdof', + '_-jj', + 'G12', + ], + }); + }); + + it('hyphen disabled', () => { + test({ + validator: 'isNanoID', + args: [{ hyphen: false }], + valid: ['ACABE', '123123', '_jj', 'Aiugiu645654sdf', 'D87D'], + invalid: ['-999', 'A-B', 'RGRY$', ' '], + }); + }); + + it('underscore disabled', () => { + test({ + validator: 'isNanoID', + args: [{ underscore: false }], + valid: ['ACABE', '123123', '-jj', 'Aiugiu645654sdf', 'D87D'], + invalid: ['_123', 'A_B', 'RGRY$', ' '], + }); + }); + + it('fixed length', () => { + test({ + validator: 'isNanoID', + args: [{ length: 6 }], + valid: ['ABC123', 'a1B_9-'], + invalid: ['ABC12', 'ABC1234', 'ABC+12'], + }); + }); +});