Skip to content

Commit

Permalink
- added validation feedback in form of thrown error
Browse files Browse the repository at this point in the history
- the public API is now broken, still needs to be fixed
  • Loading branch information
piebev committed Jul 3, 2014
1 parent 39c81ec commit 4f4e009
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 31 deletions.
14 changes: 9 additions & 5 deletions lib/extensions/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/extensions/Boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 13 additions & 6 deletions lib/extensions/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion lib/patterns/anything.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion lib/patterns/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
})

Expand Down
4 changes: 3 additions & 1 deletion lib/patterns/equality.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion lib/patterns/nothing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
36 changes: 24 additions & 12 deletions lib/patterns/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/patterns/or.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
})
},

Expand Down
4 changes: 3 additions & 1 deletion lib/patterns/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion lib/patterns/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 4f4e009

Please sign in to comment.