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

makesure

v2.0.0

Published

JavaScript object validation made easy.

Downloads

10

Readme

makesure

![Gitter](https://badges.gitter.im/Join Chat.svg)

NPM version build status

var makesure = require('makesure')

var validateUser = makesure(function(){
  this.permit('name email') // optional
  this.attrs('name email').isNot('empty').orSay("can't be empty")
})

// Validates a object, with an intrusive attribute.
validateUser({ name: '', description: 'My description', admin: true }, function(err, user){
  // err == {
  //   'name': [{'EMPTY': "can't be empty"}]
  // }
  //
  // user == {
  //   name: '',
  //   description: 'My description'
  // }
})

Features

  • Registry.
  • Async;
  • DSL;
  • Nested;
  • Focus on attributes or general;
  • Validate the entire object and return all the errors;
  • You can use your own functions for validation, or use a the set of functions like of the validator package provides.
  • tags;
  • Built-in validations;

Roadmap

  • Improve general validation to add general messages.
  • General messages based on attributes validation

Installation

npm install --save makesure

or for client-side:

bower install --save makesure

Nested validation

You can use makesure validate nested function to validate a whole object and get all the errors at once.

var makesure = require('makesure')

var validateAddress = makesure(function(){
  this.attr('street').isNot('empty')
    .orSay("can't be empty")
})

var validateUser = makesure(function(){
  this.attr('name').is('length', 3, 200)
    .orSay('minimum length is 3 and max is 200')
  this.attr('address').with(validateAddress) // nested
})

validateUser({ name: '', address: { street: '' } }, function(err, user){
  // err == {
  //   'name': [{code: "LENGTH", message: "minimum length is 3 and max is 200"}],
  //   'address.street': [{ code: "EMPTY", message: "can't be empty"}]
  // }
})

General validation

Sometimes, it's needed to validate the time of the operation or if a configuration flag is enabled. That validation is general for that object/operation.

var validateAction = makesure(function(){
  this.validate(function(cb){
    cb(null, new Date().getDay() != 7);
  }).orSay("The operation can't be performed on Sunday.")
  .tag("sunday_restriction"); // if not set the tag default 'invalid' is used.
})

validateAction({}, function(err){
  // err = {
  //   'base': [ {code: 'SUNDAY_RESTRICTION', message: "The operation can't be performed on Sunday."} ]
  // }
})

Built-in validations

This project is using the validator package as the built-in validations functions.

var validator = require('validator');

makesure.registerSync('equals', validator.equals);
makesure.registerSync('contains', validator.contains);
makesure.registerSync('matches', validator.matches);
makesure.registerSync('email', validator.isEmail);
makesure.registerSync('url', validator.isURL);
makesure.registerSync('fqdn', validator.isFQDN);
makesure.registerSync('ip', validator.isIP);
makesure.registerSync('alpha', validator.isAlpha);
makesure.registerSync('numeric', validator.isNumeric);
makesure.registerSync('alphanumeric', validator.isAlphanumeric);
makesure.registerSync('base64', validator.isBase64);
makesure.registerSync('hexadecimal', validator.isHexadecimal);
makesure.registerSync('hex_color', validator.isHexColor);
makesure.registerSync('lowercase', validator.isLowercase);
makesure.registerSync('uppercase', validator.isUppercase);
makesure.registerSync('int', validator.isInt);
makesure.registerSync('float', validator.isFloat);
makesure.registerSync('divisible_by', validator.isDivisibleBy);
makesure.registerSync('null', validator.isNull);
makesure.registerSync('empty', validator.isNull);
makesure.registerSync('length', validator.isLength);
makesure.registerSync('byte_length', validator.isByteLength);
makesure.registerSync('uuid', validator.isUUID);
makesure.registerSync('date', validator.isDate);
makesure.registerSync('after', validator.isAfter);
makesure.registerSync('before', validator.isBefore);
makesure.registerSync('in', validator.isIn);
makesure.registerSync('credit_card', validator.isCreditCard);
makesure.registerSync('isbn', validator.isISBN);
makesure.registerSync('json', validator.isJSON);
makesure.registerSync('multibyte', validator.isMultibyte);
makesure.registerSync('ascii', validator.isAscii);
makesure.registerSync('full_width', validator.isFullWidth);
makesure.registerSync('half_width', validator.isHalfWidth);
makesure.registerSync('variable_width', validator.isVariableWidth);
makesure.registerSync('surrogate_pair', validator.isSurrogatePair);
makesure.registerSync('mongo_id', validator.isMongoId);

License

MIT