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

enforcement

v1.0.0

Published

Declarative Object Validation

Downloads

16

Readme

Enforcement

Enforcement is yet another tool for validating your javascript object. It's minimalistic and made to be extensible

Installation

    npm install enforcement

Quickstart

    var validator = require('enforcement').create();

    var Person = validator.Schema({
        name: 'string',
        age: 'number'
    });

    var myPersonObject = {
        name: 'jeff',
        age: 22
    }

    Person.check(myPersonObject); // This returns undefined, but throws if validation fails;

    Person.validate(myPersonObject); //This returns a Folktale.js Either (http://docs.folktalejs.org/en/latest/api/data/either/Either.html);

Defining Schemas

To create a schema, use the enforcement.Schema() method. It accepts a schema definition and builds the validation.

The schema definition is a hash with the name of the fields as keys and the validations as values.

You can combine validations like the following:

    var mySchema = validator.Schema({
        aField: 'required string',
        another: ['required', 'number']
    });

You can use an array with the names of the constraints or combine them into a space separated string.

Nested Schemas

You can use schemas as validations for fields inside other schemas too. Consider you want to validate objects with the following format:

    {
        name: 'my name',
        age: 50,
        address:{
            street: 'some street',
            number 12
        }
    }

Here we want to validate a subdocument containing two fields, a string and a number. We can do this using a schema inside another, like this:

    var schema = validator.Schema({
        name: 'required string',
        age: 'positive integer',
        address: validator.Schema({
            street: 'required string',
            number: 'required positive integer'
        })
    })

Array Types

A field can be defined as an array. You can, then, define validations to run through an array field. For example, to validate an array of string that cannot be empty, you can write the following schema:

    var schema = validator.Schema({
        tags: '[string notEmpty]' //write the definition between square brackets to define that tags field is an array
    })

    schema.check({
        tags: ['abc', '']
    }); //this will not validate because the second string won't pass notEmpty constraint

Supported Validations

  • string
  • boolean
  • number
  • integer
  • positive
  • negative
  • notEmpty
  • required
  • array

Custom Validations

You can also create custom validations and register them into the validator:

    var greaterThanTen = {
        name: 'greaterThanTen',
        test: function(value){
            return value > 10;
        }
    }

    validator.register( greaterThanTen );

    validator.Schema({
        foo: 'integer greaterThanTen'
    })
}

It is imperative that you call register before creating any schema that uses the custom validation.