1 | var validate = require('validate.js');
|
2 | var moment = require('moment');
|
3 |
|
4 | validate.validators.bbox = function(value, options) {
|
5 | if (!options.presence && !value) {
|
6 | return null;
|
7 | }
|
8 | if (options.presence && !value) {
|
9 | return 'not supplied';
|
10 | }
|
11 | var errMsg = 'not a valid bbox string';
|
12 | var coords = value.split(',');
|
13 | if (coords.length != 4) {
|
14 | return errMsg;
|
15 | }
|
16 | for (var i =0; i < coords.length; i++) {
|
17 | if (!validate.isNumber(parseFloat(coords[i]))) {
|
18 | return errMsg;
|
19 | }
|
20 | }
|
21 | if (coords[0] < -180 || coords[0] > 180) {
|
22 | return errMsg;
|
23 | }
|
24 | if (coords[2] < -180 || coords[2] > 180) {
|
25 | return errMsg;
|
26 | }
|
27 | if (coords[1] < -90 || coords[1] > 90) {
|
28 | return errMsg;
|
29 | }
|
30 | if (coords[3] < -90 || coords[3] > 90) {
|
31 | return errMsg;
|
32 | }
|
33 | return null;
|
34 | };
|
35 |
|
36 | validate.extend(validate.validators.datetime, {
|
37 |
|
38 |
|
39 | parse: function(value) {
|
40 | return +moment.utc(value);
|
41 | },
|
42 |
|
43 | format: function(value, options) {
|
44 | var format = options.dateOnly ? 'YYYY-MM-DD' : 'YYYY-MM-DD hh:mm:ss';
|
45 | return moment.utc(value).format(format);
|
46 | }
|
47 | });
|