/
datatypes.js
188 lines (174 loc) · 4.1 KB
/
datatypes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Datatype verification -- may modify the value by casting
*/
var datatypes = new (function () {
var _isArray = function (obj) {
return obj &&
typeof obj === 'object' &&
typeof obj.length === 'number' &&
typeof obj.splice === 'function' &&
!(obj.propertyIsEnumerable('length'));
};
this.string = function (name, val) {
return {
err: null,
val: String(val)
};
};
this.number = function (name, val) {
if (isNaN(val)) {
return {
err: 'Field "' + name + '" must be a Number.',
val: null
};
};
return {
err: null,
val: Number(val)
};
};
this.int = function (name, val) {
// Allow decimal values like 10.0 and 3.0
if (Math.round(val) != val) {
return {
err: 'Field "' + name + '" must be an integer.',
val: null
};
};
return {
err: null,
val: parseInt(val, 10)
};
};
this.boolean = function (name, val) {
var validated;
switch (typeof val) {
case 'string':
if (val == 'true') {
validated = true;
}
else if (val == 'false') {
validated = false;
}
break;
case 'number':
if (val == 1) {
validated = true;
}
else if (val == 0) {
validated = false;
}
break;
case 'boolean':
validated = val;
break;
default:
// Nothing
}
if (typeof validated != 'boolean') {
return {
err: 'Field "' + name + '" must be a Boolean.',
val: null
};
};
return {
err: null,
val: validated
};
};
this.object = function (name, val) {
// Sure, Arrays are technically Objects, but we're treating Array as a
// separate datatype. Remember, instanceof Array fails across window
// boundaries, so let's also make sure the Object doesn't have a 'length'
// property.
if (typeof val != 'object' || _isArray(val)) {
return {
err: 'Field "' + name + '" must be an Object.',
val: null
};
};
return {
err: null,
val: val
};
};
this.array = function (name, val) {
// instanceof check can fail across window boundaries. Also check
// to make sure there's a length property
if (!_isArray(val)) {
return {
err: 'Field "' + name + '" must be an Array.',
val: null
};
};
return {
err: null,
val: val
};
};
this.date = function (name, val) {
var dt = geddy.date.parse(val);
if (dt) {
return {
err: null,
val: dt
};
}
else {
return {
err: 'Field "' + name + '" must be in a valid date format.',
val: null
};
}
};
this.datetime = function (name, val) {
var dt = geddy.date.parse(val);
if (dt) {
return {
err: null,
val: dt
};
}
else {
return {
err: 'Field "' + name + '" must be in a valid datetime format.',
val: null
};
}
};
// This is a hack -- we're saving times as Dates of 12/31/1969, and the
// desired time
this.time = function (name, val) {
var dt = geddy.date.parse(val);
if (dt) {
return {
err: null,
val: dt
};
}
else {
return {
err: 'Field "' + name + '" must be in a valid time format.',
val: null
};
}
};
})();
module.exports = datatypes;