-
Notifications
You must be signed in to change notification settings - Fork 55
/
datatypes.js
351 lines (330 loc) · 7.98 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* 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.
*
*/
var model = require('./index')
, utils = require('utilities')
, i18n = utils.i18n
, datatypes
, _isArray
, _serialize
, _quoteize
, _escape;
_isArray = function (obj) {
// Defer to native if possible
if (typeof Array.isArray == 'function') {
return Array.isArray(obj);
}
return obj &&
typeof obj === 'object' &&
typeof obj.length === 'number' &&
typeof obj.splice === 'function' &&
!(obj.propertyIsEnumerable('length'));
};
_serialize = function (input, options) {
var val = String(input)
, opts = options || {};
if (opts.escape) {
val = _escape(val, opts.escape);
}
if (opts.useQuotes) {
val = _quoteize(val);
}
if (opts.lowercase) {
val = val.toLowerCase();
}
return val;
};
_quoteize = function (val) {
return ["'", "'"].join(val);
}
_escape = function (s, type) {
var ret;
switch (type) {
// Scrub input for basic SQL injection protection
case 'sql':
ret = s.replace(/'/g, "''");
break;
// Backslash-esc single quotes for use in M/R JS sourcecode str
case 'js':
ret = s.replace(/'/g, "\\'");
break;
default:
throw new Error(type + ' is not a valid type of escaping.');
}
return ret;
};
/*
* Datatype verification -- may modify the value by casting
*/
datatypes = {
'string': {
validate: function (name, val, locale) {
return {
err: null
, val: String(val)
};
}
, serialize: function (input, options) {
return _serialize(input, options);
}
}
, 'text': {
validate: function (name, val, locale) {
return {
err: null
, val: String(val)
};
}
, serialize: function (input, options) {
return _serialize(input, options);
}
}
, 'number': {
validate: function (name, val, locale) {
if (isNaN(val)) {
return {
err: i18n.getText('model.validatesNumber', {name: name}, locale)
, val: null
};
}
return {
err: null
, val: Number(val)
};
}
, serialize: function (input, options) {
var opts = options || {};
return _serialize(input, {
escape: opts.escape
});
}
}
, 'int': {
validate: function (name, val, locale) {
// Allow decimal values like 10.0 and 3.0
if (Math.round(val) != val) {
return {
err: i18n.getText('model.validatesInteger', {name: name}, locale)
, val: null
};
}
return {
err: null
, val: parseInt(val, 10)
};
}
, serialize: function (input, options) {
var opts = options || {};
return _serialize(input, {
escape: opts.escape
});
}
}
, 'boolean': {
validate: function (name, val, locale) {
var validated;
switch (typeof val) {
case 'string':
switch (val) {
case 'true':
case 't':
case 'yes':
case '1':
validated = true;
break;
case 'false':
case 'f':
case 'no':
case '0':
validated = false;
break;
}
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: i18n.getText('model.validatesBoolean', {name: name}, locale)
, val: null
};
}
return {
err: null
, val: validated
};
}
, serialize: function (input, options) {
var opts = options || {};
return _serialize(input, {
escape: opts.escape
});
}
}
, 'object': {
validate: function (name, val, locale) {
// Allow saving of arrays as the datatype array only saves arrays
// of numbers or strings correctly, but not arrays of objects
// We're just not bothing with a separate Array datatype anymore
// maybe a JSON string?
if (typeof val === 'string') {
try {
var obj = JSON.parse(val);
return {
err: null,
val: obj
}
}
catch(err) {
return {
err: i18n.getText('model.validatesObject', {name: name}, locale),
val: null
}
}
}
else if (typeof val != 'object') {
return {
err: i18n.getText('model.validatesObject', {name: name}, locale)
, val: null
};
}
return {
err: null
, val: val
};
}
, serialize: function (input, options) {
var val
, opts = options || {};
// Arrays will be converted to JSON
if (_isArray(input)) {
val = JSON.stringify(input);
}
// Otherwise just try to serialize via toString
else if (typeof input.toString == 'function') {
val = input.toString();
// If this happens the object had no usefull toString()
// method and we should make JSON out of it
if (val == "[object Object]") {
val = JSON.stringify(input);
}
}
else {
val = JSON.stringify(input);
}
// FIXME: Does escaping a JSONized object make sense?
return _serialize(val, opts);
}
}
, 'date': {
validate: function (name, val, locale) {
var dt = utils.date.parse(val);
if (dt) {
return {
err: null
, val: dt
};
}
else {
return {
err: i18n.getText('model.validatesDate', {name: name}, locale)
, val: null
};
}
}
, serialize: function (input, options) {
var val
, opts = options || {};
if (model.config.useUTC) {
val = utils.date.toUTC(input);
}
else {
val = input;
}
val = utils.date.strftime(val, '%F');
return _serialize(val, opts);
}
}
, 'datetime': {
validate: function (name, val, locale) {
var dt = utils.date.parse(val);
if (dt) {
return {
err: null
, val: dt
};
}
else {
return {
err: i18n.getText('model.validatesDatetime', {name: name}, locale)
, val: null
};
}
}
, serialize: function (input, options) {
var val
, opts = options || {};
if (model.config.useUTC) {
val = utils.date.toUTC(input);
}
else {
val = input;
}
val = utils.date.toISO8601(val, {utc: true});
return _serialize(val, options);
}
}
// This is a hack -- we're saving times as Dates of 12/31/1969, and the
// desired time
, 'time': {
validate: function (name, val, locale) {
var dt = utils.date.parse(val);
if (dt) {
return {
err: null
, val: dt
};
}
else {
return {
err: i18n.getText('model.validatesTime', {name: name}, locale)
, val: null
};
}
}
, serialize: function (input, options) {
var val
, opts = options || {};
val = utils.date.strftime(val, '%T');
return _serialize(val, opts);
}
}
};
module.exports = datatypes;
// Lazy-load; model loads this file first
model = require('./index');