Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

bilalfastnu
Copy link

@bilalfastnu bilalfastnu commented Oct 11, 2024

Description:

Add list of regex validations to reuse across multiple projects.

Breaking changes:

  • No

New features:

  • Yes

Fixes:

  • No

PR status:

  • Version bumped
  • Change-log updated
  • Tests added or updated
  • PR keeps 100% test coverage
  • Type definition updated
  • Readme updated
  • Linter is applied
  • Const arrow functions are used instead of pure function definitions where applicable
  • Functions are listed in alphabetic order in index.js, index.d.ts and readme

Copy link
Contributor

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

Copy link
Author

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.

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,}$/
Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to PASSWORD

const DIGITS = '0-9'

const REGEX_VALIDATION = {
NUMBER: new RegExp(`^[${DIGITS}]+$`),
Copy link
Contributor

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

Copy link
Author

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.

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]+$/,
Copy link
Contributor

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?

Copy link
Author

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})$

https://regex101.com/r/dR5ngE/1


const REGEX_VALIDATION = {
NUMBER: new RegExp(`^[${DIGITS}]+$`),
EMAIL: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
Copy link
Contributor

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

Suggested change
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,

Copy link
Author

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
Copy link
Contributor

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

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants