Repository navigation
Expand file tree
/
Copy pathvalidation.js
More file actions
116 lines (98 loc) · 3 KB
/
Copy pathvalidation.js
File metadata and controls
116 lines (98 loc) · 3 KB
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
/* jshint node: true */
'use strict';
var sanitize = require('google-caja').sanitize;
var _ = require('lodash');
var config = require('./config');
module.exports = {
isNumber: function(datetime) {
// return true if this is a number
return !isNaN(parseFloat(datetime)) && isFinite(datetime);
},
createAuction: function(start, end, adsStart, adsEnd) {
// ensure all three parameters are given
if (start && end && adsStart && adsEnd) {
return this.updateAuction(start, end, adsStart, adsEnd);
}
else { return false; }
},
updateAuction: function(start, end, adsStart, adsEnd) {
// check that start datetime is valid millisec
if (start && !this.isNumber(start)) {
return false;
}
// check that end datetime is a valid milli sec
if (end && !this.isNumber(end)) {
return false;
}
// check that end is after start
if (start && end && end < start) {
return false;
}
// check that ads start datetime is valid millisec
if (adsStart && !this.isNumber(adsStart)) {
return false;
}
// check that ads end datetime is a valid milli sec
if (adsEnd && !this.isNumber(adsEnd)) {
return false;
}
// check that adsEnd is after adsStart
if (adsStart && adsEnd && adsEnd < adsStart) {
return false;
}
// check that adsStart is after end
if (adsStart && end && adsStart < end) {
return false;
}
return true;
},
createBid: function(price, slots) {
// ensure all parameters are given
if (price && slots) {
return this.updateBid(price, slots);
}
else { return false; }
},
updateBid: function(price, slots) {
// validate all inputs are numbers
// validate that slots can't be negative
// validate end time is after start time
// check that start datetime is valid millisec
if (price && !this.isNumber(price)) {
return false;
}
// check that end datetime is a valid milli sec
if (slots && !this.isNumber(slots)) {
return false;
}
if (price && price < 0) { return false; }
if (slots && slots < 0) { return false; }
return true;
},
html: function(html) {
function urlX(url) { if(/^https?:\/\//.test(url)) { return url; }}
return sanitize(html, urlX);
},
regions: function(regions) {
// get all regions by name
var configRegions = config.regions;
var configRegionNames = _.pluck(configRegions, 'name');
var valid = true;
if (regions.length < 1) { valid = false; }
regions.forEach(function(region) {
// validate region name
if (_.contains(configRegionNames, region.name) === false) {
valid = false;
}
// validate slots exist
if (!region.slots) { valid = false; }
// valid slots is a number
if (valid && !(!isNaN(parseFloat(region.slots)) && isFinite(region.slots))) {
valid = false;
}
// validate slots is not negative
if (valid && region.slots < 1) { valid = false; }
});
return valid;
}
};