@bluedot-innovation/models 2.0.20 → 2.0.21-alpha.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,3 +1,7 @@
1
+ ## 2.0.21
2
+
3
+ - Validation: Polygon needs 3 or more vertices
4
+
1
5
  ## 2.0.20
2
6
  - Added customData validation in helpers so that it can be imported in the UI
3
7
 
@@ -4,8 +4,8 @@ mongoose.Promise = global.Promise
4
4
 
5
5
  let PolygonModel = require('../../../Models').Polygon(mongoose)
6
6
  let PolygonModelBuiltInMongoose = require('../../../Models').Polygon()
7
- let testCase = require('mocha').describe
8
- let assertions = require('mocha').it
7
+ let describe = require('mocha').describe
8
+ let it = require('mocha').it
9
9
  let chai = require('chai')
10
10
 
11
11
  let assert = chai.assert
@@ -19,9 +19,10 @@ let polygon
19
19
  beforeEach(() => {
20
20
  polygon = JSON.parse(JSON.stringify(require('./polygon.data.json')))
21
21
  })
22
- testCase('Polygon', () => {
23
- testCase('Valid Polygon', () => {
24
- assertions('should validate a valid polygon ', (done) => {
22
+
23
+ describe('Polygon', () => {
24
+ describe('Valid Polygon', () => {
25
+ it('should validate a valid polygon ', (done) => {
25
26
  let Polygon = new PolygonModel(polygon)
26
27
  Polygon.validate((err, res) => {
27
28
  assert.notExists(res)
@@ -29,7 +30,7 @@ testCase('Polygon', () => {
29
30
  done()
30
31
  })
31
32
  })
32
- assertions('should validate a valid polygon (built in mongoose)', (done) => {
33
+ it('should validate a valid polygon (built in mongoose)', (done) => {
33
34
  let Polygon = new PolygonModelBuiltInMongoose(polygon)
34
35
  Polygon.validate((err, res) => {
35
36
  assert.notExists(res)
@@ -39,8 +40,8 @@ testCase('Polygon', () => {
39
40
  })
40
41
  })
41
42
 
42
- testCase('Invalid Polygon', () => {
43
- assertions('should not validate a polygon with invalid Polygon', (done) => {
43
+ describe('Invalid Polygon', () => {
44
+ it('should not validate a polygon with invalid Polygon', (done) => {
44
45
  delete polygon[rules.requiredFields[0]]
45
46
  let Polygon = new PolygonModel(polygon)
46
47
  Polygon.validate((err, res) => {
@@ -49,10 +50,11 @@ testCase('Polygon', () => {
49
50
  done()
50
51
  })
51
52
  })
52
- testCase('check each required property of the model and validate that it is present', () => {
53
+
54
+ describe('check each required property of the model and validate that it is present', () => {
53
55
  let requiredFields = rules.requiredFields
54
56
  requiredFields.map((i) => {
55
- assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
57
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
56
58
  // delete i from geofeatures' polygon child
57
59
  delete polygon[i]
58
60
  let Polygon = new PolygonModel(polygon)
@@ -66,5 +68,22 @@ testCase('Polygon', () => {
66
68
  })
67
69
  })
68
70
  })
71
+
72
+ it('when it only has less than 3 vertices, it should error', (done) => {
73
+ polygon.vertices = [{
74
+ "latitude": -37.81902680201739,
75
+ "longitude": 144.9821037054062
76
+ },
77
+ {
78
+ "latitude": -37.8196878692931,
79
+ "longitude": 144.98161554336548
80
+ }]
81
+
82
+ let Polygon = new PolygonModel(polygon)
83
+ Polygon.validate((err, res) => {
84
+ assert.match(err, /A polygon must have 3 or more vertices/i)
85
+ done()
86
+ })
87
+ })
69
88
  })
70
89
  })
package/helpers.js CHANGED
@@ -11,17 +11,38 @@ helpers.validateEmail = function (value) {
11
11
  return regex.test(value);
12
12
  }
13
13
 
14
+ helpers.validatePolygonVertices = (vertices) => {
15
+ return vertices.length >= 3;
16
+ }
17
+
14
18
  helpers.validateCustomData = function (data) {
15
19
  const customDataKeys = Object.keys(data)
16
20
  let hasValidationErrors = false
17
21
 
18
22
  customDataKeys.forEach(key => {
19
- const alphaNumericValidation = new RegExp(/^\w+$/)
23
+ const alphaNumericValidation = new RegExp(/^[\w\s]+$/)
20
24
  if(!alphaNumericValidation.test(key)){
21
25
  hasValidationErrors = true
22
26
  }
23
27
  })
24
28
 
29
+ const checkDuplicateKeys = [...new Set(customDataKeys)]
30
+ if(customDataKeys.length > checkDuplicateKeys.length) {
31
+ hasValidationErrors = true
32
+ }
33
+
34
+ return !hasValidationErrors
35
+ }
36
+
37
+ helpers.checkDuplicateKeys = function (data) {
38
+ const customDataKeys = Object.keys(data)
39
+ let hasValidationErrors = false
40
+
41
+ const duplicateKeys = [...new Set(customDataKeys)]
42
+ if(customDataKeys.length > duplicateKeys.length) {
43
+ hasValidationErrors = true
44
+ }
45
+
25
46
  return !hasValidationErrors
26
47
  }
27
48
 
package/models/Beacon.js CHANGED
@@ -25,8 +25,8 @@ let schema = {
25
25
  geometry: {type: PointGeometry},
26
26
  accountId: {type: String, required: true},
27
27
  // These must be unique
28
- major: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Major needs to be within range(1-65535)'}]},
29
- minor: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Minor needs to be within range(1-65535)'}]},
28
+ major: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Major needs to be within range(0-65535)'}]},
29
+ minor: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Minor needs to be within range(0-65535)'}]},
30
30
  txPower: {type: Number},
31
31
  macAddress: {type: String},
32
32
  proximityUUID: {type: String}
@@ -51,7 +51,7 @@ BeaconSchema.post('validate', function (doc) {
51
51
 
52
52
  function validateMajorMinor (v) {
53
53
  'use strict'
54
- return v >= 1 && v <= 65535
54
+ return v >= 0 && v <= 65535
55
55
  }
56
56
 
57
57
  function validateBeaconType (v, magicCallback) {
package/models/Polygon.js CHANGED
@@ -2,29 +2,32 @@ let mongoose = require('mongoose')
2
2
  let StateEnum = require('@bluedot-innovation/enums').StateEnum
3
3
  let Point = require('../schemas/Point')
4
4
  let uuidv4 = require('uuid/v4')
5
+ let helpers = require('../helpers')
5
6
 
6
7
  const schema = {
7
8
  _id: {type: String, default: uuidv4},
8
9
  geometry: {type: mongoose.Schema.Types.Mixed},
9
- name: {type: String, required: [true, 'name is required. ']},
10
+ name: {type: String, required: [true, 'name is required.']},
10
11
  color: String,
11
- vertices: {type: [Point], required: [true, 'vertices is required. ']},
12
+ vertices: {
13
+ type: [Point], required: [true, 'vertices is required.'],
14
+ validate: {
15
+ validator: helpers.validatePolygonVertices,
16
+ msg: 'A polygon must have 3 or more vertices.'
17
+ }
18
+ },
12
19
  creationTime: {type: Date, default: Date.now},
13
20
  lastUpdateTime: {type: Date, default: Date.now},
14
21
  state: {type: String, default: StateEnum.active, select: false}
15
22
  }
16
- const PolygonSchema = new mongoose.Schema(
17
- schema,
18
- {
19
- _id: false,
20
- versionKey: false
21
- })
22
23
 
23
- PolygonSchema.add({
24
- type:
25
- {type: String, required: true}
24
+ const PolygonSchema = new mongoose.Schema(schema, {
25
+ _id: false,
26
+ versionKey: false
26
27
  })
27
28
 
29
+ PolygonSchema.add({type: {type: String, required: true}})
30
+
28
31
  // optional mongoose
29
32
  let model = (mongooseInstance) => {
30
33
  let Polygon
package/models/Zone.js CHANGED
@@ -27,6 +27,11 @@ let helpers = require('../helpers')
27
27
  // Schema //
28
28
  // ////// //
29
29
 
30
+ let customDataValidators = [
31
+ {validator: helpers.validateCustomData, msg: 'CustomData key should have alpha numeric and _ characters only'},
32
+ {validator: helpers.checkDuplicateKeys, msg: 'CustomData key should be unique'},
33
+ ];
34
+
30
35
  let actionSchema = new Schema({},
31
36
  { discriminatorKey: 'type', _id: false })
32
37
 
@@ -61,7 +66,7 @@ let zoneSchema = new Schema({
61
66
  required: true
62
67
  },
63
68
  triggerOnExit: {type: Boolean, default: false},
64
- customData: {type: Schema.Types.Mixed},
69
+ customData: {type: Schema.Types.Mixed, validate: customDataValidators},
65
70
  creationTime: {type: Date, default: Date.now},
66
71
  lastUpdateTime: {type: Date, default: Date.now},
67
72
  projectId: {type: String, required: [true, 'projectId is required']},
@@ -87,8 +92,6 @@ zoneSchema.path('geofeatures').discriminator('polygon', new Schema(polygonSchema
87
92
  zoneSchema.path('geofeatures').discriminator('polyline', new Schema(polylineSchema))
88
93
  zoneSchema.path('geofeatures').discriminator('boundingBox', new Schema(boundingBoxSchema))
89
94
 
90
- zoneSchema.path('customData').validate(helpers.validateCustomData, 'CustomData key should have alpha numeric and _ characters only')
91
-
92
95
  // optional mongoose
93
96
  const model = (mongooseInstance) => {
94
97
  let Zone
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "2.0.20",
3
+ "version": "2.0.21-alpha.3",
4
4
  "description": "",
5
5
  "main": "Models.js",
6
6
  "scripts": {