@bluedot-innovation/models 2.0.20-alpha.1 → 2.0.20
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 +1 -1
- package/__tests__/geofeature/polygon/polygon.spec.js +10 -29
- package/helpers.js +12 -2
- package/models/Polygon.js +11 -14
- package/models/Zone.js +1 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -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
|
|
8
|
-
let
|
|
7
|
+
let testCase = require('mocha').describe
|
|
8
|
+
let assertions = require('mocha').it
|
|
9
9
|
let chai = require('chai')
|
|
10
10
|
|
|
11
11
|
let assert = chai.assert
|
|
@@ -19,10 +19,9 @@ let polygon
|
|
|
19
19
|
beforeEach(() => {
|
|
20
20
|
polygon = JSON.parse(JSON.stringify(require('./polygon.data.json')))
|
|
21
21
|
})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
it('should validate a valid polygon ', (done) => {
|
|
22
|
+
testCase('Polygon', () => {
|
|
23
|
+
testCase('Valid Polygon', () => {
|
|
24
|
+
assertions('should validate a valid polygon ', (done) => {
|
|
26
25
|
let Polygon = new PolygonModel(polygon)
|
|
27
26
|
Polygon.validate((err, res) => {
|
|
28
27
|
assert.notExists(res)
|
|
@@ -30,7 +29,7 @@ describe('Polygon', () => {
|
|
|
30
29
|
done()
|
|
31
30
|
})
|
|
32
31
|
})
|
|
33
|
-
|
|
32
|
+
assertions('should validate a valid polygon (built in mongoose)', (done) => {
|
|
34
33
|
let Polygon = new PolygonModelBuiltInMongoose(polygon)
|
|
35
34
|
Polygon.validate((err, res) => {
|
|
36
35
|
assert.notExists(res)
|
|
@@ -40,8 +39,8 @@ describe('Polygon', () => {
|
|
|
40
39
|
})
|
|
41
40
|
})
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
testCase('Invalid Polygon', () => {
|
|
43
|
+
assertions('should not validate a polygon with invalid Polygon', (done) => {
|
|
45
44
|
delete polygon[rules.requiredFields[0]]
|
|
46
45
|
let Polygon = new PolygonModel(polygon)
|
|
47
46
|
Polygon.validate((err, res) => {
|
|
@@ -50,11 +49,10 @@ describe('Polygon', () => {
|
|
|
50
49
|
done()
|
|
51
50
|
})
|
|
52
51
|
})
|
|
53
|
-
|
|
54
|
-
describe('check each required property of the model and validate that it is present', () => {
|
|
52
|
+
testCase('check each required property of the model and validate that it is present', () => {
|
|
55
53
|
let requiredFields = rules.requiredFields
|
|
56
54
|
requiredFields.map((i) => {
|
|
57
|
-
|
|
55
|
+
assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
|
|
58
56
|
// delete i from geofeatures' polygon child
|
|
59
57
|
delete polygon[i]
|
|
60
58
|
let Polygon = new PolygonModel(polygon)
|
|
@@ -68,22 +66,5 @@ describe('Polygon', () => {
|
|
|
68
66
|
})
|
|
69
67
|
})
|
|
70
68
|
})
|
|
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
|
-
})
|
|
88
69
|
})
|
|
89
70
|
})
|
package/helpers.js
CHANGED
|
@@ -11,8 +11,18 @@ helpers.validateEmail = function (value) {
|
|
|
11
11
|
return regex.test(value);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
helpers.
|
|
15
|
-
|
|
14
|
+
helpers.validateCustomData = function (data) {
|
|
15
|
+
const customDataKeys = Object.keys(data)
|
|
16
|
+
let hasValidationErrors = false
|
|
17
|
+
|
|
18
|
+
customDataKeys.forEach(key => {
|
|
19
|
+
const alphaNumericValidation = new RegExp(/^\w+$/)
|
|
20
|
+
if(!alphaNumericValidation.test(key)){
|
|
21
|
+
hasValidationErrors = true
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return !hasValidationErrors
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
module.exports = helpers
|
package/models/Polygon.js
CHANGED
|
@@ -2,32 +2,29 @@ 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')
|
|
6
5
|
|
|
7
6
|
const schema = {
|
|
8
7
|
_id: {type: String, default: uuidv4},
|
|
9
8
|
geometry: {type: mongoose.Schema.Types.Mixed},
|
|
10
|
-
name: {type: String, required: [true, 'name is required.']},
|
|
9
|
+
name: {type: String, required: [true, 'name is required. ']},
|
|
11
10
|
color: String,
|
|
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
|
-
},
|
|
11
|
+
vertices: {type: [Point], required: [true, 'vertices is required. ']},
|
|
19
12
|
creationTime: {type: Date, default: Date.now},
|
|
20
13
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
21
14
|
state: {type: String, default: StateEnum.active, select: false}
|
|
22
15
|
}
|
|
16
|
+
const PolygonSchema = new mongoose.Schema(
|
|
17
|
+
schema,
|
|
18
|
+
{
|
|
19
|
+
_id: false,
|
|
20
|
+
versionKey: false
|
|
21
|
+
})
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
PolygonSchema.add({
|
|
24
|
+
type:
|
|
25
|
+
{type: String, required: true}
|
|
27
26
|
})
|
|
28
27
|
|
|
29
|
-
PolygonSchema.add({type: {type: String, required: true}})
|
|
30
|
-
|
|
31
28
|
// optional mongoose
|
|
32
29
|
let model = (mongooseInstance) => {
|
|
33
30
|
let Polygon
|
package/models/Zone.js
CHANGED
|
@@ -87,17 +87,7 @@ zoneSchema.path('geofeatures').discriminator('polygon', new Schema(polygonSchema
|
|
|
87
87
|
zoneSchema.path('geofeatures').discriminator('polyline', new Schema(polylineSchema))
|
|
88
88
|
zoneSchema.path('geofeatures').discriminator('boundingBox', new Schema(boundingBoxSchema))
|
|
89
89
|
|
|
90
|
-
zoneSchema.path('customData').validate(
|
|
91
|
-
const customDataKeys = Object.keys(data)
|
|
92
|
-
let hasValidationErrors = false
|
|
93
|
-
|
|
94
|
-
customDataKeys.forEach(key => {
|
|
95
|
-
const alphaNumericValidation = new RegExp(/^\w+$/)
|
|
96
|
-
hasValidationErrors = !alphaNumericValidation.test(key)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
return !hasValidationErrors
|
|
100
|
-
}, 'CustomData key should have alpha numeric and _ characters only')
|
|
90
|
+
zoneSchema.path('customData').validate(helpers.validateCustomData, 'CustomData key should have alpha numeric and _ characters only')
|
|
101
91
|
|
|
102
92
|
// optional mongoose
|
|
103
93
|
const model = (mongooseInstance) => {
|