Skip to content

Commit

Permalink
Merge pull request #36 from bilalfastnu/input-validations
Browse files Browse the repository at this point in the history
Add URL regex validation
  • Loading branch information
vigan-abd authored Nov 27, 2024
2 parents a39b688 + 0bff1e0 commit 551dfe6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#1.18.1
- feat: Add URL validation

#1.18.0
- feat: isUndefined

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | number>, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion src/validateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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')
}
Expand Down
26 changes: 25 additions & 1 deletion test/validateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down

0 comments on commit 551dfe6

Please sign in to comment.