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 = ; // Define an object schemavar schema = firstname: milkcheck lastname: milkcheck role: milkcheck email: milkcheck; // This is our incorrect user datavar user = firstname: 'Edward' lastname: 'Snow' role: 'messiah'; // Check user datatry milkcheck catch e console; // "InvalidContent" console; // "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;
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
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: main@jokester.fr
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 { return milkcheck; // Use it in our schemavar schema = watIwilSay2u:
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 = name: milkcheck; server; server;
Contributing
Please use github as you like, contributions are very welcome.
Licence
MIT