@bluedot-innovation/models 5.1.6 → 5.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/__tests__/helpers.js +26 -0
- package/__tests__/project/project.rules.json +2 -1
- package/__tests__/project/project.spec.js +26 -0
- package/__tests__/user/user.rules.js +2 -1
- package/__tests__/user/user.spec.js +15 -0
- package/__tests__/utils/utils.spec.js +1 -1
- package/__tests__/zone/zone.data.json +1 -1
- package/__tests__/zone/zone.rules.json +4 -0
- package/__tests__/zone/zone.spec.js +36 -1
- package/constants/tests.js +1 -0
- package/helpers.js +24 -1
- package/models/Beacon.js +5 -4
- package/models/Project.js +14 -2
- package/models/User.js +14 -2
- package/models/Zone.js +6 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
let chai = require('chai')
|
|
2
|
+
let assert = chai.assert
|
|
3
|
+
|
|
4
|
+
let helpers = {}
|
|
5
|
+
|
|
6
|
+
helpers.testFalsyValue = (falsyValue, arrayField, docObject, model, callback) => {
|
|
7
|
+
docObject = JSON.parse(JSON.stringify(docObject))
|
|
8
|
+
docObject[arrayField] = [falsyValue]
|
|
9
|
+
const Model = new model(docObject)
|
|
10
|
+
Model.validate((err, res) => {
|
|
11
|
+
assert.exists(err)
|
|
12
|
+
assert(err instanceof Error, "Should throw error.")
|
|
13
|
+
callback()
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
helpers.testNullValue = (docObject, model, callback) => {
|
|
18
|
+
let Model = new model(docObject)
|
|
19
|
+
Model.validate((err, res) => {
|
|
20
|
+
assert.exists(err, 'err object should exist')
|
|
21
|
+
assert(err instanceof Error, "Should throw error.")
|
|
22
|
+
callback()
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = helpers
|
|
@@ -5,5 +5,6 @@
|
|
|
5
5
|
{"name": "lastUpdateTime", "invalidInput": ["21/07/2017", "saturday"]},
|
|
6
6
|
{"name": "creationTime", "invalidInput": ["21/07/2017", "thursday"]},
|
|
7
7
|
{"name": "accountId", "invalidInput": ["21/07/2017", 21072017, "qwe123-21321-ad"]}
|
|
8
|
-
]
|
|
8
|
+
],
|
|
9
|
+
"arrayTypeFields": ["webhooks"]
|
|
9
10
|
}
|
|
@@ -11,6 +11,8 @@ let assert = chai.assert
|
|
|
11
11
|
let mocha = require('mocha')
|
|
12
12
|
let beforeEach = mocha.beforeEach
|
|
13
13
|
let rules = require('./project.rules.json')
|
|
14
|
+
let helpers = require('../helpers')
|
|
15
|
+
let { FALSY_VALUES } = require('../../constants/tests')
|
|
14
16
|
|
|
15
17
|
let project
|
|
16
18
|
|
|
@@ -96,5 +98,29 @@ testCase('Project', () => {
|
|
|
96
98
|
})
|
|
97
99
|
})
|
|
98
100
|
})
|
|
101
|
+
|
|
102
|
+
it(`should trow an error when webhooks is null`, (done) => {
|
|
103
|
+
project = JSON.parse(JSON.stringify(project))
|
|
104
|
+
project.webhooks = null
|
|
105
|
+
helpers.testNullValue(project, ProjectModel, done)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it(`should trow an error when firebaseApiKey is null`, (done) => {
|
|
109
|
+
project = JSON.parse(JSON.stringify(project))
|
|
110
|
+
project.firebaseApiKey = null
|
|
111
|
+
helpers.testNullValue(project, ProjectModel, done)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('should throw an error when any of the values of the array contains a falsy value', () => {
|
|
115
|
+
let arrayTypeFields = rules.arrayTypeFields
|
|
116
|
+
|
|
117
|
+
arrayTypeFields.map(arrayField => {
|
|
118
|
+
FALSY_VALUES.map(falsyValue => {
|
|
119
|
+
it(`should trow an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
|
|
120
|
+
helpers.testFalsyValue(falsyValue, arrayField, project, ProjectModel, done)
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
})
|
|
99
125
|
})
|
|
100
126
|
})
|
|
@@ -4,5 +4,6 @@ module.exports = {
|
|
|
4
4
|
{name: 'phoneNumber', invalidInputs: ['409203492039284232034398']},
|
|
5
5
|
{name: 'creationTime', invalidInputs: ['21/07/2017', 'thursday']},
|
|
6
6
|
{name: 'lastUpdateTime', invalidInputs: ['21/07/2017', 'saturday']}
|
|
7
|
-
]
|
|
7
|
+
],
|
|
8
|
+
arrayTypeFields: ["projectPermissions"]
|
|
8
9
|
}
|
|
@@ -9,6 +9,8 @@ let chai = require('chai')
|
|
|
9
9
|
let assert = chai.assert
|
|
10
10
|
let mocha = require('mocha')
|
|
11
11
|
let beforeEach = mocha.beforeEach
|
|
12
|
+
let helpers = require('../helpers')
|
|
13
|
+
let { FALSY_VALUES } = require('../../constants/tests')
|
|
12
14
|
|
|
13
15
|
let user
|
|
14
16
|
|
|
@@ -127,4 +129,17 @@ describe('User', () => {
|
|
|
127
129
|
})
|
|
128
130
|
})
|
|
129
131
|
})
|
|
132
|
+
|
|
133
|
+
describe('should throw an error when any of the values of the array contains a falsy value', () => {
|
|
134
|
+
let arrayTypeFields = rules.arrayTypeFields
|
|
135
|
+
|
|
136
|
+
arrayTypeFields.map(arrayField => {
|
|
137
|
+
FALSY_VALUES.map(falsyValue => {
|
|
138
|
+
it(`should trow an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
|
|
139
|
+
helpers.testFalsyValue(falsyValue, arrayField, user, UserModel, done)
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
130
145
|
})
|
|
@@ -2,7 +2,9 @@ let mongoose = require('mongoose')
|
|
|
2
2
|
mongoose.Promise = global.Promise
|
|
3
3
|
let chai = require('chai')
|
|
4
4
|
let assert = chai.assert
|
|
5
|
-
|
|
5
|
+
let rules = require('./zone.rules.json')
|
|
6
|
+
let helpers = require('../helpers')
|
|
7
|
+
let { FALSY_VALUES } = require('../../constants/tests')
|
|
6
8
|
|
|
7
9
|
let ZoneModel = require('../../Models').Zone(mongoose)
|
|
8
10
|
let ZoneModelBuiltInMongoose = require('../../Models').Zone()
|
|
@@ -72,6 +74,17 @@ describe('Zone', () => {
|
|
|
72
74
|
done()
|
|
73
75
|
})
|
|
74
76
|
})
|
|
77
|
+
|
|
78
|
+
it('should set triggerOnExit default and save without errors', (done) => {
|
|
79
|
+
delete zone.triggerOnExit
|
|
80
|
+
|
|
81
|
+
let Zone = new ZoneModel(zone)
|
|
82
|
+
Zone.validate((err, res) => {
|
|
83
|
+
assert.notExists(res)
|
|
84
|
+
assert(err === null, 'err equals null')
|
|
85
|
+
done()
|
|
86
|
+
})
|
|
87
|
+
})
|
|
75
88
|
})
|
|
76
89
|
|
|
77
90
|
describe('Invalid Zone ', () => {
|
|
@@ -230,5 +243,27 @@ describe('Zone', () => {
|
|
|
230
243
|
done()
|
|
231
244
|
})
|
|
232
245
|
})
|
|
246
|
+
|
|
247
|
+
it('should throw an error when triggerOnExit is null', (done) => {
|
|
248
|
+
zone.triggerOnExit = null
|
|
249
|
+
helpers.testNullValue(zone, ZoneModel, done)
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('should throw an error when tags is null', (done) => {
|
|
253
|
+
zone.tags = null
|
|
254
|
+
helpers.testNullValue(zone, ZoneModel, done)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
describe('should throw an error when any of the values of the array contains a falsy value', () => {
|
|
258
|
+
let arrayTypeFields = rules.arrayTypeFields
|
|
259
|
+
|
|
260
|
+
arrayTypeFields.map(arrayField => {
|
|
261
|
+
FALSY_VALUES.map(falsyValue => {
|
|
262
|
+
it(`should trow an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
|
|
263
|
+
helpers.testFalsyValue(falsyValue, arrayField, zone, ZoneModel, done)
|
|
264
|
+
})
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
})
|
|
233
268
|
})
|
|
234
269
|
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports.FALSY_VALUES = [null, false, NaN, "", undefined]
|
package/helpers.js
CHANGED
|
@@ -96,6 +96,8 @@ helpers.checkConditionsSuitableForBeacon = function(zone, next) {
|
|
|
96
96
|
let hasBeacons = false;
|
|
97
97
|
if( zone.geofeatures && zone.geofeatures.length ) {
|
|
98
98
|
for( let geofeature of zone.geofeatures ) {
|
|
99
|
+
if(!geofeature) return next(new Error("Geofeature must have a type"))
|
|
100
|
+
|
|
99
101
|
if( geofeature.type === "beacon" ) {
|
|
100
102
|
hasBeacons = true;
|
|
101
103
|
break;
|
|
@@ -107,6 +109,7 @@ helpers.checkConditionsSuitableForBeacon = function(zone, next) {
|
|
|
107
109
|
if( !zone.conditions || !zone.conditions.length )
|
|
108
110
|
return next(); // No conditions.
|
|
109
111
|
for( let condition of zone.conditions ) {
|
|
112
|
+
if( !condition ) return next(new Error("Condition must have a valid value"))
|
|
110
113
|
if( condition.type === "travelDirection" ) {
|
|
111
114
|
return next(new Error("Zone cannot both contain a beacon and have a Travel Direction condition."))
|
|
112
115
|
}
|
|
@@ -123,6 +126,7 @@ helpers.checkMinimumGeofeaturesValidForZone = function(zone, next) {
|
|
|
123
126
|
if( !zone.conditions || !zone.conditions.length )
|
|
124
127
|
return next(); // No conditions.
|
|
125
128
|
for( let condition of zone.conditions ) {
|
|
129
|
+
if( !condition ) return next(new Error("Condition must have a valid value"))
|
|
126
130
|
if( condition.type === "travelPath" && condition.minimumGeofeaturesCrossed > zone.geofeatures.length ) {
|
|
127
131
|
return next(new Error("Zone must not have minimumGeofeaturesCrossed higher than geofeatures count"))
|
|
128
132
|
}
|
|
@@ -139,12 +143,31 @@ helpers.validateTravelPathConditionCount = function(zone, next) {
|
|
|
139
143
|
let numberOfTravelPathConditions = 0
|
|
140
144
|
|
|
141
145
|
for( let condition of zone.conditions ) {
|
|
146
|
+
if( !condition ) return next(new Error("Condition must have a valid value"))
|
|
142
147
|
if( condition.type === "travelPath" ) numberOfTravelPathConditions ++
|
|
143
|
-
if
|
|
148
|
+
if( numberOfTravelPathConditions > 1 ) return next(new Error("Zone must not have more than 1 travel path condition"))
|
|
144
149
|
}
|
|
145
150
|
next();
|
|
146
151
|
}
|
|
147
152
|
|
|
153
|
+
helpers.checkArrayContainsValidValues = function(document, properties, next) {
|
|
154
|
+
properties.forEach(property => {
|
|
155
|
+
if(document[property] && document[property].length) {
|
|
156
|
+
for(let item of document[property]) {
|
|
157
|
+
if(!Boolean(item)) {
|
|
158
|
+
return next(new Error(`${property} must have a value`))
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
return next()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
helpers.validateNotNull = function(value) {
|
|
168
|
+
return value !== null
|
|
169
|
+
}
|
|
170
|
+
|
|
148
171
|
const isValidObject = data => {
|
|
149
172
|
// data not null, not undefined, must be an object
|
|
150
173
|
return !(data === null || typeof data === 'undefined'|| data.constructor !== Object)
|
package/models/Beacon.js
CHANGED
|
@@ -4,11 +4,12 @@ const patcher = require('@bluedot-innovation/mongoose-json-patch')
|
|
|
4
4
|
const Point = require('../schemas/Point')
|
|
5
5
|
const mongoose = require('mongoose')
|
|
6
6
|
const uuidv4 = require('uuid/v4')
|
|
7
|
+
const helpers = require('../helpers')
|
|
7
8
|
|
|
8
9
|
let schema = {
|
|
9
10
|
_id: {type: String, default: uuidv4},
|
|
10
11
|
name: {type: String, required: [true, 'name is required.']},
|
|
11
|
-
description: {type: String, default: ''},
|
|
12
|
+
description: {type: String, default: '', require: true},
|
|
12
13
|
creationTime: {type: Date, default: Date.now},
|
|
13
14
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
14
15
|
state: {type: String, default: StateEnum.active},
|
|
@@ -28,9 +29,9 @@ let schema = {
|
|
|
28
29
|
// These must be unique
|
|
29
30
|
major: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Major needs to be within range(0-65535)'}]},
|
|
30
31
|
minor: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Minor needs to be within range(0-65535)'}]},
|
|
31
|
-
txPower: {type: Number},
|
|
32
|
-
macAddress: {type: String},
|
|
33
|
-
proximityUUID: {type: String}
|
|
32
|
+
txPower: {type: Number, validate: helpers.validateNotNull},
|
|
33
|
+
macAddress: {type: String, validate: helpers.validateNotNull},
|
|
34
|
+
proximityUUID: {type: String, validate: helpers.validateNotNull}
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
let BeaconSchema = new mongoose.Schema(schema, {collection: 'beacon', autoIndex: false})
|
package/models/Project.js
CHANGED
|
@@ -32,18 +32,30 @@ let ProjectSchema = new mongoose.Schema({
|
|
|
32
32
|
lastSyncTime: {type: Date, default: Date.now},
|
|
33
33
|
name: {type: String, required: [true, 'name is required']},
|
|
34
34
|
accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}},
|
|
35
|
-
webhooks: [WebhookSchema],
|
|
35
|
+
webhooks: {type:[WebhookSchema], validate: helpers.validateNotNull},
|
|
36
36
|
configurationParameters: {
|
|
37
37
|
iosConfig: iosConfig, // when we want to enable this {type: iosConfig, required: true}
|
|
38
38
|
androidConfig: androidConfig, // when we want to enable this {type: androidConfig, required: true}
|
|
39
39
|
}, // http://collab.bluedot.io/display/PD/Remote+configuration => https://bluedotinnovation.atlassian.net/wiki/spaces/ENG/pages/223150126/Remote+Config+details
|
|
40
|
-
firebaseApiKey: {type: String},
|
|
40
|
+
firebaseApiKey: {type: String, validate: helpers.validateNotNull},
|
|
41
41
|
apiKey: {type: String}
|
|
42
42
|
},
|
|
43
43
|
{collection: 'project'})
|
|
44
44
|
|
|
45
45
|
ProjectSchema.plugin(patcher)
|
|
46
46
|
|
|
47
|
+
ProjectSchema.pre('validate', function(next) {
|
|
48
|
+
Promise.all([
|
|
49
|
+
new Promise((resolve, reject) => helpers.checkArrayContainsValidValues(this, ['webhooks'], resolve)),
|
|
50
|
+
]).then(results => {
|
|
51
|
+
for( let result of results ) {
|
|
52
|
+
if( result )
|
|
53
|
+
return next(result)
|
|
54
|
+
}
|
|
55
|
+
next()
|
|
56
|
+
}).catch(console.log);
|
|
57
|
+
})
|
|
58
|
+
|
|
47
59
|
// optional mongoose
|
|
48
60
|
let model = (mongooseInstance) => {
|
|
49
61
|
let Project
|
package/models/User.js
CHANGED
|
@@ -28,13 +28,13 @@ let UserSchema = new mongoose.Schema({
|
|
|
28
28
|
},
|
|
29
29
|
firstName: {type: String, required: true},
|
|
30
30
|
lastName: {type: String, required: true},
|
|
31
|
-
phoneNumber: {type: String, maxlength: 20},
|
|
31
|
+
phoneNumber: {type: String, maxlength: 20, validate: helpers.validateNotNull},
|
|
32
32
|
state: {type: String, default: StateEnum.active},
|
|
33
33
|
creationTime: {type: Date, default: Date.now},
|
|
34
34
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
35
35
|
isAdmin: {type: Boolean, required: true},
|
|
36
36
|
secret: String,
|
|
37
|
-
projectPermissions: [ProjectPermission],
|
|
37
|
+
projectPermissions: {type: [ProjectPermission], validate: helpers.validateNotNull},
|
|
38
38
|
accountId: {
|
|
39
39
|
type: String,
|
|
40
40
|
required: true,
|
|
@@ -45,6 +45,18 @@ let UserSchema = new mongoose.Schema({
|
|
|
45
45
|
|
|
46
46
|
UserSchema.plugin(patcher)
|
|
47
47
|
|
|
48
|
+
UserSchema.pre('validate', function(next) {
|
|
49
|
+
Promise.all([
|
|
50
|
+
new Promise((resolve, reject) => helpers.checkArrayContainsValidValues(this, ['projectPermissions'], resolve)),
|
|
51
|
+
]).then(results => {
|
|
52
|
+
for( let result of results ) {
|
|
53
|
+
if( result )
|
|
54
|
+
return next(result)
|
|
55
|
+
}
|
|
56
|
+
next()
|
|
57
|
+
}).catch(console.log);
|
|
58
|
+
})
|
|
59
|
+
|
|
48
60
|
// optional mongoose
|
|
49
61
|
let model = (mongooseInstance) => {
|
|
50
62
|
let User
|
package/models/Zone.js
CHANGED
|
@@ -59,7 +59,7 @@ let zoneSchema = new Schema({
|
|
|
59
59
|
message: '{VALUE} is not a number'
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
|
-
tags: {type: [String]},
|
|
62
|
+
tags: {type: [String], validate: helpers.validateNotNull},
|
|
63
63
|
conditions: {
|
|
64
64
|
type: [conditionSchema]
|
|
65
65
|
},
|
|
@@ -70,11 +70,13 @@ let zoneSchema = new Schema({
|
|
|
70
70
|
type: [geofeatureSchema],
|
|
71
71
|
required: true
|
|
72
72
|
},
|
|
73
|
-
|
|
73
|
+
// To prevent saving `null` we use `required: true`
|
|
74
|
+
// https://github.com/Automattic/mongoose/issues/6034#issuecomment-362112268
|
|
75
|
+
triggerOnExit: {type: Boolean, default: false, required: true},
|
|
74
76
|
customData: {type: Schema.Types.Mixed, validate: customDataValidators},
|
|
75
77
|
creationTime: {type: Date, default: Date.now},
|
|
76
78
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
77
|
-
projectId: {type: String, required:
|
|
79
|
+
projectId: {type: String, required: true},
|
|
78
80
|
state: {type: String, default: StateEnum.active, select: false},
|
|
79
81
|
accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}}
|
|
80
82
|
}, {collection: 'zone'})
|
|
@@ -101,6 +103,7 @@ zoneSchema.path('geofeatures').discriminator('boundingBox', new Schema(boundingB
|
|
|
101
103
|
|
|
102
104
|
zoneSchema.pre('validate', function(next) {
|
|
103
105
|
Promise.all([
|
|
106
|
+
new Promise((resolve, reject) => helpers.checkArrayContainsValidValues(this, ['geofeatures', 'actions', 'conditions', 'tags'], resolve)),
|
|
104
107
|
new Promise((resolve, reject) => helpers.checkConditionsSuitableForBeacon(this, resolve)),
|
|
105
108
|
new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
|
|
106
109
|
new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve))
|
package/package.json
CHANGED