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

easy-validation

v1.2.0

Published

TypeScript validation library

Downloads

29

Readme

Easy Validation

Build Status Known Vulnerabilities codecov

A TypeScript validation library. The lib use a subset of validator.js validators, but it can use any function that validates string and returns boolean or boolean promise.

Installation

npm install easy-validation

Usage

import * as Promise from "bluebird";
import validate, {
  V,
  ValidationError,
  ValidationObject,
  Validations,
  Validator
} from "easy-validation";

const invalidUser: ValidationObject = {
  username: "uname",
  email: "[email protected]",
  phone: "1234-567"
};

const validUser: ValidationObject = {
  username: "uname",
  email: "[email protected]",
  phone: "123456789"
};

const isUnique: Validator = email => {
  return Promise.resolve(email !== "[email protected]");
};

const validations: Validations = {
  username: [{
    validate: V.isNotBlank(),
    message: "username cannot be blank"
  }],
  email: [{
    validate: V.isNotNull(),
    message: "email cannot be null"
  }, {
    validate: isUnique,
    message: "email has been taken"
  }, {
    validate: V.isEmail(),
    message: "invalid email"
  }],
  phone: [{
    validate: V.hasLengthOf({ min: 9, max: 9 }),
    message: "phone must have nine digits"
  }, {
    validate: V.isNumeric(),
    message: "phone must contain only digits"
  }]
};

validate(invalidUser, validations)
  .catch(ValidationError, err => console.log(err.errors));

// { username: { errorMessages: [], hasError: false },
//   email:
//    { errorMessages: [ 'email has been taken' ],
//      hasError: true },
//   phone:
//    { errorMessages:
//       [ 'phone must have nine digits',
//         'phone must contain only digits' ],
//      hasError: true } }

validate(validUser, validations)
  .then(console.log);

// { username: 'uname',
//   email: '[email protected]',
//   phone: '123456789' }

The extended example that includes nested properties and array of objects can be found in tests

Validators

All validators except isNotNull treat properties as optional. That is they validate null and undefined.

contains

contains(str)

Fails if property does not contain given str.

equals

equals(str)

Fails if property is not equal to the given str.

hasLengthOf

interface LengthOption {
  min?: number;
  max?: number;
}

hasLengthOf(option)

Fails if property does not have length between min and max.

Valid options are { min: m } and { max: m } as well.

If both values are omitted string is considered valid.

isAfterDate

isAfterDate(datetime)

Fails if property is not valid date or not after datetime param.

If date param is not valid date or datetime string validation fails with ValidatorError.

isAfterNow

isAfterNow()

Fails if property is not valid date or not after current datetime.

isBeforeDate

isBeforeDate(datetime)

Fails if property is not valid date or not before datetime param.

If date param is not valid date or datetime string validation fails with ValidatorError.

isBeforeNow

isBeforeNow()

Fails if property is not valid date or not before current datetime.

isDate

isDate()

Fails if property is not parsable date string.

isDateOnly

isDateOnly()

Fails if property is not parasble date or if it contains any component but mm dd and yyyy.

isEmail

isEmail()

Fails if property is not valid email.

IsFloat

isFloat()

Fails if property is not number.

isIn

isIn(arr)

Fails if property is not memeber of the given arr.

isInt

isInt()

Fails if property is not an integer

isMax

isMax(num)

Fails if property is not number or is greater than given num.

isMin

isMin(num)

Fails if property is not number or is less than the given num.

isNotBlank

isNotBlank()

Fails if property is empty string.

isNotNull

isNotNull()

Fails if property is null or undefined.

isNotNumeric

isNotNumeric()

Fails if property is numeric.

isNumeric

isNumeric()

Fails if property is not numeric.