@bluedot-innovation/models 3.1.1 → 5.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,4 +1,8 @@
1
1
 
2
+ ## 4.0.0
3
+ - Webhooks schema no longer allow headers of more than 1 key- value pair
4
+ - Webhooks no longer require enabled field
5
+
2
6
  ## 3.1.1
3
7
  - Rename `Polyline` to `Geoline`
4
8
 
@@ -40,6 +40,17 @@ testCase('Project', () => {
40
40
  })
41
41
  })
42
42
  testCase('Invalid Project ', () => {
43
+ assertions('should not validate if webhook headers have more than 1 key value pair ', (done) => {
44
+
45
+ project.webhooks.headers.anotherKey = 'anotherValue'
46
+
47
+ let Project = new ProjectModel(project)
48
+ Project.validate((err, res) => {
49
+ assert.exists(err)
50
+ assert.match(err.message, /1 key <-> value pair/)
51
+ done()
52
+ })
53
+ })
43
54
  testCase('recursive property check', () => {
44
55
  let requiredFields = rules.requiredFields
45
56
  requiredFields.map((i) => {
@@ -17,9 +17,9 @@ beforeEach(() => {
17
17
  zone = JSON.parse(JSON.stringify(require('./zone.data.json')))
18
18
  // remove actions as it's not tested
19
19
  delete zone.actions
20
- // remove all but 1 geofeatures as geofeatures are required
20
+ // remove all but 2 geofeatures as geofeatures are required
21
21
  geofeatures = zone.geofeatures
22
- zone.geofeatures = zone.geofeatures.filter(x => x.type.match(/circle/i))
22
+ zone.geofeatures = zone.geofeatures.filter(x => (x.type.match(/circle/i) || x.type.match(/boundingBox/i)) )
23
23
  // remove customData
24
24
  delete zone.customData
25
25
  })
@@ -72,5 +72,23 @@ testCase('Zone - Condition', () => {
72
72
  done()
73
73
  })
74
74
  })
75
+ assertions('should throw an error when more than 1 travelpath condition is specified', (done) => {
76
+ const travelPath = {
77
+ type: "travelPath",
78
+ minimumGeofeaturesCrossed: 2,
79
+ timeOutSeconds: 60,
80
+ sequential: true
81
+ }
82
+ // push 2 travelpaths into the zone's conditions array
83
+ zone.conditions.push(travelPath)
84
+ zone.conditions.push(travelPath)
85
+
86
+ let Zone = new ZoneModel(zone)
87
+ Zone.validate((err, res) => {
88
+ assert(err instanceof Error, "It should throw an error when more htan 1 travelpath condition is specified")
89
+ assert(err.message.includes("Zone must not have more than 1 travel path condition"), "It should throw a descriptive error when more than 1 travel path condition is specified")
90
+ done()
91
+ })
92
+ })
75
93
  })
76
94
  })
package/helpers.js CHANGED
@@ -99,4 +99,19 @@ helpers.checkMinimumGeofeaturesValidForZone = function(zone, next) {
99
99
  next();
100
100
  }
101
101
 
102
+ helpers.validateTravelPathConditionCount = function(zone, next) {
103
+ if( !zone.conditions )
104
+ return next();
105
+ if( !zone.conditions || !zone.conditions.length )
106
+ return next(); // No conditions.
107
+
108
+ let numberOfTravelPathConditions = 0
109
+
110
+ for( let condition of zone.conditions ) {
111
+ if( condition.type === "travelPath" ) numberOfTravelPathConditions ++
112
+ if ( numberOfTravelPathConditions > 1 ) return next(new Error("Zone must not have more than 1 travel path condition"))
113
+ }
114
+ next();
115
+ }
116
+
102
117
  module.exports = helpers
package/models/Zone.js CHANGED
@@ -77,7 +77,9 @@ let zoneSchema = new Schema({
77
77
  accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}}
78
78
  }, {collection: 'zone'})
79
79
 
80
- // TODO: Test patcher and include for other schemas if required
80
+ // Mongoose-json-patch
81
+ // https://www.npmjs.com/package/mongoose-json-patch
82
+ // plugin for PATCH operations
81
83
  zoneSchema.plugin(patcher)
82
84
 
83
85
  zoneSchema.path('actions').discriminator('url', new Schema(urlActionSchema, {_id: false}))
@@ -98,7 +100,8 @@ zoneSchema.path('geofeatures').discriminator('boundingBox', new Schema(boundingB
98
100
  zoneSchema.pre('validate', function(next) {
99
101
  Promise.all([
100
102
  new Promise((resolve, reject) => helpers.checkConditionsSuitableForBeacon(this, resolve)),
101
- new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve))
103
+ new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
104
+ new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve))
102
105
  ]).then(results => {
103
106
  for( let result of results ) {
104
107
  if( result )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "3.1.1",
3
+ "version": "5.0.0",
4
4
  "main": "Models.js",
5
5
  "scripts": {
6
6
  "lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",
@@ -8,8 +8,7 @@ let typeEnum = {
8
8
  let WebhookSchema = new mongoose.Schema({
9
9
  type: {type: String, enum: typeEnum, required: [true, 'webhook type is required']}, // configurationChanged, trigger
10
10
  url: {type: String, required: [true, 'URL is required'], validate: {isAsync: false, validator: validateUrl, msg: 'Please enter a valid URL'}},
11
- enabled: {type: Boolean, required: [true, 'enabled is required']},
12
- headers: {type: mongoose.Schema.Types.Mixed}
11
+ headers: {type: mongoose.Schema.Types.Mixed, validate: { isAsync: false, validator: validateHeaderlength, msg: 'Header object can only have 1 key <-> value pair' }}
13
12
  }, {
14
13
  _id: false
15
14
  })
@@ -19,4 +18,10 @@ function validateUrl (url) {
19
18
  const regex = new RegExp(expression, 'i')
20
19
  return regex.test(url)
21
20
  }
21
+
22
+ // https://bluedotinnovation.atlassian.net/browse/BP-1500 -- Limiting headers to 1
23
+ function validateHeaderlength (headers) {
24
+ return Object.keys(headers).length <= 1
25
+ }
26
+
22
27
  module.exports = WebhookSchema