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

gotta-validate

v0.2.0

Published

A POJO object validator for node, built with resources, promises, and testing in mind.

Downloads

17

Readme

Gotta Validate!

Build Status NPM Version

A POJO object validator for node, built with resources, promises, and testing in mind.

Currently being used on armory.net.au's node backend. Still a work in progress and missing many common validators. If you're using Gotta Validate! please submit pull requests to add rules you've made! Make sure to add tests else it will be rejected.

To run tests use: gulp test.

1. Examples

1.1 General stuff

Note: Take a look at src/example.spec.js for these tests.

Require it!

var GottaValidate = require('gotta-validate');

Add rules you need, you can chain it too.

GottaValidate.addRule({
    name: 'required',
    func: function (property, object) {
        var item = object[property];
        if (!item) {
            return 'is required!';
        }
    }
})
.addRule(..);

Add resources you need, you can chain it too. Add the name of any property you want to validate to the rules object. It can be a single rule (string) or many rules with an array (of strings).

GottaValidate.addResource({
    name: 'Users',
    mode: 'create',
    rules: {
        id: 'required',
        email: ['required']
    }
})
.addResource(..);

Now you gotta validate! Call the constructor function every time you want a different validator. The promise will resolve if validation was a success, and reject if any validators returned an error.

var validator = GottaValidate({
    resource: 'Users',
    mode: 'create'
});

var my_object = {};

validator
    .validate(my_object)
    .then(null, function (e) {
        expect(e).toBe([
            '[id] is required!', 
            '[email] is required!' 
        ]);
    });
    
my_object = {
    id: 'ayylmao',
    email: 'coolemailthough'
};

validator
    .validate(my_object)
    .then(function (e) {
        expect(e).not.toBeDefined();
        done();
    });

1.2 Pre-defined Rules

Rules which you can use without needing to add yourself are as follows (after using the addDefaultRules method).

GottaValidate.addDefaultRules();

GottaValidate.addResource({
    name: 'cool-resource',
    mode: 'mode',
    rules: {
        aCoolProperty: ['email', 'no-white-space', 'password', 'required']
    }
});

1.3 Extra stuff

1.3.1 Promise based rules

You can add promise based rules like the following. You can use any library that will work with q. Just make sure you return a promise! If an error occurred return an object like in the example.

var somethingAsync = require('something-async');

GottaValidate.addRule({
    name: 'required',
    func: function (property, object) {
        var defer = q.defer();
        
        if (something_bad_happened_immediately) {
            return q.reject({
                property: property,
                message: 'was bad!'
            });
        }
        
        somethingAsync.then(function (e) {
            // resolve or reject
        });
        
        return defer.promise;
    }
});

1.3.2 Rules with dependencies

You can also add rules that have dependencies. Promise based or synchronous!

GottaValidate.addRule({
    name: 'depender',
    func: function (property, object, dependencies) {
        var error = dependencies.a();
        if (error) {
            return 'oh no error!';
        }
    },
    dependencies: {
        a: function () { return true; }
    }
});

1.3.3 Rules that inherit

    GottaValidate.addRule({
        name: 'rule-a',
        func: function () {
            return 'bad';
        }
    });

    GottaValidate.addRule({
        name: 'rule-b',
        func: function () {
            return 'naughty!';
        },
        inherits: ['rule-a'] // Array for multiple, string for single
    });

    GottaValidate.addResource({
        name: 'inherit',
        mode: 'deez',
        rules: {
            id: ['rule-b']
        }
    });

2. Api

2.1 Instantiating

Add some rules and resources and then call the consturctor method. No need for new!

Options properties: resource (required), mode (required)

var validator = GottaValidate(options);
validator.validate(object);

2.2 Static methods

2.2.1 addRule(options)

Adds a rule. Returns this.

Options properties: name (required), func (required), inherits (optional)

2.2.2 addResource(options)

Add a resource. Returns this.

Options properties: name (required), mode (required), rules (optional)

2.2.3 addDefaultRules()

Adds the default rules to the rule table.

2.3 Instance methods

2.3.1 validate(object)

Validates an object based on an instantiated validator. Returns a promise.

3. License

Copyright (c) 2015 Michael Dougall

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.