@bluedot-innovation/models 5.1.4 → 5.1.6

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,8 @@
1
+ ## 5.1.5
2
+ - Added Test Management Portal models in Models.js
3
+
1
4
  ## 5.1.4
2
- - Remove ProjectPermissions required validation
5
+ - Removed ProjectPermissions required validation
3
6
 
4
7
  ## 5.1.3
5
8
  - Added bluedot's mongoose-json-patch
package/Models.js CHANGED
@@ -29,6 +29,12 @@ Models.TravelPath = require('./models/conditions/TravelPath').Model
29
29
  Models.Speed = require('./models/conditions/Speed').Model
30
30
  Models.TravelDirection = require('./models/conditions/TravelDirection').Model
31
31
 
32
+ // Test Management Portal
33
+ Models.TestDevice = require('./models/TestDevice')
34
+ Models.TestDeviceCluster = require('./models/TestDeviceCluster')
35
+ Models.TestInstance = require('./models/TestInstance')
36
+ Models.TestScenario = require('./models/TestScenario')
37
+
32
38
  let Schemas = {}
33
39
 
34
40
  Schemas.Account = require('./models/Account').Schema
@@ -107,12 +107,106 @@ describe('Zone', () => {
107
107
  })
108
108
  })
109
109
 
110
- it('should throw an error when validating an invalid customData key ', (done) => {
111
- zone.customData["name#3"] = "A Custom action3"
110
+ it('should throw an error when custom data is null', (done) => {
111
+ zone.customData = null
112
112
  let Zone = new ZoneModel(zone)
113
113
  Zone.validate((err, res) => {
114
114
  assert.exists(err, 'err object should exist')
115
115
  assert.isObject(err)
116
+ assert.match(err.message, /Zone validation failed: customData/i)
117
+ done()
118
+ })
119
+ })
120
+
121
+ it('should throw an error when custom data is undefined', (done) => {
122
+ zone.customData = undefined
123
+ let Zone = new ZoneModel(zone)
124
+ Zone.validate((err, res) => {
125
+ assert.notExists(err, 'err is neither `null` nor `undefined`')
126
+ assert(err === null, 'err equals null')
127
+ done()
128
+ })
129
+ })
130
+
131
+ it('should throw an error when custom data is not an object', (done) => {
132
+ zone.customData = 1
133
+ let Zone = new ZoneModel(zone)
134
+ Zone.validate((err, res) => {
135
+ assert.exists(err, 'err object should exist')
136
+ assert.isObject(err)
137
+ assert.match(err.message, /Zone validation failed: customData/i)
138
+ done()
139
+ })
140
+ })
141
+
142
+ it('should throw an error if CustomData key does not have alpha numeric and _ characters only ', (done) => {
143
+ zone.customData["name#3"] = "A Custom action"
144
+ let Zone = new ZoneModel(zone)
145
+ Zone.validate((err, res) => {
146
+ assert.match(err.message, /(CustomData key should have alpha numeric and _ characters only)/i);
147
+ done()
148
+ })
149
+ })
150
+
151
+ it('should throw an error if custom data keys count is greater than 5 ', (done) => {
152
+ zone.customData["name3"] = true;
153
+ zone.customData["name4"] = 1;
154
+ zone.customData["name5"] = "Action value 5";
155
+ zone.customData["name6"] = "Action Value 6";
156
+
157
+ let Zone = new ZoneModel(zone)
158
+ Zone.validate((err, res) => {
159
+ assert.match(err.message, /(Only 5 custom data fields are allowed to be added per zone)/i)
160
+ done()
161
+ })
162
+ })
163
+
164
+ it('should throw an error if CustomData value is null ', (done) => {
165
+ zone.customData["name3"] = null;
166
+
167
+ let Zone = new ZoneModel(zone)
168
+ Zone.validate((err, res) => {
169
+ assert.match(err.message, /(CustomData value should not be empty)/i);
170
+ done()
171
+ })
172
+ })
173
+
174
+ it('should throw an error if CustomData value is undefined ', (done) => {
175
+ zone.customData["name3"] = undefined;
176
+
177
+ let Zone = new ZoneModel(zone)
178
+ Zone.validate((err, res) => {
179
+ assert.match(err.message, /(CustomData value should not be empty)/i);
180
+ done()
181
+ })
182
+ })
183
+
184
+ it('should throw an error if CustomData value is an empty string ', (done) => {
185
+ zone.customData["name3"] = ' ';
186
+
187
+ let Zone = new ZoneModel(zone)
188
+ Zone.validate((err, res) => {
189
+ assert.match(err.message, /(CustomData value should not be empty)/i);
190
+ done()
191
+ })
192
+ })
193
+
194
+ it('should throw an error if CustomData value is an empty object ', (done) => {
195
+ zone.customData["name3"] = {};
196
+
197
+ let Zone = new ZoneModel(zone)
198
+ Zone.validate((err, res) => {
199
+ assert.match(err.message, /(CustomData value should not be empty)/i);
200
+ done()
201
+ })
202
+ })
203
+
204
+ it('should throw an error if CustomData value is an empty array ', (done) => {
205
+ zone.customData["name3"] = [];
206
+
207
+ let Zone = new ZoneModel(zone)
208
+ Zone.validate((err, res) => {
209
+ assert.match(err.message, /(CustomData value should not be empty)/i);
116
210
  done()
117
211
  })
118
212
  })
package/helpers.js CHANGED
@@ -19,34 +19,65 @@ helpers.threeOrMoreVertices = function(vertices) {
19
19
  }
20
20
 
21
21
  helpers.validateCustomData = function (data) {
22
- const customDataKeys = Object.keys(data)
23
- let hasValidationErrors = false
24
-
25
- customDataKeys.forEach(key => {
26
- const alphaNumericValidation = new RegExp(/^[\w\s]+$/)
27
- if(!alphaNumericValidation.test(key)){
28
- hasValidationErrors = true
29
- }
30
- })
31
22
 
32
- const checkDuplicateKeys = [...new Set(customDataKeys)]
33
- if(customDataKeys.length > checkDuplicateKeys.length) {
34
- hasValidationErrors = true
23
+ if(!isValidObject(data)){
24
+ return false
35
25
  }
36
26
 
37
- return !hasValidationErrors
27
+ const customDataKeys = Object.keys(data)
28
+ const alphaNumericValidation = new RegExp(/^[\w\s]+$/)
29
+ return customDataKeys.every(key => {
30
+ return alphaNumericValidation.test(key)
31
+ });
38
32
  }
39
33
 
40
34
  helpers.checkDuplicateKeys = function (data) {
41
- const customDataKeys = Object.keys(data)
42
- let hasValidationErrors = false
43
35
 
36
+ if(!isValidObject(data)){
37
+ return false
38
+ }
39
+
40
+ const customDataKeys = Object.keys(data);
44
41
  const duplicateKeys = [...new Set(customDataKeys)]
45
- if(customDataKeys.length > duplicateKeys.length) {
46
- hasValidationErrors = true
42
+ return !(customDataKeys.length > duplicateKeys.length)
43
+ }
44
+
45
+
46
+ /**
47
+ * helper function to validate valid custom data value is not empty
48
+ *
49
+ * @remarks
50
+ * ensures that data is an object
51
+ * ensures data.key is valid (`not null, not undefined, not {}, not [], not empty String`)
52
+ *
53
+ * @param data
54
+ * @returns boolean
55
+ */
56
+ helpers.isCustomDataValuesNotEmpty = function (data) {
57
+
58
+ if(!isValidObject(data)){
59
+ return false
47
60
  }
48
61
 
49
- return !hasValidationErrors
62
+ const customDataValues = Object.values(data)
63
+
64
+ return customDataValues.every(value => {
65
+ //not empty String
66
+ if(typeof value === 'string' && value.trim().length == 0) return false
67
+
68
+ // not null, not undefined, not {}, not []
69
+ return (value !== null && value !== undefined && JSON.stringify(value) !== '{}' && JSON.stringify(value) !== '[]')
70
+ })
71
+ }
72
+
73
+ helpers.isValidCustomDataCount = function (data) {
74
+
75
+ if(!isValidObject(data)){
76
+ return false
77
+ }
78
+ const customDataKeysSize = Object.keys(data).length;
79
+
80
+ return !(customDataKeysSize > 5)
50
81
  }
51
82
 
52
83
  helpers.checkIsDifferent = function(propertyName) {
@@ -114,4 +145,9 @@ helpers.validateTravelPathConditionCount = function(zone, next) {
114
145
  next();
115
146
  }
116
147
 
148
+ const isValidObject = data => {
149
+ // data not null, not undefined, must be an object
150
+ return !(data === null || typeof data === 'undefined'|| data.constructor !== Object)
151
+ }
152
+
117
153
  module.exports = helpers
package/models/Zone.js CHANGED
@@ -33,6 +33,8 @@ let helpers = require('../helpers')
33
33
  let customDataValidators = [
34
34
  {validator: helpers.validateCustomData, msg: 'CustomData key should have alpha numeric and _ characters only'},
35
35
  {validator: helpers.checkDuplicateKeys, msg: 'CustomData key should be unique'},
36
+ {validator: helpers.isCustomDataValuesNotEmpty, msg: 'CustomData value should not be empty'},
37
+ {validator: helpers.isValidCustomDataCount, msg: 'Only 5 custom data fields are allowed to be added per zone'}
36
38
  ];
37
39
 
38
40
  let actionSchema = new Schema({},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.4",
3
+ "version": "5.1.6",
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",