@@ -17,13 +17,15 @@ empty list will be returned. A validation error will have two properties:
17
17
define ( [ ] , function ( ) {
18
18
var exports = validate ;
19
19
// setup primitive classes to be JSON Schema types
20
- String . type = "string" ;
21
- Boolean . type = "boolean" ;
22
- Number . type = "number" ;
23
20
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
+ }
27
29
exports . validate = validate ;
28
30
function validate ( /*Any*/ instance , /*Object*/ schema ) {
29
31
// Summary:
@@ -55,6 +57,9 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
55
57
if ( ! options ) options = { } ;
56
58
var _changing = options . changing ;
57
59
60
+ function getType ( schema ) {
61
+ return schema . type || ( primitiveConstructors [ schema . name ] == schema && schema . name . toLowerCase ( ) ) ;
62
+ }
58
63
var errors = [ ] ;
59
64
// validate a value against a property definition
60
65
function checkProp ( value , schema , path , i ) {
@@ -65,7 +70,7 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
65
70
errors . push ( { property :path , message :message } ) ;
66
71
}
67
72
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 ) ) ) {
69
74
if ( typeof schema == 'function' ) {
70
75
if ( ! ( value instanceof schema ) ) {
71
76
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
117
122
addError ( "is missing and it is required" ) ;
118
123
}
119
124
} else {
120
- errors = errors . concat ( checkType ( schema . type , value ) ) ;
125
+ errors = errors . concat ( checkType ( getType ( schema ) , value ) ) ;
121
126
if ( schema . disallow && ! checkType ( schema . disallow , value ) . length ) {
122
127
addError ( " disallowed value was matched" ) ;
123
128
}
0 commit comments