Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean out modifications to primitives

  • Loading branch information...
commit 1dd6acbdeda7304b9fd66ce393c940d95ac169a8 1 parent 6c1d7ff
@kriszyp authored
Showing with 13 additions and 8 deletions.
  1. +13 −8 lib/validate.js
View
21 lib/validate.js
@@ -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:
@@ -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){
@@ -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);
@@ -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");
}
Please sign in to comment.
Something went wrong with that request. Please try again.