From fa0532f75f8eeda4738f0de85548ca33872d5c3b Mon Sep 17 00:00:00 2001 From: Durgeshwar-AI Date: Tue, 14 Oct 2025 20:37:27 +0530 Subject: [PATCH 1/2] Created tests for isDecimal --- test/validators/isDecimal.test.js | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/validators/isDecimal.test.js 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'], + }); + }); +}); From 9be4820f74f35de0e211c5006417ba69c4721167 Mon Sep 17 00:00:00 2001 From: Durgeshwar-AI Date: Tue, 14 Oct 2025 22:34:31 +0530 Subject: [PATCH 2/2] Created the validator for NanoID checking --- src/index.js | 2 ++ src/lib/isNanoID.js | 26 +++++++++++++++ test/validators/isNanoID.test.js | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/lib/isNanoID.js create mode 100644 test/validators/isNanoID.test.js diff --git a/src/index.js b/src/index.js index 87be7113c..ff80eb730 100644 --- a/src/index.js +++ b/src/index.js @@ -129,6 +129,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.15'; @@ -245,6 +246,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/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'], + }); + }); +});