Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 1dd6acb

Browse files
committed
Clean out modifications to primitives
1 parent 6c1d7ff commit 1dd6acb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/validate.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ empty list will be returned. A validation error will have two properties:
1717
define([], function(){
1818
var exports = validate;
1919
// setup primitive classes to be JSON Schema types
20-
String.type = "string";
21-
Boolean.type = "boolean";
22-
Number.type = "number";
2320
exports.Integer = {type:"integer"};
24-
Object.type = "object";
25-
Array.type = "array";
26-
Date.type = "date";
21+
var primitiveConstructors = {
22+
String: String,
23+
Boolean: Boolean,
24+
Number: Number,
25+
Object: Object,
26+
Array: Array,
27+
Date: Date
28+
}
2729
exports.validate = validate;
2830
function validate(/*Any*/instance,/*Object*/schema) {
2931
// Summary:
@@ -55,6 +57,9 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
5557
if (!options) options = {};
5658
var _changing = options.changing;
5759

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

68-
if((typeof schema != 'object' || schema instanceof Array) && (path || typeof schema != 'function') && !(schema && schema.type)){
73+
if((typeof schema != 'object' || schema instanceof Array) && (path || typeof schema != 'function') && !(schema && getType(schema))){
6974
if(typeof schema == 'function'){
7075
if(!(value instanceof schema)){
7176
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
117122
addError("is missing and it is required");
118123
}
119124
}else{
120-
errors = errors.concat(checkType(schema.type,value));
125+
errors = errors.concat(checkType(getType(schema),value));
121126
if(schema.disallow && !checkType(schema.disallow,value).length){
122127
addError(" disallowed value was matched");
123128
}

0 commit comments

Comments
 (0)