0.0.5 • Published 9 years ago
oke v0.0.5
Oke`
Lightweight schemas for everywhere, whenever.
Why
Oke was created to validate everything from javascript models and ajax requests to databases like CouchDB. It allows for quick simple validations as well as complex nested validation. Collections, objects and more are supported out of the box and it can be easily extended!
Features
- Chainable tests that read clearly.
- Composable tests that make sense.
- Schema validation for complex objects and arrays.
- An error message structure that is terse and friendly for developers.
Installation
Npm
npm install oke
Bower
bower install oke
Download
<script type="text/javascript" src="oke.js"></script>
<script>
define(['oke'], function (oke) {...}); // AMD
window.oke; // Otherwise we expose a global.
</script>
Example
var ok = require('oke');
var schema = ok().object({
username: ok().string().alphaNumeric().length(5, 30),
password: ok().string().optional().matches(/^[a-zA-Z0-9]{2,30}$/),
phoneNumbers: ok().array(
ok().string().phone()
)
});
var doc = {
username: 'Hello',
password: 'World',
phoneNumbers: [
'123',
'555-555-5555'
]
};
schema.assert(doc); // Throw an error unless doc matches the schema.
The above schema defines the following constraints:
- username
- A required string.
- Must contain only alpha numeric characters.
- Between 5 and 30 characters long.
- password
- An optional string.
- Must satisfy custom regex.
- contacts
- Must be an array.
- Each element must be a string and a phone number.
Click for API Methods
Error Structure
{
"name": "ValidationError",
"message": "Validation Failed",
"errors": {
// The key is the full path of the error. In this case, the first phone number.
'phoneNumbers.0':
// Array of errors for this path.
[
{
// The name of the failed validator.
type: 'phone',
// The value that was at the path (that failed).
value: '123'
}
]
}
}
Contributiors
- Use gulp to run tests and build api docs.
Please feel free to recommend more default tests and/or submit a PR!