Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Validator | Description
**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 `['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.<br/><br/>`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.<br/><br/>`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 `.`).<br/><br/>`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.<br/><br/>`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`.
Expand Down Expand Up @@ -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

2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -246,6 +247,7 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isNanoID,
};

export default validator;
26 changes: 26 additions & 0 deletions src/lib/isNanoID.js
Original file line number Diff line number Diff line change
@@ -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' });
46 changes: 46 additions & 0 deletions test/validators/isDecimal.test.js
Original file line number Diff line number Diff line change
@@ -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'],
});
});
});
56 changes: 56 additions & 0 deletions test/validators/isNanoID.test.js
Original file line number Diff line number Diff line change
@@ -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'],
});
});
});
Loading