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

milkcheck

v0.1.2

Published

Shake off dirty values, this is an object checker for nodejs

Downloads

15

Readme

node-milkcheck

Presentation

Milkcheck is a tool to help you checking input data, and sanitize their values when possible.

You can install it simply by doing:

npm install milkcheck

Then use it into your project:

var milkcheck = require('milkcheck');

// Define an object schema
var schema = new milkcheck.Schema({
    firstname: milkcheck.string({
        maxLength: 255,
        mandatory: true
    }),
    lastname: milkcheck.string({
        maxLength: 255,
        mandatory: true
    }),
    role: milkcheck.string({
        reg: /^(user|admin)$/,
        mandatory: true
    }),
    email: milkcheck.email()
});

// This is our incorrect user data
var user = {
    firstname: 'Edward',
    lastname: 'Snow',
    role: 'messiah'
};

// Check user data
try { milkcheck.check(user) }
catch (e) {
    console.error(e.name); // "InvalidContent"
    console.error(e.message); // "role is invalid"
}

Checking a variable

While checking a variable, you can pass some checking options to milkcheck, such as:

  • sanitize: for extended types which support it, this can change the format on the fly of recognized format (ie: fix missing spaces, lowercase...). This highly depends on the chosen convention, and helps getting homogeneous strings with more flexible entries.
  • partial: this will tell the checker to check only given variable, and not throw any error if one is missing (even mandatory ones). This is useful if you consider editing the content partially.

(no other option so far, contribute if you need more).

Usage:

milkcheck.check(user, { sanitize: true });

Built-in types

Boolean

milkcheck.boolean() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the boolean must have

Number

milkcheck.number() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the number must have
schema.isFloat - the value must be float
schema.isInteger - the value must be integer
schema.isPositive - the value must be >= 0
schema.isNegative - the value must be <= 0
schema.isNotNull - the value must be !== 0
schema.maximum - top range value (included)
schema.minimum - bottom range value (included)

Array

milkcheck.array() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the array must have
schema.maxLength - maximum length of array
schema.minLength - minimum length of array
schema.length - length of the array

String

milkcheck.string() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have
schema.maxLength - maximum length of string
schema.minLength - minimum length of string
schema.length - length of the string
schema.re - regex to apply to the string
schema.reg - alias to schema.re
schema.regex - alias to schema.re

Extra types

More types are coming soon, for now I only needed these:

MAC address

milkcheck.mac() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: a0:b1:c2:d3:e4:f5

IPv4 address

milkcheck.ipv4() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: 192.168.0.22

IPv6 address

milkcheck.ipv6() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: FE80:0000:0000:0000:0202:B3FF:FE1E:8329

MongoDB ObjectId

milkcheck.mongoId() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: 53f4b9031cf6455b326f4c7a

email

milkcheck.email() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: [email protected]

siret

milkcheck.siret() takes a schema object:

schema.mandatory - value can't be undefined or null (complete check)
schema.value - exact value the string must have

Example: 532 685 104 00012 or 53268510400012

Extending types

This package will never be exhaustive in that topic, so we provide a method to extend built-in types: milkcheck.extend().

Example of use:

var lolcat = function(schema) {
    return milkcheck.extend(schema, {
        type: 'string',
        check: function (obj, opt) {
            if (schema.cheat) return true;

            // Count lolcat occurencies
            var l = (obj.match(/(lol|cuz|haz)/g) || []).length; 

            // Check ratio
            if (l < (obj.length / 6)) {
                return false;
            }

            // sanitize the string
            if (opt.sanitize) {
                return 'lol'; // <- this is the more sensible thing to say so far
            }
            return true;
        }
    }
});

// Use it in our schema
var schema = new milkshake.Schema({
    watIwilSay2u: lolcat({ cheat: true })
})

Play nicely with restify

This project aim to simplify the way you check input data, and this design is especially helpful if you want to check data from a webservice before writing them in a database. For example if you use restify, you can check your input data this way:

var schema = new milkcheck.Schema({
    name: milkcheck.string()
});

server.put('/suppliers', function (req, res, next) {
    schema.check(req.params);
    db.collection('suppliers').insert(supplier, function (err, result) {
        if (err) return next(restify.BadGatewayError('db error'));
        res.send(result);
        return next();
    });
});

server.on('uncaughtException', function (req, res, route, err) {
    switch (err.name) {
        case 'MissingParameter': 
            res.send(new restify.MissingParameterError(e.message));
            break;
        case 'InvalidContent':
            res.send(new restify.InvalidContentError(e.message));
            break;
        default:
            res.send(new restify.InternalError(err.message));
            break;
    }
});

Contributing

Please use github as you like, contributions are very welcome.

Licence

MIT