-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add regex validations #33
base: main
Are you sure you want to change the base?
Conversation
src/validations.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm file name is very generic and also exposing a method similar to other cases, so it's not fitting well.
I think we should readjust this to expose a method maybe something like this:
// validateInput.js
const REG_INT = /^\d+$/
const REG_EMAIL: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
/**
* @param {string} input
* @param {'integer'|'emai'|..} format
* @returns {boolean}
* @throws {Error}
*/
const validateInput = (input, format) => {
switch(format) {
case 'integer': return REG_INT.test(input)
case 'email': return REG_EMAIL.test(input)
...
default: throw new Error('ERR_FORMAT_NOT_SUPPORTED')
}
}
module.exports = validateInput
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is better approach instead of exposing the regex, I have updated as a function module.
src/validations.js
Outdated
IMAGE: /^data:image\/[A-Za-z+]+;base64,[A-Za-z0-9+/=]+$/, | ||
FILE: /^data:(image|application|video)\/[A-Za-z0-9+]+;base64,[A-Za-z0-9+/=]+$/, | ||
FILENAME: new RegExp(`^[-${DIGITS}.${CHARACTERS} ()_]+$`, 'u'), | ||
ADMIN_PASSWORD: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]).{8,}$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one seems very strange naming, there's no specific rules for some admin passwords, maybe you can name it like common_password
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to PASSWORD
src/validations.js
Outdated
const DIGITS = '0-9' | ||
|
||
const REGEX_VALIDATION = { | ||
NUMBER: new RegExp(`^[${DIGITS}]+$`), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
number is also float and integer, so not correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabled to include both floating and signed numbers.
src/validations.js
Outdated
NAME_WITH_DIGITS: new RegExp(`^[-${CHARACTERS}${DIGITS}' &()/]+$`, 'u'), | ||
INPUT: new RegExp(`^[${INPUT_BASE_REG_EXP.source}]+$`, 'u'), | ||
ADDRESS: new RegExp(`^[${INPUT_BASE_REG_EXP.source}#]+$`, 'u'), | ||
PHONE_CODE: /^(\+)*[0-9]+$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think + should be mandatory in phone numbers, and we don't know about -, should we include it, or spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code format, for international numbers either starts with +
sign or 00
: with limit to number of digits. -
sign is not applicable to phone codes.
Updated the regex: ^(\+?\d{1,3}|\d{1,4})$
src/validations.js
Outdated
|
||
const REGEX_VALIDATION = { | ||
NUMBER: new RegExp(`^[${DIGITS}]+$`), | ||
EMAIL: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this regex fix better on rfc2822
EMAIL: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/, | |
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is updated per suggestions.
index.d.ts
Outdated
@@ -34,4 +34,5 @@ export function snakeCase (str: string): string | |||
export function sum (values: Array<string>): number | |||
export function sumBy (values: Array, iteratee: Function | string): number | |||
export function uniqBy (array: Array, iteratee: Function | string): Array | |||
export function without (array: Array, ...values: Array): Array | |||
export const validation : { NUMBER: RegExp; EMAIL: RegExp ; PATH: RegExp; NAME: RegExp; NAME_WITH_DIGITS: RegExp; INPUT: RegExp; ADDRESS: RegExp; PHONE_CODE: RegExp; IMAGE: RegExp; FILE: RegExp; FILENAME: RegExp; ADMIN_PASSWORD: RegExp } | |||
export function without (array: Array, ...values: Array): Array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I see newline missing at end of file, please restore it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line is present, but somehow Github review don't show the last empty line.
Description:
Add list of regex validations to reuse across multiple projects.
Breaking changes:
New features:
Fixes:
PR status: