diff --git a/lib/extensions/Array.js b/lib/extensions/Array.js index 474e8f5..efdb332 100644 --- a/lib/extensions/Array.js +++ b/lib/extensions/Array.js @@ -11,20 +11,24 @@ var ArraySchema = module.exports = Schema.extensions.ArraySchema = Schema.extend validate : function(instance) { // Instance must be an instance of Array - if (!(instance instanceof Array)) return false + if (!(instance instanceof Array)) throw new Error('not an array') // Checking length if (this.min === this.max) { - if (instance.length !== this.min) return false + if (instance.length !== this.min) throw new Error('array does not contain enought entries (' + instance.length + ' instead of ' + this.min) } else { - if (this.min > 0 && instance.length < this.min) return false - if (this.max < Infinity && instance.length > this.max) return false + if (this.min > 0 && instance.length < this.min) throw new Error('array does not contain enought entries (' + instance.length + ' instead of ' + this.min) + if (this.max < Infinity && instance.length > this.max) throw new Error('array does contain too many entries (' + instance.length + ' instead of ' + this.min) } // Checking conformance to the given item schema for (var i = 0; i < instance.length; i++) { - if (!this.itemSchema.validate(instance[i])) return false; + try { + this.itemSchema.validate(instance[i]) + } catch(e) { + throw new Error('array contains invalid entry at ' + i + ' --> ' + e) + } } return true diff --git a/lib/extensions/Boolean.js b/lib/extensions/Boolean.js index 7b79652..aa669d0 100644 --- a/lib/extensions/Boolean.js +++ b/lib/extensions/Boolean.js @@ -2,7 +2,10 @@ var Schema = require('../BaseSchema') var BooleanSchema = module.exports = Schema.extensions.BooleanSchema = new Schema.extend({ validate : function(instance) { - return Object(instance) instanceof Boolean + if (!Object(instance) instanceof Boolean) + throw new Error('not a Boolean') + + return true }, toJSON : function() { diff --git a/lib/extensions/Number.js b/lib/extensions/Number.js index 69a8210..9f485bc 100644 --- a/lib/extensions/Number.js +++ b/lib/extensions/Number.js @@ -47,12 +47,19 @@ var NumberSchema = module.exports = Schema.extensions.NumberSchema = Schema.exte publicFunctions : [ 'min', 'above', 'max', 'below', 'step' ], validate : function(instance) { - return (Object(instance) instanceof Number) && - (this.exclusiveMinimum ? instance > this.minimum - : instance >= this.minimum) && - (this.exclusiveMaximum ? instance < this.maximum - : instance <= this.maximum) && - (this.divisibleBy === 0 || instance % this.divisibleBy === 0) + if (! Object(instance) instanceof Number) + throw new Error('not a Number') + if (!(this.exclusiveMinimum ? instance > this.minimum + : instance >= this.minimum)) + throw new Error(instance + ' does not exceed minimum ' + this.minimum) + if (! (this.exclusiveMaximum ? instance < this.maximum + : instance <= this.maximum)) + throw new Error(instance + ' exceeds maximum ' + this.maximum) + + if (!(this.divisibleBy === 0 || instance % this.divisibleBy === 0)) + throw new Error('not an array') + + return true }, toJSON : function() { diff --git a/lib/patterns/anything.js b/lib/patterns/anything.js index 1adbf92..3495b17 100644 --- a/lib/patterns/anything.js +++ b/lib/patterns/anything.js @@ -2,7 +2,9 @@ var Schema = require('../BaseSchema') var AnythingSchema = module.exports = Schema.patterns.AnythingSchema = Schema.extend({ validate : function(instance) { - return instance != null + if (instance == null) + throw new Error('may not be null') + return true }, toJSON : function() { diff --git a/lib/patterns/class.js b/lib/patterns/class.js index 494a06a..4c69266 100644 --- a/lib/patterns/class.js +++ b/lib/patterns/class.js @@ -6,7 +6,9 @@ var ClassSchema = module.exports = Schema.patterns.ClassSchema = Schema.extend({ }, validate : function(instance) { - return instance instanceof this.constructor + if (instance instanceof this.constructor) + throw new Error('not a class constructor') + return true; } }) diff --git a/lib/patterns/equality.js b/lib/patterns/equality.js index ed33ef4..b0ab622 100644 --- a/lib/patterns/equality.js +++ b/lib/patterns/equality.js @@ -26,7 +26,9 @@ var EqualitySchema = module.exports = Schema.patterns.EqualitySchema = Schema.ex }, validate : function(instance) { - return equal(instance, this.object) + if (! equal(instance, this.object)) + throw new Error(instance + ' not equal to ' + this.object.toString() ) + return true; }, toJSON : function() { diff --git a/lib/patterns/nothing.js b/lib/patterns/nothing.js index 4966243..79f88a4 100644 --- a/lib/patterns/nothing.js +++ b/lib/patterns/nothing.js @@ -2,7 +2,9 @@ var Schema = require('../BaseSchema') var NothingSchema = module.exports = Schema.patterns.NothingSchema = Schema.extend({ validate : function(instance) { - return instance == null + if (instance != null) + throw new Error('must be null') + return true }, toJSON : function() { diff --git a/lib/patterns/object.js b/lib/patterns/object.js index 38cfcf3..e82a01c 100644 --- a/lib/patterns/object.js +++ b/lib/patterns/object.js @@ -23,14 +23,18 @@ var ObjectSchema = module.exports = Schema.patterns.ObjectSchema = Schema.extend validate : function(instance) { var self = this - if (instance == null) return false + if (instance == null) throw new Error('No data') // Simple string properties - var stringPropsValid = Object.keys(this.stringProps).every(function(key) { - return (self.stringProps[key].min === 0 && !(key in instance)) || - (self.stringProps[key].value.validate(instance[key])) + Object.keys(this.stringProps).every(function(key) { + if (self.stringProps[key].min === 0 && !(key in instance)) + throw new Error('key "' + key + '" is obligated but missing'); + try { + self.stringProps[key].value.validate(instance[key]) + } catch(e) { + throw new Error('Parse error for key "' + key + '" --> ' + e); + } }) - if (!stringPropsValid) return false // If there are no RegExp and other validator, that's all if (!this.regexpProps.length && this.other === anything) return true @@ -41,17 +45,25 @@ var ObjectSchema = module.exports = Schema.patterns.ObjectSchema = Schema.extend // Checking the key against every key regexps checked = false - var regexpPropsValid = Object.keys(this.regexpProps).every(function(key) { - return (!self.regexpProps[key].key.test(key) || - ((checked = true) && self.regexpProps[key].value.validate(instance[key])) - ) + Object.keys(this.regexpProps).every(function(key) { + if (!self.regexpProps[key].key.test(key)) return + checked = true + try { + self.regexpProps[key].value.validate(instance[key]) + } catch(e) { + throw new Error('Parse error for regexp in key "' + key + '" --> ' + e); + } }) - if (!regexpPropsValid) return false // If the key is not matched by regexps and by simple string checks // then check it against this.other - if (!checked && !(key in this.stringProps) && !this.other.validate(instance[key])) return false - + if (!checked && !(key in this.stringProps)) { + try { + this.other.validate(instance[key]) + } catch(e) { + throw new Error('Parse error for key "' + key + '" --> ' + e); + } + } } // If all checks passed, the instance conforms to the schema diff --git a/lib/patterns/or.js b/lib/patterns/or.js index a351f2f..eb0c080 100644 --- a/lib/patterns/or.js +++ b/lib/patterns/or.js @@ -8,7 +8,7 @@ var OrSchema = module.exports = Schema.patterns.OrSchema = Schema.extend({ validate : function(instance) { return this.schemas.some(function(sch) { - return sch.validate(instance) + return sch.validate(instance) //TODO: add a name of the schema to the thrown error? }) }, diff --git a/lib/patterns/reference.js b/lib/patterns/reference.js index c2c41c2..677d930 100644 --- a/lib/patterns/reference.js +++ b/lib/patterns/reference.js @@ -6,7 +6,9 @@ var ReferenceSchema = module.exports = Schema.patterns.ReferenceSchema = Schema. }, validate : function(instance) { - return instance === this.value + if (instance !== this.value) + throw new Error(instance + ' is not the same as ' + this.value) + return true; }, toJSON : function() { diff --git a/lib/patterns/regexp.js b/lib/patterns/regexp.js index 7d8ae96..606f497 100644 --- a/lib/patterns/regexp.js +++ b/lib/patterns/regexp.js @@ -6,7 +6,13 @@ var RegexpSchema = module.exports = Schema.patterns.RegexpSchema = Schema.extend }, validate : function(instance) { - return Object(instance) instanceof String && (!this.regexp || this.regexp.test(instance)) + if (! Object(instance) instanceof String) { + throw new Error('not a String') + } + if (this.regexp && !this.regexp.test(instance)) + throw new Error('"' + instance + '" does not match pattern "' + this.regexp +'"') + + return true; }, toJSON : function() {