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

jab-validator

v0.0.16

Published

validation using string and node-validator

Downloads

35

Readme

jab-validator

node-validator + string sanitize functions

Deprecated

see: https://www.npmjs.com/package/@b-flower/bdn-validator

Usage

Install

npm install jab-validator

Define schema

// user_validate.js

var validator = require('jab-validator');
var validate  = validator.validate;
var clean     = validator.clean;
var builder   = validator.builder

var user = {
  email: [validate('isEmail'), validate('notNull')]
, name: {
    last: [validate('required'), clean('capitalize')]
    // notEmpty == required
  , first: [validate('notEmpty')]
  , middle: [validate('isUppercase')]
  , abc: {
      a: [validate('isEmail')]
    }
  }
, has: [validate('contains', 'b')]
};

Build validator

// user_validate.js

var user_validator   = builder(user);
module.export = user_validator;

Validate & clean data

// user_controller.js
// ...
var user_validator = require('user_validate.js');
var o_user = {
  email: 'test@toto'
, name: {
    last: 'myname'
  , first: ''
  , middle: 'qsdf'
  }
, has: 'abc'
};

var clean_data = user_validator(o_user);
// ...

Check errors

// user_controller.js
// ...
console.log(clean_data._errors)
// => undefined if no errors

Continuation-passing style

You can use built validator with continuation-passing style (not async)

var validator = require('jab-validator');
var validate  = validator.validate;
var clean     = validator.clean;
var builder   = validator.builder;

var user = {
  email: [validate('isEmail'), validate('notNull')]
, username: [validate('required')]
};

var user_vdor = validator.builder(user);

var user_data = {
  email: '[email protected]'
, username: 'me&nobodyelse'
};

user_vdor(user_data, function(err, clean_data) {
  if (err) return handleError(err);
  // save clean_data
});

No check on empty value but required

var validator = require('jab-validator');
var validate  = validator.validate;
var clean     = validator.clean;
var builder   = validator.builder;

var schema = {
      is_int: [
        validate('isInt')
      ]
    , is_req: [validate('required'), validate('isInt')]
    }

  , validator = builder(schema)

  , object = {
      is_int: ''
    , is_req: ''
    };

var res = validator(object);

assert.equal(JSON.stringify(res._errors), '{"is_req":["String is empty","Invalid integer"]}');
delete res._errors
assert.equal(JSON.stringify(res), '{"is_int":"","is_req":""}');

Include built schema as sub schema

var validator = require('jab-validator');
var validate  = validator.validate;
var clean     = validator.clean;
var builder   = validator.builder;

var sub = {
  fd: [validate('notEmpty')]
};
var sub_cleaner = builder(sub);

var main = {
  sub_schema: sub_cleaner
, other: [validate('notEmpty')]
, last: [validate('notNull')]
};

var compare = {
  sub_schema: sub
, other: [validate('notEmpty')]
, last: [validate('notNull')]
};

compare = builder(compare);
main = builder(main);

assert.equal(JSON.stringify(main), JSON.stringify(compare));

// now validate data
var data = {
      other: 'xx'
    , last: 'last value'
    , sub_schema: {
        fd: 'df val'
      }
    };

data = main(data); // same result as compare(data);

Accept custom validator

var validator = require('jab-validator');
var validate  = validator.validate;
var clean     = validator.clean;

function myValidator(value) {
  return value > 1 ? true : false;
}

var schema = {
      is_custom: [validate('custom', myValidator)]
    };

Custom validator access full_object

function myValidator(value) {
  return Number(value) > 1 && this.another == 'Hello';
}

var schema = {
    is_custom: [validate('custom', myValidator)]
  , another: [clean('trim')]
  }
, validator = builder(schema)

, object = {
    is_custom: 2
  , another: 'Hello'
  }
, res      = validator(object)
// =>  {"is_custom":"2","another":"Hello"}

Accept custom message

 var schema = {
     my_attr: [validate({ fn: 'isInt', msg: 'Is it possible to have an integer ?' })]
   }

List of cleaners

// originals - from node-validator
trim(chars)                     //Trim optional `chars`, default is to trim whitespace (\r\n\t )
ltrim(chars)
rtrim(chars)
ifNull(replace)
toFloat()
toInt()
toBoolean()                     //True unless str = '0', 'false', or str.length == 0
toBooleanStrict()               //False unless str = '1' or 'true'
entityDecode()                  //Decode HTML entities
entityEncode()
escape()                        //Escape &, <, >, and "

// added from Stringjs
capitalize
camelize
collapseWhitespace
dasherize
ensureLeft
ensureRight
humanize
slugify
stripTags
underscore
replaceAll

// self made ;)
uppercase
lowercase

List of checkers

isEmail
isUrl
isIP
isIPv4
isIPv6
isIPNet
isAlpha
isAlphanumeric
isNumeric
isHexadecimal
isHexColor
isLowercase
isUppercase
isInt
isDecimal
isFloat
isDivisibleBy
notNull
isNull
notEmpty
equals
contains
notContains
regex
is
notRegex
not
len
isUUID
isUUIDv3
isUUIDv4
isUUIDv5
isDate
isAfter
isBefore
isIn
notIn
min
max
isCreditCard
custom