-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from bilalfastnu/regex-validations
Add regex validations
- Loading branch information
Showing
7 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#1.16.0 | ||
- feat: regex validations | ||
|
||
#1.15.0 | ||
- feat: snakeCase | ||
- feat: promiseResolveCb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable no-misleading-character-class */ | ||
'use strict' | ||
// https://v8.dev/features/regexp-v-flag | ||
// https://mathiasbynens.be/notes/es-unicode-property-escapes | ||
// https://www.regular-expressions.info/unicode.html | ||
|
||
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 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') | ||
const NAME_WITH_DIGITS = new RegExp(`^[-${CHARACTERS}${DIGITS}' &()/]+$`, 'u') | ||
const INPUT = new RegExp(`^[${INPUT_BASE_REG_EXP.source}]+$`, 'u') | ||
const ADDRESS = new RegExp(`^[${INPUT_BASE_REG_EXP.source}#]+$`, 'u') | ||
const PHONE_CODE = /^(\+?\d{1,3}|\d{1,4})$/ | ||
const PHONE = /^\+?(\d{1,3}|\d{1,4})?[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/ | ||
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,}$/ | ||
|
||
/** | ||
* @param {string} input | ||
* @param {'NAME'|'EMAIL'|..} format | ||
* @returns {boolean} | ||
* @throws {Error} | ||
*/ | ||
const validateInput = (input, format) => { | ||
switch (format) { | ||
case 'NAME': return NAME.test(input) | ||
case 'NAME_WITH_DIGITS': return NAME_WITH_DIGITS.test(input) | ||
case 'NUMBER': return NUMBER.test(input) | ||
case 'EMAIL': return EMAIL.test(input) | ||
case 'PATH': return PATH.test(input) | ||
case 'INPUT': return INPUT.test(input) | ||
case 'ADDRESS': return ADDRESS.test(input) | ||
case 'PHONE': return PHONE.test(input) | ||
case 'PHONE_CODE': return PHONE_CODE.test(input) | ||
case 'IMAGE': return IMAGE.test(input) | ||
case 'FILE': return FILE.test(input) | ||
case 'FILENAME': return FILENAME.test(input) | ||
case 'PASSWORD': return PASSWORD.test(input) | ||
|
||
default: throw new Error('ERR_FORMAT_NOT_SUPPORTED') | ||
} | ||
} | ||
|
||
module.exports = validateInput |
Oops, something went wrong.