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

rules

v0.6.53

Published

Validate objects against predefined rules (node.js).

Downloads

1,691

Readme

rules (node.js)

Build Status

A tiny rules framework that can be used to validate any value, either by you creating a rules/schema object or applying validations to a single value. In both cases a fluent interface is used.

Samples

Schema

You create an object to declare the rules/invariants you want to apply (something akin to a schema). A fluent interface makes it easy to specify the invariants for each property:

var nameRules = {
    first  : mustBe().populated().string({ minLength: 5, maxLength: 20}), [1]
    second : mustBe().populated().string({ minLength: 5, maxLength: 20}),
}

var personRules = {
    name:        nameRules,
    weight:      mustBe().populated().numeric({min : 0, max: 130}),
    dateOfBirth: function() { 
        this.populated().date({ before: now.subtract("years", 1) }); 
    } [2]
}

As shown you can access this fluent interface using two approaches:

  • [1] mustBe() - Acts as the entry point to the fluent interface.
  • [2] function - 'this' inside the function being the entry point to the fluent interface.

The function based approach is designed primarily for use with CoffeeScript:

# This schema is not showing how to validate a real address, it just shows a few validators
addressRules = {
  streetOne: mustBe().populated()
  streetTwo: -> @.populated().string( minLength: 10, maxLength : 50 ) 
  streetThree: -> @.populated().string( minLength : 10, maxLength: 50) 
  town: -> @.populated()
  postCode: -> @.populated().matchFor(/.../)
}

Inline Validation

The same validators are available for use validating individual values:

var doSomeStuff = function(name, age) {
    ensure(name).populated().string();
    ensure(age, "age").integer();

    ...
}

Triggering validation

You trigger validation using:

result = rules.apply(person, personRules)

The returned object has the per-property details of any validation failures, e.g.:

{ 
    name: { 
        first: { 
            message: 'The value must be populated.',
            type: 'not_populated',
            value: '' 
        },
        second: { 
            message: 'The value must be populated.',
            type: 'not_populated',
            value: undefined } 
        },
    weight: { 
        message: 'The value must be populated.',
        type: 'not_populated',
        value: undefined 
    } 
}

Note in this case both the first name (e.g. person.name.first) and second name (person.name.second) needed to be populated, along with the weight.

Validators

The framework comes with several validators, to understand them further you may want to run the examples.

  • populated - Checks the value is not null, undefined, "", or an empty array.
  • array
  • numeric - Optionally you can also pass in object with min and/or max values
  • integer
  • matchFor - You can pass in an object with pattern and optionally flags, alternatively you can pass in the RegExp object to use.
  • date - Optionally you can specify that the date must be before and/or after specified dates. To make this easier you use now.add or now.subtract to specify the dates to use for before/after.
  • string - Optionally you can pass in minLength and/or maxLength.

Examples

The project comes with examples in the examples directory.

Tests

mocha -R spec spec/testFixture spec/ --recursive

Future

  • Numeric validators - >, <, >=, <=
  • Date validator - Support now()
  • boolean validator
  • enum style validator - valueIn(list), valueNotIn(list).
  • Potentially UMD support
  • Trying to apply multiple of same validator, multiple type validators (integer and string, numeric and boolean), regex with anything other than string
  • Ensure interface e.g. ensure(5).populate().numeric(), allowing direct validation of single values
  • API for throwing
  • Cyclical rules objects warning
  • Numeric validator - failing if passed "15.5"