occamsrazor-validator
Advanced tools
Comparing version 5.1.0 to 6.0.0
@@ -82,3 +82,3 @@ var combineValidators = require('./lib/combine-validators'); | ||
var k; | ||
funcs = funcs || [isAnything]; | ||
funcs = funcs || []; | ||
var v = function validator(obj){ | ||
@@ -85,0 +85,0 @@ var i, score, total = 0; |
{ | ||
"name": "occamsrazor-validator", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"description": "A duck-typing library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,6 +30,6 @@ occamsrazor-validator | ||
"validator()" creates the simplest validator. It matches everything with score 1: | ||
"validator()" creates the simplest validator. It matches everything with score 0: | ||
```js | ||
isAnything('hello').value(); // 1 | ||
isAnything({width: 10}).value(); // 1 | ||
isAnything('hello').value(); // 0 | ||
isAnything({width: 10}).value(); // 0 | ||
``` | ||
@@ -42,5 +42,5 @@ You can chain a function to create a more strict validation: | ||
``` | ||
So for example, the score of this new validator will be 2: | ||
So for example, the score of this new validator will be 1: | ||
```js | ||
hasWheels({wheels: 4}).value(); // 2 | ||
hasWheels({wheels: 4}).value(); // 1 | ||
hasWheels({feet: 2}); // returns null | ||
@@ -56,5 +56,5 @@ ``` | ||
```js | ||
isAnything.score() // 1 | ||
hasWheels.score() // 2 | ||
hasWheelsAndWings.score() // 3 | ||
isAnything.score() // 0 | ||
hasWheels.score() // 1 | ||
hasWheelsAndWings.score() // 2 | ||
``` | ||
@@ -148,5 +148,5 @@ In order to write validators you can use duck typing, type checking or whatever check you want to use: | ||
```js | ||
v(1, 5, 8).value(); // it will return [2, 3, 3] | ||
v(1, 5, 8).value(); // it will return [1, 2, 2] | ||
``` | ||
If all values match, the validator will return a validationResult with value [2, 3, 3]. | ||
If all values match, the validator will return a validationResult with value [1, 2, 2]. | ||
The elements of the array are the values of the respective validators. | ||
@@ -185,3 +185,3 @@ If one of them doesn't match the result will be null: | ||
Returns a generic validator. It will validate every object with score 1. | ||
Returns a generic validator. It will validate every object with score 0. | ||
@@ -188,0 +188,0 @@ validator().score |
@@ -15,3 +15,3 @@ var assert = require('chai').assert; | ||
it('must succeed', function () { | ||
assert.deepEqual(v(1, 5, [3, 4]).value(), [1, 2, 3]); | ||
assert.deepEqual(v(1, 5, [3, 4]).value(), [0, 1, 2]); | ||
}); | ||
@@ -24,3 +24,3 @@ | ||
it('must not fail if too long', function () { | ||
assert.deepEqual(v(1, 5, [3, 4], 33).value(), [1, 2, 3]); | ||
assert.deepEqual(v(1, 5, [3, 4], 33).value(), [0, 1, 2]); | ||
}); | ||
@@ -27,0 +27,0 @@ |
@@ -15,4 +15,4 @@ var assert = require('chai').assert; | ||
it('must validate custom validator', function () { | ||
var is5 = validator().is5() | ||
assert.equal(is5(5).value(), 2); | ||
var is5 = validator().is5(); | ||
assert.equal(is5(5).value(), 1); | ||
assert.isNull(is5(6)); | ||
@@ -28,3 +28,3 @@ }); | ||
this.l = l; | ||
} | ||
}; | ||
square = new Square(4); | ||
@@ -34,9 +34,9 @@ }); | ||
it('must validate using isPrototypeOf', function () { | ||
var isSquareProto = validator().isPrototypeOf(Square.prototype) | ||
assert.equal(isSquareProto(square).value(), 2); | ||
var isSquareProto = validator().isPrototypeOf(Square.prototype); | ||
assert.equal(isSquareProto(square).value(), 1); | ||
}); | ||
it('must validate using isInstanceOf', function () { | ||
var isSquareInstance = validator().isInstanceOf(Square) | ||
assert.equal(isSquareInstance(square).value(), 2); | ||
var isSquareInstance = validator().isInstanceOf(Square); | ||
assert.equal(isSquareInstance(square).value(), 1); | ||
}); | ||
@@ -54,7 +54,7 @@ }); | ||
it('must set score correctly', function () { | ||
assert.equal(hasWidthAndHeight.score(), 2); | ||
assert.equal(hasWidthAndHeight.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(hasWidthAndHeight({width: 8, height: 10}).value(), 2); | ||
assert.equal(hasWidthAndHeight({width: 8, height: 10}).value(), 1); | ||
}); | ||
@@ -61,0 +61,0 @@ |
@@ -38,3 +38,3 @@ var assert = require('chai').assert; | ||
var a = items.map(function (v) {return v.toString();}); | ||
assert.deepEqual(a, ['B', 'F', 'I', 'J']) | ||
assert.deepEqual(a, ['B', 'F', 'I', 'J']); | ||
}); | ||
@@ -76,3 +76,3 @@ }); | ||
var a = items.map(function (v) {return v.toString();}); | ||
assert.deepEqual(a, ['BCD', 'H', 'IB', 'IC']) | ||
assert.deepEqual(a, ['BCD', 'H', 'IB', 'IC']); | ||
}); | ||
@@ -79,0 +79,0 @@ |
@@ -12,7 +12,7 @@ var assert = require('chai').assert; | ||
it('must be a function', function () { | ||
assert.equal(typeof is_hello, "function"); | ||
assert.equal(typeof is_hello, 'function'); | ||
}); | ||
it('must have the right name', function () { | ||
assert.equal(is_hello.name, "validator"); | ||
assert.equal(is_hello.name, 'validator'); | ||
}); | ||
@@ -19,0 +19,0 @@ |
@@ -8,4 +8,4 @@ var assert = require('chai').assert; | ||
var is_anything = validator().match(undefined); | ||
assert.equal(is_anything('hello').value(), 2); | ||
assert.equal(is_anything('nothello').value(), 2); | ||
assert.equal(is_anything('hello').value(), 1); | ||
assert.equal(is_anything('nothello').value(), 1); | ||
}); | ||
@@ -15,3 +15,3 @@ | ||
var is_hello = validator().match('hello'); | ||
assert.equal(is_hello('hello').value(), 2); | ||
assert.equal(is_hello('hello').value(), 1); | ||
assert.isNull(is_hello('nothello')); | ||
@@ -22,3 +22,3 @@ }); | ||
var is_hello = validator().match(/hello/); | ||
assert.equal(is_hello('hello world').value(), 2); | ||
assert.equal(is_hello('hello world').value(), 1); | ||
assert.isNull(is_hello('good morning world')); | ||
@@ -37,10 +37,10 @@ }); | ||
it('must set score correctly', function () { | ||
assert.equal(isAnything.score(), 1); | ||
assert.equal(hasWidth.score(), 2); | ||
assert.equal(hasHeight_hasWidth.score(), 2); | ||
assert.equal(isAnything.score(), 0); | ||
assert.equal(hasWidth.score(), 1); | ||
assert.equal(hasHeight_hasWidth.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(hasWidth({width: 1, height: 2}).value(), 2); | ||
assert.equal(hasHeight_hasWidth({width: 1, height: 2}).value(), 2); | ||
assert.equal(hasWidth({width: 1, height: 2}).value(), 1); | ||
assert.equal(hasHeight_hasWidth({width: 1, height: 2}).value(), 1); | ||
}); | ||
@@ -57,3 +57,3 @@ }); | ||
center: { | ||
x: "10", y: undefined | ||
x: '10', y: undefined | ||
} | ||
@@ -64,16 +64,16 @@ }); | ||
it('must set score correctly', function () { | ||
assert.equal(hasWidth10.score(), 2); | ||
assert.equal(hasX10.score(), 2); | ||
assert.equal(hasWidth10.score(), 1); | ||
assert.equal(hasX10.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(hasWidth10({width: "10"}).value(), 2); | ||
assert.equal(hasX10({center: {x:"10", y:"1"}}).value(), 2); | ||
assert.equal(hasWidth10({width: '10'}).value(), 1); | ||
assert.equal(hasX10({center: {x:'10', y:'1'}}).value(), 1); | ||
}); | ||
it('must not match', function () { | ||
assert.isNull(hasX10({center: {x:"11", y:"1"}})); | ||
assert.isNull(hasX10({center: {x:'11', y:'1'}})); | ||
assert.isNull(hasWidth10({width: 1})); | ||
assert.isNull(hasX10({center: {x:"10"}})); | ||
assert.isNull(hasX10({center: "1"})); | ||
assert.isNull(hasX10({center: {x:'10'}})); | ||
assert.isNull(hasX10({center: '1'})); | ||
}); | ||
@@ -88,3 +88,3 @@ | ||
isAnything = validator(); | ||
hasWidthbetween5and10 = isAnything.match({width: function (w){ | ||
hasWidthbetween5and10 = isAnything.match({width: function (w) { | ||
return w >= 5 && w <=10; | ||
@@ -97,10 +97,10 @@ }}); | ||
it('must set score correctly', function () { | ||
assert.equal(hasWidthbetween5and10.score(), 2); | ||
assert.equal(hasWidthbetween5and10.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(hasWidthbetween5and10({width: 8}).value(), 2); | ||
assert.equal(hasWidthbetween5and10({width: 8}).value(), 1); | ||
assert.equal(isNotANumber(NaN).value(), 2); | ||
assert.equal(isArray([1, 2, 3]).value(), 2); | ||
assert.equal(isNotANumber(NaN).value(), 1); | ||
assert.equal(isArray([1, 2, 3]).value(), 1); | ||
}); | ||
@@ -129,7 +129,7 @@ | ||
it('must set score correctly', function () { | ||
assert.equal(hasWidthAndHeight.score(), 2); | ||
assert.equal(hasWidthAndHeight.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(hasWidthAndHeight(['width', 'height']).value(), 2); | ||
assert.equal(hasWidthAndHeight(['width', 'height']).value(), 1); | ||
}); | ||
@@ -141,9 +141,9 @@ | ||
it('must match only second item', function (){ | ||
assert.equal(hasWidthAsSecondField(['xxx', 'width']).value(), 2); | ||
it('must match only second item', function () { | ||
assert.equal(hasWidthAsSecondField(['xxx', 'width']).value(), 1); | ||
assert.isNull(hasWidthAsSecondField(['width'])); | ||
}); | ||
it('must match only second item with function', function (){ | ||
assert.equal(hasSecondNumberOdd(['xxx', 3]).value(), 2); | ||
it('must match only second item with function', function () { | ||
assert.equal(hasSecondNumberOdd(['xxx', 3]).value(), 1); | ||
assert.isNull(hasSecondNumberOdd(['xxx', 2])); | ||
@@ -163,7 +163,7 @@ }); | ||
it('must set score correctly', function () { | ||
assert.equal(isNull.score(), 2); | ||
assert.equal(isNull.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(isNull(null).value(), 2); | ||
assert.equal(isNull(null).value(), 1); | ||
}); | ||
@@ -186,9 +186,9 @@ | ||
it('must set score correctly', function () { | ||
assert.equal(isTrue.score(), 2); | ||
assert.equal(isFalse.score(), 2); | ||
assert.equal(isTrue.score(), 1); | ||
assert.equal(isFalse.score(), 1); | ||
}); | ||
it('must match', function () { | ||
assert.equal(isTrue(true).value(), 2); | ||
assert.equal(isFalse(false).value(), 2); | ||
assert.equal(isTrue(true).value(), 1); | ||
assert.equal(isFalse(false).value(), 1); | ||
}); | ||
@@ -195,0 +195,0 @@ |
@@ -12,11 +12,11 @@ var assert = require('chai').assert; | ||
is_instrument = validator().chain(function (obj){ | ||
is_instrument = validator().chain(function (obj) { | ||
return 'instrument_name' in obj; | ||
}); | ||
is_guitar = is_instrument.chain( function (obj){ | ||
is_guitar = is_instrument.chain( function (obj) { | ||
return 'nStrings' in obj; | ||
}); | ||
is_electricguitar = is_guitar.chain(function (obj){ | ||
is_electricguitar = is_guitar.chain(function (obj) { | ||
return 'ampli' in obj; | ||
@@ -45,20 +45,20 @@ }); | ||
it('must return right score', function () { | ||
assert.equal(is_anything.score(), 1); | ||
assert.equal(is_instrument.score(), 2); | ||
assert.equal(is_guitar.score(), 3); | ||
assert.equal(is_electricguitar.score(), 4); | ||
assert.equal(is_anything.score(), 0); | ||
assert.equal(is_instrument.score(), 1); | ||
assert.equal(is_guitar.score(), 2); | ||
assert.equal(is_electricguitar.score(), 3); | ||
}); | ||
it('must pass generic validation', function () { | ||
assert.equal(is_anything(instrument).value(), 1); | ||
assert.equal(is_anything(drum).value(), 1); | ||
assert.equal(is_anything(guitar).value(), 1); | ||
assert.equal(is_anything(electricguitar).value(), 1); | ||
assert.equal(is_anything(instrument).value(), 0); | ||
assert.equal(is_anything(drum).value(), 0); | ||
assert.equal(is_anything(guitar).value(), 0); | ||
assert.equal(is_anything(electricguitar).value(), 0); | ||
}); | ||
it('must sometimes pass validation (1 check)', function () { | ||
assert.equal(is_instrument(instrument).value(), 2); | ||
assert.equal(is_instrument(drum).value(), 2); | ||
assert.equal(is_instrument(guitar).value(), 2); | ||
assert.equal(is_instrument(electricguitar).value(), 2); | ||
assert.equal(is_instrument(instrument).value(), 1); | ||
assert.equal(is_instrument(drum).value(), 1); | ||
assert.equal(is_instrument(guitar).value(), 1); | ||
assert.equal(is_instrument(electricguitar).value(), 1); | ||
}); | ||
@@ -69,4 +69,4 @@ | ||
assert.isNull(is_guitar(drum)); | ||
assert.equal(is_guitar(guitar).value(), 3); | ||
assert.equal(is_guitar(electricguitar).value(), 3); | ||
assert.equal(is_guitar(guitar).value(), 2); | ||
assert.equal(is_guitar(electricguitar).value(), 2); | ||
}); | ||
@@ -78,3 +78,3 @@ | ||
assert.isNull(is_electricguitar(guitar)); | ||
assert.equal(is_electricguitar(electricguitar).value(), 4); | ||
assert.equal(is_electricguitar(electricguitar).value(), 3); | ||
}); | ||
@@ -90,7 +90,7 @@ | ||
it('must be raised by important', function () { | ||
assert.equal(is_guitar_important.score(), 67); | ||
assert.equal(is_guitar_important.score(), 66); | ||
}); | ||
it('must not match using important', function () { | ||
assert.equal(is_guitar_important(guitar).value(), 67); | ||
assert.equal(is_guitar_important(guitar).value(), 66); | ||
}); | ||
@@ -97,0 +97,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29899