npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@8baselabs/validation-utils

v0.0.2

Published

This is package to deal with common scenarios working with Javascript validations

Downloads

10

Readme

Validation utils

This is a package for functions that are use full in common cases for validations.

Index

| Function | Description | | ------ | ------ | | isNullOrUndefined | Validate if a value is null or undefined. | | isValidString | Validate if a value is a valid string or not. |

isNullOrUndefined(value)

  • Returns true for null and undefined values.

Example:

console.log(isNullOrUndefined(null));
// true
console.log(isNullOrUndefined(undefined));
// true
console.log(isNullOrUndefined(true));
// false

isValidString(value)

  • Validate if a value is a valid string or not.
  • Optional validates empty strings
  • Optional validates size fo the string

Example:

console.log(isValidString(null));
// false
console.log(isValidString(undefined));
// false
console.log(isValidString(true));
// false
console.log(isValidString(""));
// false
console.log(isValidString("", true));
// true
console.log(isValidString("123456", true, 5));
// false
console.log(isValidString("123456"));
// true

isValidEmail(value)

  • Validates if the value has an email pattern

Example

console.log(isValidEmail('')); // false
console.log(isValidEmail('Some string')); // false
console.log(isValidEmail('jhondoe@com')); // false
console.log(isValidEmail('@gmail.com')); // false
console.log(isValidEmail('[email protected]')); // true

isValidNumber(value, allowZero, allowNegative)

  • Validates if a value of type string is a valid number
  • If allowZero equals true, '0' would return true
  • If allowNegative equals true, negative values would return true
console.log(isValidNumber('')); // false
console.log(isValidNumber('Some string')); // false
console.log(isValidNumber('123a424')); // false
console.log(isValidNumber('123a424', true)); // false
console.log(isValidNumber('123a424', true, true)); // false
console.log(isValidNumber('123a424', false, true)); // false
console.log(isValidNumber('1.23a424', false, true)); // false
console.log(isValidNumber('123-424', false, true)); // false
console.log(isValidNumber('0')); // false  
console.log(isValidNumber('0', true)); // true
console.log(isValidNumber('-1', true, true)); // true
console.log(isValidNumber('201')); // true
console.log(isValidNumber('3.14159')); // true

isValidInteger(value, allowZero, allowNegative)

  • Validates if a value of type number is an integer
  • If allowZero equals true, 0 would return true
  • If allowNegative equals true, negative values would return true
console.log(isValidInteger(3.14159)); // false
console.log(isValidInteger(0)); // false
console.log(isValidInteger(-1)); // false
console.log(isValidInteger(0, true)); // true 
console.log(isValidInteger(-1, false, true)); // true
console.log(isValidInteger(10)); // true

isValidDate(value, format)

  • Validates if a value is a valid date string
  • If the format is passed, then the value should match the pattern
console.log(isValidDate('')); // false
console.log(isValidDate('Some string')); // false
console.log(isValidDate('04939-343.643')); // false
console.log(isValidDate('2020-06-26', 'YYYY-MM-DD')); // true
console.log(isValidDate('2020-12-31', 'YYYY-MM-DD')); // true
console.log(isValidDate('Mar 25 2015', 'MMM DD YYYY')); // true
console.log(isValidDate('Tue Mar 24 2015 19:30:00', 'ddd MMM DD YYY HH:mm:ss')); // true

isValidPhoneNumber(value)

  • Validates if a value of type string is a valid phone number
  • To be a valid phone number must contain 7 digits
  • cannot contain letters or special characters between numbers, in that case return false
  • The country code is not required, but it allows you to place it
  • The allowed character is "+" at the beginning of the number to specify the country code
console.log(isValidNumber('')); // false
console.log(isValidNumber('abc')); // false
console.log(isValidNumber('0414-182-552')); // false
console.log(isValidNumber('123456')); // false
console.log(isValidNumber('123a424'); // false
console.log(isValidNumber('+584141822552')); // true
console.log(isValidNumber('584141822552')); // true
console.log(isValidNumber('04141822552')); // true
console.log(isValidNumber('4141822552')); // true