schema-inspector
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -126,2 +126,5 @@ /* | ||
}, | ||
"NaN": function (element) { | ||
return typeof element === 'number' && isNaN(element); | ||
}, | ||
"boolean": function (element) { | ||
@@ -148,2 +151,5 @@ return typeof element === 'boolean'; | ||
function _simpleType(type, candidate) { | ||
if (typeof type == 'function') { | ||
return candidate instanceof type; | ||
} | ||
type = type in _typeIs ? type : 'any'; | ||
@@ -156,3 +162,4 @@ return _typeIs[type](candidate); | ||
if (_simpleType(i, candidate)) { | ||
return i !== 'any' ? i : (typeof candidate && isNaN(candidate) ? 'NaN': typeof candidate); | ||
if (i !== 'any') { return i; } | ||
return 'an instance of ' + candidate.constructor.name; | ||
} | ||
@@ -209,3 +216,3 @@ } | ||
if (typeof candidate === 'undefined' | ||
|| (typeof schema.type !== 'string' && !(schema.type instanceof Array))) { | ||
|| (typeof schema.type !== 'string' && !(schema.type instanceof Array) && typeof schema.type !== 'function')) { | ||
return; | ||
@@ -218,2 +225,3 @@ } | ||
if (!typeIsValid) { | ||
types = types.map(function (t) {return typeof t === 'function' ? 'and instance of ' + t.name : t; }); | ||
this.report('must be ' + types.join(' or ') + ', but is ' + _realType(candidate)); | ||
@@ -428,3 +436,3 @@ } | ||
} | ||
if (!(schema.properties instanceof Object) || !_typeIs.object(candidate)) { | ||
if (!(schema.properties instanceof Object) || !(candidate instanceof Object)) { | ||
return; | ||
@@ -1537,2 +1545,2 @@ } | ||
}; | ||
})(); | ||
})(); |
{ | ||
"name": "schema-inspector", | ||
"description": "Schema-Inspector is a powerful tool to sanitize and validate JS objects.", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"main": "./lib/schema-inspector.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -189,2 +189,3 @@ [![schema-inspector logo](https://raw2.github.com/Atinux/schema-inspector/master/misc/schema-inspector.png)](http://atinux.github.io/schema-inspector/) | ||
* `array` (constructor === Array) | ||
* A function (candidate isinstance) | ||
* `any` (it can be anything) | ||
@@ -200,2 +201,4 @@ | ||
function Class() {} | ||
var schema = { | ||
@@ -206,3 +209,4 @@ type: 'object', | ||
ipsum: { type: 'any' }, | ||
dolor: { type: ['number' 'string', 'null'] } | ||
dolor: { type: ['number' 'string', 'null'] }, | ||
sit: { type: Class } | ||
} | ||
@@ -214,3 +218,4 @@ }; | ||
ipsum: 'sit amet', | ||
dolor: 23 | ||
dolor: 23, | ||
sit: new Class() | ||
}; | ||
@@ -221,2 +226,3 @@ var c2 = { | ||
dolor: 'sit amet' | ||
sit: new Class(); | ||
}; | ||
@@ -226,3 +232,4 @@ var c3 = { | ||
ipsum: [ 'sit amet' ], | ||
dolor: null | ||
dolor: null, | ||
sit: new Class(); | ||
}; | ||
@@ -232,8 +239,10 @@ var c4 = { | ||
ipsum: 'sit amet', | ||
dolor: new Date() | ||
dolor: new Date(), | ||
sit: {} | ||
}; | ||
inspector.validate(schema, c1); // Valid | ||
inspector.validate(schema, c2); // Valid | ||
inspector.validate(schema, c3); // Valid | ||
inspector.validate(schema, c4); // Invalid: @.lorem must be a number, @dolor must be a number, a string or null | ||
inspector.validate(schema, c4); // Invalid: @.lorem must be a number, @dolor must be a number, a string or null, @.sit must be an instance of Class, but is object | ||
``` | ||
@@ -240,0 +249,0 @@ |
@@ -96,2 +96,3 @@ var should = require('should'); | ||
suite('schema #1.1 (Types tests)', function () { | ||
function F() {}; | ||
var schema = { | ||
@@ -103,3 +104,4 @@ type: 'array', | ||
{ type: 'number' }, | ||
{ type: 'integer' } | ||
{ type: 'integer' }, | ||
{ type: F }, | ||
] | ||
@@ -113,3 +115,4 @@ }; | ||
1234.1234, | ||
1234 | ||
1234, | ||
new F() | ||
]; | ||
@@ -124,2 +127,3 @@ var result = si.validate(schema, candidate); | ||
test('candidate #2', function () { | ||
function G() {}; | ||
var candidate = [ | ||
@@ -129,3 +133,4 @@ null, | ||
1234, | ||
1234.1234 | ||
1234.1234, | ||
new G() | ||
]; | ||
@@ -136,5 +141,7 @@ var result = si.validate(schema, candidate); | ||
result.should.have.property('error').with.be.an.instanceof(Array) | ||
.and.be.lengthOf(2); | ||
.and.be.lengthOf(3); | ||
result.error[0].property.should.equal('@[0]'); | ||
result.error[1].property.should.equal('@[3]'); | ||
result.error[2].property.should.equal('@[4]'); | ||
result.error[2].message.should.equal('must be and instance of F, but is an instance of G'); | ||
}); | ||
@@ -141,0 +148,0 @@ }); // suite "schema #1.1" |
296988
5268
1267