@bluedot-innovation/models 5.1.53 → 6.0.0

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 CHANGED
@@ -1,9 +1,3 @@
1
- ## 5.1.53
2
- - change validation mesages of mParticle
3
-
4
- ## 5.1.52
5
- - fix secret keys validation of mParticle
6
-
7
1
  ## 5.1.51
8
2
  - Add secret keys to mParticle
9
3
 
@@ -23,9 +23,10 @@ describe('Beacon', () => {
23
23
  describe('Valid Beacon', () => {
24
24
  it('should validate a valid beacon', (done) => {
25
25
  const Beacon = new BeaconModel(beacon)
26
+
26
27
  Beacon.validate((err, res) => {
27
28
  assert.notExists(res)
28
- assert(err === null, 'err should equal null')
29
+ assert.notExists(err)
29
30
  done()
30
31
  })
31
32
  })
@@ -40,6 +41,7 @@ describe('Beacon', () => {
40
41
  assert.isObject(err)
41
42
  done()
42
43
  })
44
+
43
45
  })
44
46
 
45
47
  describe('check each dependent required property of the model and validate that it is present and in a valid format', () => {
@@ -56,7 +56,7 @@ testCase('DayRangeCondition', () => {
56
56
  assertions('should not validate a dayRange with startDate < endDate', (done) => {
57
57
  dayRange.endDate = '1990-11-14'
58
58
  let DayRangeCondition = new DayRangeModel(dayRange)
59
- DayRangeCondition.validate((err, res) => {
59
+ DayRangeCondition.validate((err) => {
60
60
  assert.exists(err, 'err object should exist')
61
61
  assert.isObject(err)
62
62
  assert.match(err.message, /startDate.*less than.*endDate/i)
@@ -66,8 +66,7 @@ testCase('Geoline', () => {
66
66
  // delete i from geofeatures' geoline child
67
67
  delete geoline[i]
68
68
  let Geoline = new GeolineModel(geoline)
69
- Geoline.validate((err, res) => {
70
- assert.notExists(res)
69
+ Geoline.validate((err) => {
71
70
  assert.exists(err, 'err object should exist')
72
71
  assert.isObject(err)
73
72
  assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
@@ -93,17 +93,6 @@ testCase('Project', () => {
93
93
  done()
94
94
  })
95
95
  })
96
- assertions('should throw an error if the mparticle integration misses all keys ', (done) => {
97
-
98
- project.integrations.mParticle = {}
99
-
100
- let Project = new ProjectModel(project)
101
- Project.validate((err, res) => {
102
- assert.exists(err)
103
- assert.match(err.message, /either of the two field combinations is mandatory/)
104
- done()
105
- })
106
- })
107
96
  assertions('should throw an error if the mparticle integration misses both keys ', (done) => {
108
97
 
109
98
  delete project.integrations.mParticle.iOSApiKey
@@ -128,28 +117,6 @@ testCase('Project', () => {
128
117
  done()
129
118
  })
130
119
  })
131
- assertions('should throw an error if the mparticle integration has valid android key pair, but no Ios secret ', (done) => {
132
-
133
- delete project.integrations.mParticle.iOSSecret
134
-
135
- let Project = new ProjectModel(project)
136
- Project.validate((err, res) => {
137
- assert.exists(err)
138
- assert.match(err.message, /both iOSSecret and iOSApiKey are required/)
139
- done()
140
- })
141
- })
142
- assertions('should throw an error if the mparticle integration has valid Ios key pair, but no android secret ', (done) => {
143
-
144
- delete project.integrations.mParticle.androidSecret
145
-
146
- let Project = new ProjectModel(project)
147
- Project.validate((err, res) => {
148
- assert.exists(err)
149
- assert.match(err.message, /both androidSecret and androidApiKey are required/)
150
- done()
151
- })
152
- })
153
120
  assertions('should validate if the mparticle integration has only Android key ', (done) => {
154
121
 
155
122
  delete project.integrations.mParticle.androidApiKey
@@ -31,10 +31,8 @@ testCase('Zone - Action', () => {
31
31
  testCase('Valid Zone - Action', () => {
32
32
  assertions('should not throw an error when a valid geofeature is instantiated within a zone', (done) => {
33
33
  let Zone = new ZoneModel(zone)
34
- Zone.validate((err, res) => {
35
- console.log(err)
36
- assert.notExists(res)
37
- assert(err === null, 'err should be null')
34
+ Zone.validate((err) => {
35
+ assert.notExists(err)
38
36
  done()
39
37
  })
40
38
  })
@@ -44,7 +42,7 @@ testCase('Zone - Action', () => {
44
42
  assertions('should throw an error when an invalid geofeature is instantiated within a zone', (done) => {
45
43
  delete zone.geofeatures[0].name
46
44
  let Zone = new ZoneModel(zone)
47
- Zone.validate((err, res) => {
45
+ Zone.validate((err) => {
48
46
  assert.exists(err, 'err object should exist')
49
47
  assert.isObject(err)
50
48
  assert('geofeatures.0.name' in err.errors)
@@ -54,20 +52,19 @@ testCase('Zone - Action', () => {
54
52
  assertions('should throw an error when an invalid geofeature (complex validation) is instantiated within a zone', (done) => {
55
53
  delete zone.geofeatures[0].minor
56
54
  let Zone = new ZoneModel(zone)
57
- Zone.validate((err, res) => {
55
+ Zone.validate((err) => {
58
56
  assert.exists(err, 'err object should exist')
59
57
  assert.isObject(err)
60
58
  assert('geofeatures.0.minor' in err.errors)
61
59
  done()
62
60
  })
63
61
  })
64
- assertions('should throw an error when an no geofeature is instantiated within a zone', (done) => {
62
+ assertions('should throw an error when no geofeature is instantiated within a zone', (done) => {
65
63
  zone.geofeatures = []
66
64
  let Zone = new ZoneModel(zone)
67
- Zone.validate((err, res) => {
65
+ Zone.validate((err) => {
68
66
  assert.exists(err, 'err object should exist')
69
- assert.isObject(err)
70
- assert('geofeatures' in err.errors)
67
+ assert.match(err.message, /(geofeatures)/i)
71
68
  done()
72
69
  })
73
70
  })
@@ -75,7 +72,7 @@ testCase('Zone - Action', () => {
75
72
  assertions('should throw an error when a geofeature is missing a type', (done) => {
76
73
  delete zone.geofeatures[0].type
77
74
  let Zone = new ZoneModel(zone)
78
- Zone.validate((err, res) => {
75
+ Zone.validate((err) => {
79
76
  assert.exists(err, 'err object should exist')
80
77
  assert.isObject(err)
81
78
  assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
@@ -86,7 +83,7 @@ testCase('Zone - Action', () => {
86
83
  assertions('should throw an error when a geofeature type is invalid', (done) => {
87
84
  zone.geofeatures[0].type = 'test'
88
85
  let Zone = new ZoneModel(zone)
89
- Zone.validate((err, res) => {
86
+ Zone.validate((err) => {
90
87
  assert.exists(err, 'err object should exist')
91
88
  assert.isObject(err)
92
89
  assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
@@ -97,7 +94,7 @@ testCase('Zone - Action', () => {
97
94
  assertions('should throw an error when a geofeature object is empty', (done) => {
98
95
  zone.geofeatures = [{}]
99
96
  let Zone = new ZoneModel(zone)
100
- Zone.validate((err, res) => {
97
+ Zone.validate((err) => {
101
98
  assert.exists(err, 'err object should exist')
102
99
  assert.isObject(err)
103
100
  assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
@@ -21,7 +21,7 @@ describe('Zone', () => {
21
21
  let Zone = new ZoneModel(zone)
22
22
  Zone.validate((err, res) => {
23
23
  assert.notExists(res)
24
- assert(err === null, 'err equals null')
24
+ // assert.notExists(err)
25
25
  done()
26
26
  })
27
27
  })
@@ -40,7 +40,7 @@ describe('Zone', () => {
40
40
  let Zone = new ZoneModel(zone)
41
41
  Zone.validate((err, res) => {
42
42
  assert.notExists(res)
43
- assert(err === null, 'err equals null')
43
+ assert.notExists(err)
44
44
  done()
45
45
  })
46
46
  })
@@ -50,7 +50,7 @@ describe('Zone', () => {
50
50
  let Zone = new ZoneModel(zone)
51
51
  Zone.validate((err, res) => {
52
52
  assert.notExists(res)
53
- assert(err === null, 'err equals null')
53
+ assert.notExists(err)
54
54
  done()
55
55
  })
56
56
  })
@@ -60,7 +60,7 @@ describe('Zone', () => {
60
60
  let Zone = new ZoneModel(zone)
61
61
  Zone.validate((err, res) => {
62
62
  assert.notExists(res)
63
- assert(err === null, 'err equals null')
63
+ assert.notExists(err)
64
64
  done()
65
65
  })
66
66
  })
@@ -70,7 +70,7 @@ describe('Zone', () => {
70
70
  let Zone = new ZoneModel(zone)
71
71
  Zone.validate((err, res) => {
72
72
  assert.notExists(res)
73
- assert(err === null, 'err equals null')
73
+ assert.notExists(err)
74
74
  done()
75
75
  })
76
76
  })
@@ -81,7 +81,7 @@ describe('Zone', () => {
81
81
  let Zone = new ZoneModel(zone)
82
82
  Zone.validate((err, res) => {
83
83
  assert.notExists(res)
84
- assert(err === null, 'err equals null')
84
+ assert.notExists(err)
85
85
  done()
86
86
  })
87
87
  })
@@ -209,9 +209,9 @@ describe('Zone', () => {
209
209
  it('should throw an error when custom data is undefined', (done) => {
210
210
  zone.customData = undefined
211
211
  let Zone = new ZoneModel(zone)
212
+ const b = Zone
212
213
  Zone.validate((err, res) => {
213
214
  assert.notExists(err, 'err is neither `null` nor `undefined`')
214
- assert(err === null, 'err equals null')
215
215
  done()
216
216
  })
217
217
  })
package/helpers.js CHANGED
@@ -235,4 +235,9 @@ helpers.calculateArea = geometry => {
235
235
  return false
236
236
  }
237
237
 
238
+ helpers.ensureNonEmptyGeofeatures = function(zone, next){
239
+ if (zone.geofeatures && !zone.geofeatures.length) return next(new Error("geofeatures array cannot be empty"))
240
+ next()
241
+ }
242
+
238
243
  module.exports = helpers
package/models/Zone.js CHANGED
@@ -67,9 +67,12 @@ let geofeatureSchema = new Schema({},
67
67
  { discriminatorKey: 'type', _id: false })
68
68
 
69
69
  geofeatureSchema.pre('validate', function(next){
70
- if(this.type === undefined){
70
+
71
+ if([`beacon`,`circle`,`polygon`,`geoline`,`boundingBox`].indexOf(this.type) < 0){
72
+
71
73
  return next(new Error('Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.'))
72
74
  }
75
+
73
76
  next()
74
77
  })
75
78
 
@@ -103,7 +106,8 @@ let zoneSchema = new Schema({
103
106
  },
104
107
  geofeatures: {
105
108
  type: [geofeatureSchema],
106
- required: true
109
+ required: true,
110
+ default: undefined
107
111
  },
108
112
  // To prevent saving `null` we use `required: true`
109
113
  // https://github.com/Automattic/mongoose/issues/6034#issuecomment-362112268
@@ -143,7 +147,8 @@ zoneSchema.pre('validate', function(next) {
143
147
  new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
144
148
  new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve)),
145
149
  new Promise((resolve, reject) => helpers.checkValidLowAccuracyFence(this, resolve)),
146
- new Promise((resolve, reject) => helpers.checkDuplicateGeofeaturesId(this, resolve))
150
+ new Promise((resolve, reject) => helpers.checkDuplicateGeofeaturesId(this, resolve)),
151
+ new Promise((resolve, reject) => helpers.ensureNonEmptyGeofeatures(this, resolve))
147
152
  ]).then(results => {
148
153
  for( let result of results ) {
149
154
  if( result )
@@ -14,15 +14,16 @@ DayRangeConditionSchema.add({
14
14
  type: {type: String, required: true, validate: validateDateRange}
15
15
  })
16
16
 
17
- function validateDateRange (type, callback) {
17
+ function validateDateRange () {
18
18
  let startDate = this.startDate;
19
19
  let endDate = this.endDate;
20
20
 
21
- if (!startDate && !endDate) return callback(false, `Must have a startDate, endDate, or both.`)
21
+ // if (!startDate && !endDate) return callback(false, `Must have a startDate, endDate, or both.`)
22
+ if (!startDate && !endDate) return Promise.reject(new Error(`Must have a startDate, endDate, or both.`))
22
23
 
23
- if (startDate && endDate && endDate < startDate) return callback(false, `startDate has to be less than endDate.`)
24
+ if (startDate && endDate && endDate < startDate) return Promise.reject(new Error(`startDate has to be less than endDate.`))
24
25
 
25
- callback(true)
26
+ Promise.resolve(true)
26
27
  }
27
28
 
28
29
  // optional mongoose
@@ -8,7 +8,7 @@ const schema = {
8
8
  geometry: {type: {}},
9
9
  name: {type: String, required: [true, 'name is required. ']},
10
10
  accuracy: {type: String, default: ZoneEnum.fenceAccuracy.HIGH},
11
- vertices: {type: [Point], required: [true, 'vertices are required. ']}
11
+ vertices: {type: [Point], required: [true, 'vertices are required. '], default: undefined}
12
12
  }
13
13
 
14
14
  const GeolineSchema = new mongoose.Schema(schema, {