@@ -4,15 +4,7 @@ var t = require('tcomb');
4
4
var fcomb = require ( 'fcomb' ) ;
5
5
var util = require ( './util' ) ;
6
6
7
- var Str = t . Str ;
8
- var Num = t . Num ;
9
- var Bool = t . Bool ;
10
- var Obj = t . Obj ;
11
- var Arr = t . Arr ;
12
- var subtype = t . subtype ;
13
- var enums = t . enums ;
14
-
15
- var SchemaType = enums . of ( 'null string number integer boolean object array' , 'SchemaType' ) ;
7
+ var SchemaType = t . enums . of ( 'null string number integer boolean object array' , 'SchemaType' ) ;
16
8
17
9
function and ( f , g ) {
18
10
return f ? fcomb . and ( f , g ) : g ;
@@ -22,7 +14,7 @@ var types = {
22
14
23
15
string : function ( s ) {
24
16
if ( s . hasOwnProperty ( 'enum' ) ) {
25
- return enums . of ( s [ 'enum' ] ) ;
17
+ return t . enums . of ( s [ 'enum' ] ) ;
26
18
}
27
19
var predicate ;
28
20
if ( s . hasOwnProperty ( 'minLength' ) ) {
@@ -35,10 +27,10 @@ var types = {
35
27
predicate = and ( predicate , fcomb . regexp ( new RegExp ( s . pattern ) ) ) ;
36
28
}
37
29
if ( s . hasOwnProperty ( 'format' ) ) {
38
- t . assert ( formats . hasOwnProperty ( s . format ) , 'missing format %s , use the `registerFormat` API' , s . format ) ;
30
+ t . assert ( formats . hasOwnProperty ( s . format ) , '[tcomb-json-schema] Missing format ' + s . format + ' , use the (format, predicate) API') ;
39
31
predicate = and ( predicate , formats [ s . format ] ) ;
40
32
}
41
- return predicate ? subtype ( Str , predicate ) : Str ;
33
+ return predicate ? t . subtype ( t . String , predicate ) : t . String ;
42
34
} ,
43
35
44
36
number : function ( s ) {
@@ -56,7 +48,7 @@ var types = {
56
48
if ( s . hasOwnProperty ( 'integer' ) && s . integer ) {
57
49
predicate = and ( predicate , util . isInteger ) ;
58
50
}
59
- return predicate ? subtype ( Num , predicate ) : Num ;
51
+ return predicate ? t . subtype ( t . Number , predicate ) : t . Number ;
60
52
} ,
61
53
62
54
integer : function ( s ) {
@@ -71,11 +63,11 @@ var types = {
71
63
and ( predicate , fcomb . lt ( s . maximum ) ) :
72
64
and ( predicate , fcomb . lte ( s . maximum ) ) ;
73
65
}
74
- return predicate ? subtype ( util . Int , predicate ) : util . Int ;
66
+ return predicate ? t . subtype ( util . Int , predicate ) : util . Int ;
75
67
} ,
76
68
77
69
boolean : function ( s ) {
78
- return Bool ;
70
+ return t . Boolean ;
79
71
} ,
80
72
81
73
object : function ( s ) {
@@ -91,16 +83,16 @@ var types = {
91
83
if ( s . properties . hasOwnProperty ( k ) ) {
92
84
hasProperties = true ;
93
85
var type = transform ( s . properties [ k ] ) ;
94
- props [ k ] = required [ k ] || type === Bool ? type : t . maybe ( type ) ;
86
+ props [ k ] = required [ k ] || type === t . Boolean ? type : t . maybe ( type ) ;
95
87
}
96
88
}
97
- return hasProperties ? t . struct ( props , s . description ) : Obj ;
89
+ return hasProperties ? t . struct ( props , s . description ) : t . Object ;
98
90
} ,
99
91
100
92
array : function ( s ) {
101
93
if ( s . hasOwnProperty ( 'items' ) ) {
102
94
var items = s . items ;
103
- if ( Obj . is ( items ) ) {
95
+ if ( t . Object . is ( items ) ) {
104
96
return t . list ( transform ( s . items ) ) ;
105
97
}
106
98
return t . tuple ( items . map ( transform ) ) ;
@@ -112,7 +104,7 @@ var types = {
112
104
if ( s . hasOwnProperty ( 'maxItems' ) ) {
113
105
predicate = and ( predicate , fcomb . maxLength ( s . maxItems ) ) ;
114
106
}
115
- return predicate ? subtype ( Arr , predicate ) : Arr ;
107
+ return predicate ? t . subtype ( t . Array , predicate ) : t . Array ;
116
108
} ,
117
109
118
110
null : function ( ) {
@@ -122,26 +114,26 @@ var types = {
122
114
} ;
123
115
124
116
function transform ( s ) {
125
- t . assert ( Obj . is ( s ) ) ;
117
+ t . assert ( t . Object . is ( s ) ) ;
126
118
if ( ! s . hasOwnProperty ( 'type' ) ) {
127
119
return t . Any ;
128
120
}
129
121
var type = s . type ;
130
122
if ( SchemaType . is ( type ) ) {
131
123
return types [ type ] ( s ) ;
132
124
}
133
- if ( Arr . is ( type ) ) {
125
+ if ( t . Array . is ( type ) ) {
134
126
return t . union ( type . map ( function ( type ) {
135
127
return types [ type ] ( s ) ;
136
128
} ) ) ;
137
129
}
138
- t . fail ( t . format ( 'unsupported json schema %j' , s ) ) ;
130
+ t . fail ( '[tcomb-json-schema] Unsupported json schema ' + t . stringify ( s ) ) ;
139
131
}
140
132
141
133
var formats = { } ;
142
134
143
135
transform . registerFormat = function registerFormat ( format , predicate ) {
144
- t . assert ( ! formats . hasOwnProperty ( format ) , '[tcomb-json-schema] duplicated format %s' , format ) ;
136
+ t . assert ( ! formats . hasOwnProperty ( format ) , '[tcomb-json-schema] Duplicated format ' + format ) ;
145
137
formats [ format ] = predicate ;
146
138
} ;
147
139
0 commit comments