@bluedot-innovation/models 3.0.2-alpha.1 → 3.0.3

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,5 +1,11 @@
1
+ ## 3.0.3
2
+ - `BoundingBox` - disallow same values for `northEast` and `southWest`
3
+
4
+ ## 3.0.2
5
+ - Remove `_id`'s on the actions and filters (for real this time)
6
+
1
7
  ## 3.0.1
2
- - DayRange must have at least startTime or endTime or both
8
+ - `DayRange` must have at least startTime or endTime or both
3
9
 
4
10
  ## 3.0.0
5
11
 
@@ -3,8 +3,8 @@ let mongoose = require('mongoose')
3
3
  mongoose.Promise = global.Promise
4
4
 
5
5
  let BoundingBoxModel = require('../../../Models').BoundingBox(mongoose)
6
- let testCase = require('mocha').describe
7
- let assertions = require('mocha').it
6
+ let describe = require('mocha').describe
7
+ let it = require('mocha').it
8
8
  let chai = require('chai')
9
9
 
10
10
  let assert = chai.assert
@@ -19,9 +19,9 @@ beforeEach(() => {
19
19
  boundingBox = JSON.parse(JSON.stringify(require('./boundingBox.data.json')))
20
20
  })
21
21
 
22
- testCase('BoundingBox', () => {
23
- testCase('Valid BoundingBox', () => {
24
- assertions('should validate a valid boundingBox ', (done) => {
22
+ describe('BoundingBox', () => {
23
+ describe('Valid BoundingBox', () => {
24
+ it('should validate a valid boundingBox ', (done) => {
25
25
  let BoundingBox = new BoundingBoxModel(boundingBox)
26
26
  BoundingBox.validate((err, res) => {
27
27
  assert.notExists(res)
@@ -31,8 +31,8 @@ testCase('BoundingBox', () => {
31
31
  })
32
32
  })
33
33
 
34
- testCase('Invalid BoundingBox', () => {
35
- assertions('should not validate a boundingBox with invalid BoundingBox', (done) => {
34
+ describe('Invalid BoundingBox', () => {
35
+ it('should not validate a boundingBox with invalid BoundingBox', (done) => {
36
36
  delete boundingBox[rules.requiredFields[0]]
37
37
  let BoundingBox = new BoundingBoxModel(boundingBox)
38
38
  BoundingBox.validate((err, res) => {
@@ -41,10 +41,11 @@ testCase('BoundingBox', () => {
41
41
  done()
42
42
  })
43
43
  })
44
- testCase('check each required property of the model and validate that it is present', () => {
44
+
45
+ describe('check each required property of the model and validate that it is present', () => {
45
46
  let requiredFields = rules.requiredFields
46
47
  requiredFields.map((i) => {
47
- assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
48
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
48
49
  // delete i from geofeatures' boundingBox child
49
50
  delete boundingBox[i]
50
51
  let BoundingBox = new BoundingBoxModel(boundingBox)
@@ -58,5 +59,20 @@ testCase('BoundingBox', () => {
58
59
  })
59
60
  })
60
61
  })
62
+
63
+ it('should be invalid when southWest and NorthEast are the same', (done) => {
64
+ boundingBox.southWest = {
65
+ latitude: -37.81794619852986,
66
+ longitude: 144.97999548912048
67
+ }
68
+
69
+ const BoundingBox = new BoundingBoxModel(boundingBox)
70
+
71
+ BoundingBox.validate((err, res) => {
72
+ assert.exists(err, 'err object should exist')
73
+ assert.isObject(err)
74
+ done()
75
+ })
76
+ })
61
77
  })
62
78
  })
@@ -85,5 +85,27 @@ describe('Polygon', () => {
85
85
  done()
86
86
  })
87
87
  })
88
+
89
+ it('when 2 vertices in a row are the same, it should error', (done) => {
90
+ polygon.vertices = [{
91
+ "latitude": -37.81902680201739,
92
+ "longitude": 144.9821037054062
93
+ }, {
94
+ "latitude": -37.8196878692931,
95
+ "longitude": 144.9816155433654
96
+ }, {
97
+ "latitude": -37.8196878692931,
98
+ "longitude": 144.9816155433654
99
+ }, {
100
+ "latitude": -37.2396878692931,
101
+ "longitude": 144.1216155433654
102
+ }]
103
+
104
+ let Polygon = new PolygonModel(polygon)
105
+ Polygon.validate((err, res) => {
106
+ assert.match(err, /A polygon cannot have 2 vertices in a row the same/i)
107
+ done()
108
+ })
109
+ })
88
110
  })
89
111
  })
@@ -3,8 +3,8 @@ let mongoose = require('mongoose')
3
3
  mongoose.Promise = global.Promise
4
4
 
5
5
  let MonthlyStatsModel = require('../../Models').MonthlyStat(mongoose)
6
- let testCase = require('mocha').describe
7
- let assertions = require('mocha').it
6
+ let describe = require('mocha').describe
7
+ let it = require('mocha').it
8
8
  let chai = require('chai')
9
9
 
10
10
  let assert = chai.assert
@@ -17,9 +17,9 @@ beforeEach(() => {
17
17
  monthlyStats = JSON.parse(JSON.stringify(require('./monthlyStats.data.json')))
18
18
  })
19
19
 
20
- testCase('MonthlyStat', () => {
21
- testCase('Valid MonthlyStat document', () => {
22
- assertions('should validate a valid MonthlyStat document ', (done) => {
20
+ describe('MonthlyStat', () => {
21
+ describe('Valid MonthlyStat document', () => {
22
+ it('should validate a valid MonthlyStat document ', (done) => {
23
23
  let MonthlyStats = new MonthlyStatsModel(monthlyStats)
24
24
  MonthlyStats.validate((err, res) => {
25
25
  assert.notExists(res)
@@ -27,7 +27,7 @@ testCase('MonthlyStat', () => {
27
27
  done()
28
28
  })
29
29
  })
30
- assertions('should not accept a invalid customerId', (done) => {
30
+ it('should not accept a invalid customerId', (done) => {
31
31
  monthlyStats.customerId = 'invalid-customerId'
32
32
  let MonthlyStats = new MonthlyStatsModel(monthlyStats)
33
33
  MonthlyStats.validate((err, res) => {
@@ -36,7 +36,7 @@ testCase('MonthlyStat', () => {
36
36
  done()
37
37
  })
38
38
  })
39
- assertions('should not accept a invalid applicationId', (done) => {
39
+ it('should not accept a invalid applicationId', (done) => {
40
40
  const invalidAppId = {
41
41
  applicationId: 'invalid-applicationId',
42
42
  monthlyUsers: 0
@@ -44,13 +44,12 @@ testCase('MonthlyStat', () => {
44
44
  monthlyStats.applications.push(invalidAppId)
45
45
  let MonthlyStats = new MonthlyStatsModel(monthlyStats)
46
46
  MonthlyStats.validate((err, res) => {
47
- console.log(err)
48
47
  assert.exists(err, 'invalid customerId')
49
48
  assert.isObject(err)
50
49
  done()
51
50
  })
52
51
  })
53
- assertions('should not accept a negative monthlyUsers value', (done) => {
52
+ it('should not accept a negative monthlyUsers value', (done) => {
54
53
  const invalidAppId = {
55
54
  applicationId: 'd49fdf10-3744-462c-84f8-abad444d7044',
56
55
  monthlyUsers: -100
@@ -58,7 +57,6 @@ testCase('MonthlyStat', () => {
58
57
  monthlyStats.applications.push(invalidAppId)
59
58
  let MonthlyStats = new MonthlyStatsModel(monthlyStats)
60
59
  MonthlyStats.validate((err, res) => {
61
- console.log(err)
62
60
  assert.exists(err, 'Path `monthlyUsers` (-100) is less than minimum allowed value (0)')
63
61
  assert.isObject(err)
64
62
  done()
package/helpers.js CHANGED
@@ -1,5 +1,11 @@
1
+ var equal = require('deep-equal');
2
+
1
3
  let helpers = {}
2
4
 
5
+ const clean = (item) => {
6
+ return item && JSON.parse(JSON.stringify(item))
7
+ }
8
+
3
9
  helpers.validateUUIDv4 = function (id) {
4
10
  const expression = '^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'
5
11
  const regex = new RegExp(expression, 'i')
@@ -11,10 +17,26 @@ helpers.validateEmail = function (value) {
11
17
  return regex.test(value);
12
18
  }
13
19
 
14
- helpers.validatePolygonVertices = (vertices) => {
20
+ helpers.threeOrMoreVertices = function(vertices) {
15
21
  return vertices.length >= 3;
16
22
  }
17
23
 
24
+ helpers.notSameTwoInARow = function(list) {
25
+ const cleanList = clean(list)
26
+ let twoInARow = false;
27
+
28
+ for (var i = 0; i < cleanList.length; i++) {
29
+ const itemA = cleanList[i]
30
+ const itemB = cleanList[i - 1]
31
+ const isSame = equal(itemA, itemB);
32
+ if (isSame) {
33
+ twoInARow = true;
34
+ }
35
+ }
36
+
37
+ return !twoInARow;
38
+ }
39
+
18
40
  helpers.validateCustomData = function (data) {
19
41
  const customDataKeys = Object.keys(data)
20
42
  let hasValidationErrors = false
@@ -46,4 +68,16 @@ helpers.checkDuplicateKeys = function (data) {
46
68
  return !hasValidationErrors
47
69
  }
48
70
 
71
+ helpers.checkIsDifferent = function(propertyName) {
72
+ return function(propertyA) {
73
+ const propertyB = this[propertyName];
74
+
75
+ if (equal(clean(propertyA), clean(propertyB))) {
76
+ return false;
77
+ }
78
+
79
+ return true
80
+ }
81
+ }
82
+
49
83
  module.exports = helpers
@@ -3,14 +3,22 @@ let StateEnum = require('@bluedot-innovation/enums').StateEnum
3
3
  let uuidv4 = require('uuid/v4')
4
4
  let Point = require('../schemas/Point')
5
5
  let PolygonGeometry = require('../schemas/PolygonGeometry')
6
+ let helpers = require('../helpers')
6
7
 
7
8
  const schema = {
8
9
  _id: {type: String, default: uuidv4},
9
10
  geometry: {type: PolygonGeometry},
10
- name: {type: String, required: [true, 'name is required. ']},
11
+ name: {type: String, required: [true, 'name is required.']},
11
12
  color: String,
12
- northEast: {type: Point, required: [true, 'northEast is required. ']},
13
- southWest: {type: Point, required: [true, 'southWest is required. ']}
13
+ northEast: {type: Point, required: [true, 'northEast is required.']},
14
+ southWest: {
15
+ type: Point, required: [true, 'southWest is required.'],
16
+ validate: {
17
+ isAsync: true,
18
+ validator: helpers.checkIsDifferent('northEast'),
19
+ message: 'northEast and southWest cannot be the same'
20
+ }
21
+ }
14
22
  }
15
23
 
16
24
  const BoundingBoxSchema = new mongoose.Schema(schema, {
package/models/Polygon.js CHANGED
@@ -11,10 +11,14 @@ const schema = {
11
11
  color: String,
12
12
  vertices: {
13
13
  type: [Point], required: [true, 'vertices is required.'],
14
- validate: {
15
- validator: helpers.validatePolygonVertices,
14
+ validate: [{
15
+ validator: helpers.threeOrMoreVertices,
16
16
  msg: 'A polygon must have 3 or more vertices.'
17
- }
17
+ },
18
+ {
19
+ validator: helpers.notSameTwoInARow,
20
+ msg: 'A polygon cannot have 2 vertices in a row the same.'
21
+ }]
18
22
  }
19
23
  }
20
24
 
package/models/Zone.js CHANGED
@@ -80,8 +80,8 @@ let zoneSchema = new Schema({
80
80
  // TODO: Test patcher and include for other schemas if required
81
81
  zoneSchema.plugin(patcher)
82
82
 
83
- zoneSchema.path('actions').discriminator('url', new Schema(urlActionSchema))
84
- zoneSchema.path('actions').discriminator('message', new Schema(messageActionSchema))
83
+ zoneSchema.path('actions').discriminator('url', new Schema(urlActionSchema, {_id: false}))
84
+ zoneSchema.path('actions').discriminator('message', new Schema(messageActionSchema, {_id: false}))
85
85
 
86
86
  zoneSchema.path('filters').discriminator('dayRange', new Schema(dayRangeRawSchema, {_id: false}))
87
87
  zoneSchema.path('filters').discriminator('speed', new Schema(speedRawSchema, {_id: false}))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "3.0.2-alpha.1",
3
+ "version": "3.0.3",
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",
@@ -9,7 +9,6 @@
9
9
  "test:coverage-check": "nyc check-coverage --lines 90 --functions 90 --branches 70",
10
10
  "test:ci": "npm run lint && npm run test:coverage-check && npm test"
11
11
  },
12
- "author": "",
13
12
  "license": "UNLICENSED",
14
13
  "devDependencies": {
15
14
  "babel-eslint": "^8.2.3",
@@ -30,6 +29,8 @@
30
29
  },
31
30
  "dependencies": {
32
31
  "@bluedot-innovation/enums": "2.1.1",
32
+ "deep-equal": "^1.0.1",
33
+ "fast-deep-equal": "^2.0.1",
33
34
  "lodash.clonedeep": "4.5.0",
34
35
  "mongoose": "^4.13.9",
35
36
  "mongoose-error-helper": "0.0.2",