js-validate
Advanced tools
Comparing version 0.4.0 to 0.4.1
{ | ||
"name": "js-validate", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"homepage": "https://github.com/kshunz/js-validate", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "js-validate", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Functional, extensible, input validation. Make sure the value you receive is EXACTLY what you expect.", | ||
@@ -5,0 +5,0 @@ "main": "src/validate.js", |
@@ -1,32 +0,76 @@ | ||
# js-validate # | ||
<h2>js-validate</h2> | ||
<i>Functional, extensible, input validation. Make sure the value you receive is EXACTLY what you expect.</i> | ||
### Functional, extensible, input validation. Make sure the value you receive is EXACTLY what you expect. ### | ||
<h1>Getting Started</h1> | ||
Getting Started | ||
<h4>Installation</h4> | ||
``` | ||
npm install js-validate | ||
``` | ||
npm install js-validate | ||
<h4>Quick Use</h4> | ||
```node | ||
var validator = require('js-validate'); | ||
``` | ||
``` | ||
var validator = require('js-validate'); | ||
var validate = validator.start(); | ||
validate(123, 'min-length 2'); | ||
``` | ||
<h4>Validators</h4> | ||
- Alpha | ||
- Capital | ||
- EndsWith | ||
- Min-Length | ||
- Number (isNumber) | ||
- Numbers (Number Characters) | ||
- Specials (Special Characters) | ||
- StartsWith | ||
<h5>Validate against a single rule:</h5> | ||
validate('123', 'min-length 2'); //--> true | ||
<h5>Validate against multiple rules:</h5> | ||
validate('123', ['min-length 2', 'number']); //-->true | ||
<h4>Built-in Rules</h4> | ||
- alpha | ||
- alphanumeric | ||
- boolean **NEW** | ||
- capitals (counts capital characters) | ||
- ends-with | ||
- equals | ||
- length | ||
- max | ||
- max-length | ||
- min | ||
- min-length | ||
- number (isNumber) ***Altered Rule*** | ||
- numbers (Number Characters) | ||
- numeric (consists of numerical digits) **NEW** | ||
- specials (Special Characters) | ||
- starts-with | ||
<h5>Create a validator (rule) group:</h5> | ||
validator.group({ | ||
'account-number': [ | ||
'alphanumeric', | ||
'min-length 7', | ||
'starts-with 000-', | ||
'ends-with -00' | ||
] | ||
}); | ||
<h5>Validate against a group of rules:</h5> | ||
validate('000-KLJ8989123-00', 'account-number'); //--> true | ||
<h5>Create a custom rule:</h5> | ||
validator.rules({ | ||
isOkay: function(input) { | ||
return input === 'ok'; | ||
} | ||
}); | ||
<h5>Keep this in mind when creating custom rules:</h5> | ||
- Validator rules must return a pure boolean (true | false) | ||
- The first parameter must be the user input | ||
- Unlimited additional parameters are supported | ||
- Custom rules are added to the default rule list | ||
- Custom rules may be used in conjunction with defaults in rule groups |
@@ -17,2 +17,11 @@ module.exports = function () { | ||
}, | ||
'boolean': function(input) { | ||
var pureBoolean = typeof input === 'boolean'; | ||
if(!pureBoolean) { | ||
input = String(input).toLowerCase(); | ||
} | ||
return pureBoolean || input === 'true' || input === 'false'; | ||
}, | ||
'capitals': function (input, count) { | ||
@@ -25,3 +34,3 @@ count = Number(count); | ||
'endsWith': function(input, char) { | ||
return _.endsWith(input, char); | ||
return _.endsWith(input, char); | ||
}, | ||
@@ -50,10 +59,13 @@ 'equals': function (input, value) { | ||
extraChars.forEach(function(char) { | ||
if(String(char).toLowerCase() === 'space') { | ||
char = ' '; | ||
} | ||
if(String(char).toLowerCase() === 'space') { | ||
char = ' '; | ||
} | ||
input = input.split(char).join(''); | ||
input = input.split(char).join(''); | ||
}); | ||
return String(input) === String(Number(input)); | ||
var decimals = String(input).split('.')[1]; | ||
var numDecimals = decimals ? decimals.length : 0; | ||
return String(input) === String(Number(input).toFixed(numDecimals)); | ||
}, | ||
@@ -66,2 +78,11 @@ 'numbers': function (input, count) { | ||
}, | ||
'numeric': function(input) { | ||
var inputAsString = String(input); | ||
var numbers = inputAsString.split(''); | ||
return !numbers.some(function(num) { | ||
num = num === '.' ? 0 : Number(num); | ||
return num * 1 !== Number(num); | ||
}); | ||
}, | ||
'specials': function (input, count) { | ||
@@ -78,2 +99,2 @@ count = Number(count); | ||
}(); | ||
}(); |
describe('Number rule validator', function() { | ||
var number = RULES.number; | ||
it('should accept whole numbers', function() {}); | ||
it('should accept integers', function() {}); | ||
it('should accept whole numbers', function() { | ||
expect(number(123)).to.be.true; | ||
}); | ||
it('should accept integers', function() { | ||
expect(number('123.0')).to.be.true; | ||
expect(number('123.023')).to.be.true; | ||
expect(number('123.023.55')).to.be.false; | ||
}); | ||
it('should reject numbers that begin with zero', function() { | ||
expect(number('0123')).to.be.false; | ||
}); | ||
}); | ||
}); |
21009
24
443
77