-
Notifications
You must be signed in to change notification settings - Fork 1
FAQ
You can't. lewd is meant to do syntactic validation of your data, which can practically always be done synchronously. Support of asynchronous conditions is something I would like to add at some point, but there are no concrete plans as of now.
The exception that is thrown contains that information, but it is not shown when it is output to the console via console.log and the like. Instead, force a serialization so that the exception's toString method is called:
lewd({ x: Number })({ x: 'not a number' });
// outputs "ConditionViolationException: must be of type number"
try {
lewd({ x: Number })({ x: 'not a number' });
} catch (e) {
console.log(e.toString());
// outputs "Condition violation at "/x": must be of type number"
}For more details see the handling validation failures section.
Be careful when using non-primitive values for defaults. Since they are passed by reference you might get unexpected (and unwanted) side effects:
var condition = lewd({
x: lewd(Array).optional().default([])
});
var value_one = c({});
value_one.x.push(42);
// value_one is { x: [42] }
var value_two = c({});
// value_two also is { x: [42] } !As of 0.6.0, experimental browser support is available in the dist folder as a UMD module. Beware, though, that the browser builds are not yet run against our test suite, only the Node.JS source is (hence the "experimental").
No, and they probably never will be. If you have documents of that complexity you should probably take a look at json-schema.
If you are trying to do something like this:
lewd({ x: Number, y: String }, { byDefault: 'optional' });lewd will not interpret the second argument as options to the first but as two independent conditions (which is why you can write lewd(String, Number) to match either a string or a number).
What you will have to do is use the object condition explicitly:
lewd.object({ x: Number, y: String }, { byDefault: 'optional' });validator.js is a great library but it scratches a different itch. It only works with strings, which is great for URLs and query parameters but doesn't really help you validating the JSON payload of your run-of-the-mill AJAX request. However, lewd comes with condition wrappers for many of validator's validation functions so you get the best of both worlds.