UNPKG

1.3 kBJavaScriptView Raw
1var validate = require('validate.js');
2var moment = require('moment');
3
4validate.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
36validate.extend(validate.validators.datetime, {
37 // The value is guaranteed not to be null or undefined but otherwise it
38 // could be anything.
39 parse: function(value) {
40 return +moment.utc(value);
41 },
42 // Input is a unix timestamp
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});