-
Notifications
You must be signed in to change notification settings - Fork 1
Home
lewd is an intuitive and easy to use data validation library inspired by Python's voluptuous.
It validates plain data, which typically comes from a call to JSON.parse. You can use it for other sources but you need to be careful (see the following section).
The purpose of this library is to do syntactic validation, i.e. to determine whether some value looks right. If the validator finds something that smells funny, it will throw an exception and tell you what the problem was and where it occurred.
You can think of lewd as a less powerful but much more concise and simpler to use version of json-schema.
lewd is not meant to validate your data semantically, so there is no explicit support for non-local validations that require cross-referencing other parts of the input data. For simple situations like "the value of property x must be larger than the value of property y" this is not a problem. However, more complex validations such as "if property x is a string, then property y must be a number" should probably be handled by your application.
Also, only native JSON types are validated, namely strings, numbers, booleans, null, and arrays and objects are validated. If you feed lewd anything else (for example date objects or regular expressions) it may behave unexpectedly or even break (although lewd tries its best to provide error messages that are actually helpful).
Let's assume you want to make sure an object contains the properties x (which must be a number) and y (which must be an array of strings). With lewd you can simply write:
var data = { x: 42, y: ['a', 42, 'b'] };
var condition = lewd({
x: Number,
y: [String]
});
// will throw an exception because 42 is not a string
condition(data);You can also construct more complex validation structures. For instance, if you want to ensure a list only contains strings that start with "1" and booleans (in any combination), you can do this:
condition = lewd.array(lewd.some(lewd.all(String, /^1/), Boolean)),lewd has been tested on Node.JS 0.10 and later, it may or may not work on earlier versions. It can be installed via npm like any other module:
npm install --save lewd
You can then simply require the module:
var lewd = require('lewd');
var condition = lewd(Boolean);
condition(42); // fails because 42 is not a boolean valueAs of 0.6.0 lewd can be used in the browser as a UMD module which can be found in the dist folder. Please note that this feature is still experimental.
Validation is done in two steps. First, a condition is generated. Values can then be validated against that condition.
var condition = lewd.Boolean();
condition(true); // validates successfullyIn the examples you will frequently see these two steps combined:
lewd.Boolean()('not a boolean'); // validation will failFor a more complete overview please take a look at the getting started and the example pages as well as the list of built-in conditions.
You found a bug, fixed a typo or came up with a cool new feature? Feel free to open issues or send pull requests and I'll do my best to merge them. Please make sure you add tests as needed and reference the issue number in your commit (please open one if necessary). Pull requests must not break any JSHint and JSCS rules or let the test coverage drop below 100% (npm test is your friend).