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

cb-valid

v1.0.3

Published

Validator very easy to use, and implement! (email, url, password, .etc)

Downloads

8

Readme

cb-valid

It is very easy to implement in your project. Allows the use of non-decorator based validation. Use validator.js internally to perform the validation.

Installation

npm install cb-valid --save

Note: Please use at least npm@6 when using cb-valid. From npm@6 the dependency tree is flattened, which is required by cb-valid to function properly.

Usage

// Valid for typescript
import CbValid from 'cb-valid'

// Valid for javascript vanilla
const CbValid = requrie("cb-valid").default

// We declare a test string to validate

// We clearly know that the variable testValue does not have a valid email!
// why will you show us the messages entered!


const testValue = "yonicalsin"

// Here you will add the validation you need
const exps = [

   // We will validate if testValue is an email
   {
      // The value (isEmail) is to validate an email
      name: "isEmail",
      // Message in case the email is invalid!
      message: "Email is invalid !"
   },
   {
      // The value (minLength) is to validate at least 5 characters
      name: "minLength",
      // This attribute is necessary to validate the minimum number of characters
      data: {
         min: 5
      },
      // Message in case you do not meet the required conditions
      message: "Email needs at least 4 characters!"
   },
   {
      // The value (maxLength) is to validate a maximum of 10 characters
      name: "maxLength",
      // This attribute is necessary to validate the maximum number of characters
      data: {
         max: 9
      },
      // Message in case you do not meet the required conditions
      message: "Email needs a maximum of 10 characters!"
   }
]

const res = CbValid(testValue, exps)

/**
 * @Return
 * */

/*
{
   status: false,
   messages: [
      "Email is invalid !",
      "Email needs a maximum of 10 characters!"
   ]
}
*/

Passing options

Some functions require a data parameter

export interface Data {
   min: number
   max: number
}

Validation errors

The cb-valid method returns an array of ValidationError objects. Each ValidationError is:

{
    status: Boolean; // Returns true if everything is correct, in case something is not correct it will return false
    messages?: String[]; // Contains all nested validation errors of the property
}

Manual validation

There are several method exist in the Validator that allows to perform non-decorator based validation:

import { Validator } from "cb-valid";

// Validation methods
const validator = new Validator();


   Length(value, {
      min: @number,
      max: @number
   }) // 
   minLength(value, {
      min: @number
   })
   maxLength(value, {
      max: @number
   })

   isEmail(value) // Checks if the string is an email.
   isURL(value) // Checks if the string is an url.
   isFQDN(value) // Checks if the string is a fully qualified domain name (e.g. domain.com).
   isEmpty(value) // Checks if given value is empty (=== '', === null, === undefined).
   isNotEmpty(value)  // Checks if given value is not empty (!== '', !== null, !== undefined).
   isAscii(value) // Checks if the string contains ASCII chars only.
   isBase64(value) // Checks if a string is base64 encoded.
   isCreditCard(value)  // Checks if the string is a credit card.
   isCurrency(value) // Checks if the string is a valid currency amount.
   isDecimal(value) // Checks if the string is a valid decimal value.
   isFullWidth(value) // Checks if the string contains any full-width chars.
   isHexadecimal(value) // Checks if the string is a hexadecimal number.
   isHexColor(value) // Checks if the string is a hexadecimal color.
   isISIN(value) // Checks if the string is an ISIN (stock/security identifier).
   isISO31661Alpha2(value) // Check if the string is a valid ISO 3166-1 alpha-2
   isISO31661Alpha3(value) // Check if the string is a valid ISO 3166-1 alpha-3
   isISO8601(value) // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
   isISSN(value) // Checks if the string is a ISSN.
   isJSON(value) // Checks if the string is valid JSON (note: uses JSON.parse).
   isJWT(value) // Checks if the string is valid JWT.
   isLatLong(value) // Checks if the string is a valid latitude-longitude coordinate in the format lat,long
   isLowercase(value) // Checks if the string is lowercase.
   isMACAddress(value) // Checks if the string is a MAC Address.
   isMongoId(value) // Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
   isMultibyte(value) // Checks if the string contains one or more multibyte chars.
   isPort(value) // Check if the string is a valid port number.
   isSurrogatePair(value) // Checks if the string contains any surrogate pairs chars.
   isUppercase(value) // Checks if the string is uppercase.
   isVariableWidth(value) // Checks if the string contains variable-width chars.

Backers

Stay in touch

Release notes

See information about breaking changes and release notes.

cb-valid v0.0.1

License

Nest is MIT licensed.