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

order-matter-validator.js

v0.2.0

Published

An object validator for Javascript ES6, validate your object by defining order-matter, powerful and extensible validation schema.

Downloads

20

Readme

order-matter-validator

An object validator for Javascript ES6, validate your object by defining order-matter and extensible validation schema.

Usage

const validate = require('order-matter-validator.js');

// extensible with custom rules
validate.use('unique-user', require('../utils/is-unique-username'));

// order-matter schema
const schema = [
  ['user', 'type', 'string', 'username must be a string'],
  ['user', 'minlen', 6, 'username must be at least 6 characters'],
  ['user', 'unique-user', 'username must be unique'] // using the custom rule
];

validate(object, schema, err => {
  if (err) {
    console.log(err.path); // field path
    console.log(err.value); // field value
    console.log(err.rule.name); // rule name
    console.log(err.rule.opts); // rule options
    console.log(err.rule.desc); // rule description
  } else {
    // err is null when no validation error
  }
});

Order-matter schema

Why ? Let's consider a non-order-matter schema example below:

const schema = {
  user: {
    type: 'string',
    minlen: 6,
    uniqueUser: true
  },
  pass: {
    type: 'string',
    minlen: 6
  }
}

So how the underground code know the order of validated fields (user and pass here) or the order of rules (type, minlen and uniqueUser here) ?

In other words, you want to validate user first, then pass; the rule type first before minlen, but how to present those things to the underground code ?

And remember, we can't rely on the order of object definition, just because the specification of JS doesn't specify that (read more: http://stackoverflow.com/a/5525812).

Extensibility

This module only includes some basic built-in rules, they never be enough for our need.

To use a custom rule, for example, a rule that ensures an username is unique:

is-unique-username.js:

/**
 * A rule impl should accept 3 parameters.
 * The first, the validated value.
 * The second, additional options.
 * The third, the callback.
 *
 * `cb(true)`` meaning the value is valid.
 * `cb(false)` meaning the value is invalid.
 *
 * That's it!
 */
module.exports = function (username, opts, cb) {
  User.findOne({username}, (err, user) => {
    if (err) throw err;
    cb(!user);
  })
}

signup.js:

const validate = require('order-matter-validator.js');
validate.use('unique-user', require('../utils/is-unique-username'));

const schema = [
  ['user', 'unique-user', 'username must be unique']
];

validate({username: 'thenewvu'}, schema, (err) => {
  if (err) {
    // do something
  } else {
    // do something
  }
})

One-job rules

Before talking about one-job rules, lets talk about more-than-one-job rules, for example, validating when users are updating their profile, clients sends only changed fields to the server and the server need to validate them:

const schema = [
  ['phone', 'pattern', config.regex.phone, 'invalid phone number'],
  ['email', 'pattern', config.regex.email, 'invalid email']
];

validate(profile, schema, (err) => {
  // do something on err
})

The problem here is, only changed fields are sent to the server, therefore they're all optional and can be undefined, this makes the rule pattern can not validate undefined as an invalid value.

To solve this problem in a flexible way, all rules should have only one job. All rules should only validate the value if it matches some pre-condition, if it's not, just act as it's valid and let other rules does their job.

For example, these rules, len, minlen and maxlen only validates the value if it's an actual number; pattern only validates the value if it's a string, so on. If you want to validate the type of the field value, let the rule type does its job.

Rules

defined

Ensure a field is defined. It's defined only when it's not undefined. Even null is defined.

This rule has not opts, so just ignore it in the schema.

const schema = [
  ['path.to.field', 'defined', 'description']
];

len

Ensure the length of an array or a string equal to a given length.

If the value is not an array or a string, there's no validation error. If the field is mandatory, you should combine this rule with type.

const schema = [
  ['path.to.field', 'type', 'array', 'description'],
  ['path.to.field', 'len', 3, 'description']
]

minlen

Ensure the length of an array or a string respect a given minimum length.

If the value is not an array or a string, there's no validation error. If the field is mandatory, you should combine this rule with type.

const schema = [
  ['path.to.field', 'type', 'string', 'description'],
  ['path.to.field', 'minlen', 6, 'description']
]

maxlen

Ensure the length of an array or a string respect a given maximum length.

If the value is not an array or a string, there's no validation error. If the field is mandatory, you should combine this rule with type.

const schema = [
  ['path.to.field', 'type', 'string', 'description'],
  ['path.to.field', 'maxlen', 30, 'description']
]

min

Ensure a number respect a given minimum value.

If the value is not a number, there's no validation error. If the field is mandatory, you should combine this rule with 'type'.

const schema = [
  ['path.to.field', 'type', 'number', 'description'],
  ['path.to.field', 'min', 13, 'description']
]

max

Ensure a number respect a given maximum value.

If the value is not a number, there's no validation error. If the field is mandatory, you should combine this rule with 'type'.

const schema = [
  ['path.to.field', 'type', 'number', 'description'],
  ['path.to.field', 'max', 100, 'description']
]

pattern

Ensure a string respect a given pattern.

If the value is not a string, there's no validation error. If the field is mandatory, you should combine this rule with 'type'.

The pattern can be a literal regexp (/^\w+$/) or a RegExp instance (new RegExp('^\\w+$')).

const schema = [
  ['path.to.field', 'type', 'string', 'description'],
  ['path.to.field', 'pattern', /^\w+$/, 'description']
]

truthy

Ensure a value is truthy.

A value is truthy if it's not one of below values:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)
  • [] (empty array)

This rule has no opts, just ignore it in the schema.

const schema = [
  ['path.to.field', 'truthy', 'description'],
]

type

Ensure a value has a type.

The type of the value will be evaluated by typeof operator.

Below are supported types:

  • number
  • string
  • function
  • symbol
  • undefined
  • object
const schema = [
  ['path.to.field', 'type', 'number', 'description']
]

in

Ensure a value is in an array.

If the value is undefined, there is no validation error. If the field is mandatory, you should combine this rule with the rule type.

const schema = [
  ['path.to.field', 'in', [1, 2, 3], 'description']
]

nin

Ensure a value is not in an array.

If the value is undefined, there is no validation error. If the field is mandatory, you should combine this rule with the rule type.

const schema = [
  ['path.to.field', 'nin', [1, 2, 3], 'description']
]

Todos

  • [x] Add in rule
  • [x] Throw if rule opts is invalid
  • [x] Add nin rule