@bluedot-innovation/models 2.0.19 → 2.0.20-alpha.1

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,6 @@
1
+ ## 2.0.20
2
+ - Validation: Polygon needs 3 or more vertices
3
+
1
4
  ## 2.0.19
2
5
  - Added validations for keys in customData
3
6
 
@@ -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, /Must have more than 2 vertices/i)
85
+ done()
86
+ })
87
+ })
69
88
  })
70
89
  })
package/helpers.js CHANGED
@@ -11,4 +11,8 @@ 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
  module.exports = helpers
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "2.0.19",
3
+ "version": "2.0.20-alpha.1",
4
4
  "description": "",
5
5
  "main": "Models.js",
6
6
  "scripts": {