From 6c8803390c8a3dbff920eb39f311dd6a627c4f71 Mon Sep 17 00:00:00 2001 From: muhammadbilal Date: Wed, 27 Nov 2024 13:41:36 +1100 Subject: [PATCH 1/2] Add URL regex validation --- index.d.ts | 2 +- src/validateInput.js | 4 +++- test/validateInput.js | 26 +++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8fae2ab..adb3c52 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,5 +44,5 @@ export function union (...arrays: Array[]): Array export function uniqBy (array: Array, iteratee: Function | string): Array export function uniqWith (array: Array, comparator: Function): Array export function update (obj: Object, path: string | Array, updater: Function): Object -export function validateInput ( input: string, format: string ): Boolean +export function validateInput ( input: string, format: 'NUMBER' | 'EMAIL' | 'PATH' | 'NAME' | 'NAME_WITH_DIGITS' | 'INPUT' | 'ADDRESS' | 'PHONE_CODE' | 'PHONE' | 'IMAGE' | 'FILE' | 'FILENAME' | 'PASSWORD' | 'URL' ): Boolean export function without (array: Array, ...values: Array): Array diff --git a/src/validateInput.js b/src/validateInput.js index 43f147a..c678a28 100644 --- a/src/validateInput.js +++ b/src/validateInput.js @@ -8,7 +8,7 @@ const CHARACTERS = '\\p{L}\\p{M}' const INPUT_BASE_REG_EXP = new RegExp(`-0-9.,;:<>?/\n${CHARACTERS}'_ &()/+@%`, 'u') const DIGITS = '0-9' -const NUMBER = /^-?\d*[.]?\d+$/ +const NUMBER = /^[+-]?\d*[.]?\d+$/ const EMAIL = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i const PATH = /([^/]*)\/*$/ const NAME = new RegExp(`^[-${CHARACTERS}' &()/]+$`, 'u') @@ -21,6 +21,7 @@ const IMAGE = /^data:image\/[A-Za-z+]+;base64,[A-Za-z0-9+/=]+$/ const FILE = /^data:(image|application|video)\/[A-Za-z0-9+]+;base64,[A-Za-z0-9+/=]+$/ const FILENAME = new RegExp(`^[-${DIGITS}.${CHARACTERS} ()_]+$`, 'u') const PASSWORD = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]).{8,}$/ +const URL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/ /** * @param {string} input @@ -43,6 +44,7 @@ const validateInput = (input, format) => { case 'FILE': return FILE.test(input) case 'FILENAME': return FILENAME.test(input) case 'PASSWORD': return PASSWORD.test(input) + case 'URL': return URL.test(input) default: throw new Error('ERR_FORMAT_NOT_SUPPORTED') } diff --git a/test/validateInput.js b/test/validateInput.js index ef8cec2..cfcc729 100644 --- a/test/validateInput.js +++ b/test/validateInput.js @@ -253,7 +253,7 @@ describe('validations', () => { assert.strictEqual(validateInput('invalid?file.txt', 'FILENAME'), false) // Contains ? }) - it('ADMIN_PASSWORD, validate password', () => { + it('PASSWORD, validate password', () => { // valid admin password assert.strictEqual(validateInput('A1!strongpass', 'PASSWORD'), true) assert.strictEqual(validateInput('Secure#Password1', 'PASSWORD'), true) @@ -275,6 +275,30 @@ describe('validations', () => { assert.strictEqual(validateInput('Password!', 'PASSWORD'), false) // No digits }) + it('URL, validate url', () => { + assert.strictEqual(validateInput('http://example.com', 'URL'), true) + assert.strictEqual(validateInput('https://example.com', 'URL'), true) + assert.strictEqual(validateInput('http://www.example.com', 'URL'), true) + assert.strictEqual(validateInput('https://www.example.com', 'URL'), true) + assert.strictEqual(validateInput('https://sub.example.com', 'URL'), true) + assert.strictEqual(validateInput('https://example.com/path/to/page', 'URL'), true) + assert.strictEqual(validateInput('https://example.com/path?name=value', 'URL'), true) + assert.strictEqual(validateInput('https://example.com/path#section', 'URL'), true) + assert.strictEqual(validateInput('https://example.com/path?name=value#section', 'URL'), true) + assert.strictEqual(validateInput('https://example.com:8080', 'URL'), true) + assert.strictEqual(validateInput('https://example..com', 'URL'), true) + assert.strictEqual(validateInput('https://example.x', 'URL'), true) + assert.strictEqual(validateInput('https://example.com/path#', 'URL'), true) + + // invalid url + assert.strictEqual(validateInput('https://', 'URL'), false) + assert.strictEqual(validateInput('example.com', 'URL'), false) + assert.strictEqual(validateInput('ftp://example.com', 'URL'), false) + assert.strictEqual(validateInput('https://example', 'URL'), false) + assert.strictEqual(validateInput('https://example com', 'URL'), false) + assert.strictEqual(validateInput('https://#section', 'URL'), false) + }) + it('should handle unsupported format', () => { assert.throws(() => validateInput('test', 'UNSUPPORTED_FORMAT'), Error) }) From 0bff1e02473cbef5b634cc8cc5a61b3cc141ef27 Mon Sep 17 00:00:00 2001 From: muhammadbilal Date: Wed, 27 Nov 2024 14:48:11 +1100 Subject: [PATCH 2/2] update PR info --- CHANGELOG | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 74fcd4a..9d218af 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +#1.18.1 +- feat: Add URL validation + #1.18.0 - feat: isUndefined diff --git a/package.json b/package.json index bb4a294..5000a05 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitfinexcom/lib-js-util-base", - "version": "1.18.0", + "version": "1.18.1", "description": "general utils", "main": "index.js", "scripts": {