Skip to content

Commit

Permalink
Clean out modifications to primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Sep 28, 2011
1 parent 6c1d7ff commit 1dd6acb
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ empty list will be returned. A validation error will have two properties:
define([], function(){
var exports = validate;
// setup primitive classes to be JSON Schema types
String.type = "string";
Boolean.type = "boolean";
Number.type = "number";
exports.Integer = {type:"integer"};
Object.type = "object";
Array.type = "array";
Date.type = "date";
var primitiveConstructors = {
String: String,
Boolean: Boolean,
Number: Number,
Object: Object,
Array: Array,
Date: Date
}
exports.validate = validate;
function validate(/*Any*/instance,/*Object*/schema) {
// Summary:
Expand Down Expand Up @@ -55,6 +57,9 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
if (!options) options = {};
var _changing = options.changing;

function getType(schema){
return schema.type || (primitiveConstructors[schema.name] == schema && schema.name.toLowerCase());
}
var errors = [];
// validate a value against a property definition
function checkProp(value, schema, path,i){
Expand All @@ -65,7 +70,7 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
errors.push({property:path,message:message});
}

if((typeof schema != 'object' || schema instanceof Array) && (path || typeof schema != 'function') && !(schema && schema.type)){
if((typeof schema != 'object' || schema instanceof Array) && (path || typeof schema != 'function') && !(schema && getType(schema))){
if(typeof schema == 'function'){
if(!(value instanceof schema)){
addError("is not an instance of the class/constructor " + schema.name);
Expand Down Expand Up @@ -117,7 +122,7 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
addError("is missing and it is required");
}
}else{
errors = errors.concat(checkType(schema.type,value));
errors = errors.concat(checkType(getType(schema),value));
if(schema.disallow && !checkType(schema.disallow,value).length){
addError(" disallowed value was matched");
}
Expand Down

0 comments on commit 1dd6acb

Please sign in to comment.