formats
Version:
formats is a collection of validators.
106 lines (66 loc) • 3.66 kB
Markdown
is a collection of validators.
$ npm install formats
First you need to add a reference to formats in your application.
```javascript
var formats = require('formats');
```
Basically, to validate a value you need to call the appropriate function on the `formats` object, e.g. `string`. The result is a validator function that you can re-use multiple times.
```javascript
var stringValidator = formats.string();
console.log(stringValidator('foobar')); // => true
```
Some validators are customizable. For that provide an `options` object when requesting the validator.
```javascript
var stringValidator = formats.string({ minLength: 7 });
console.log(stringValidator('foobar')); // => false
```
Validates that a value is an email address, according to the [W3C HTML5 specification](http://www.w3.org/TR/html5/forms.html#valid-e-mail-address).
Validates that a value is of type `number`.
Options:
- `max`: Validates that a value is at most `n`.
- `min`: Validates that a value is at least `n`.
Validates that a value is of type `string`.
Options:
- `maxLength`: Validates that a value is at most `n` characters long.
- `minLength`: Validates that a value is at least `n` characters long.
If you want to validate a value, but there is no matching built-in validator, you may use a custom validator.
A custom validator is a function that returns a validator function that returns `true` if the specified value is valid, and `false` otherwise. Once you have defined the custom validator, you can use it by providing it to the `custom` function.
```javascript
var range = function (options) {
options = options || {};
options.min = options.min || Number.MIN_VALUE;
options.max = options.max || Number.MAX_VALUE;
return function (value) {
if (typeof value !== 'number') {
return false;
}
if (value < options.min) {
return false;
}
if (value > options.max) {
return false;
}
return true;
};
};
var rangeValidator = formats.custom(range({ min: 5, max: 23 }));
console.log(rangeValidator(42)); // => false
```
This module can be built using [Grunt](http://gruntjs.com/). Besides running the tests, this also analyses the code. To run Grunt, go to the folder where you have installed formats and run `grunt`. You need to have [grunt-cli](https://github.com/gruntjs/grunt-cli) installed.
$ grunt
The MIT License (MIT)
Copyright (c) 2014 the native web.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
formats