@bluedot-innovation/models 5.1.10 → 5.1.12

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
+ ## 5.1.11
2
+ - Remove reference to package name in TestScenario
3
+
1
4
  ## 5.1.10
2
5
  - Fix enum typos in TestDevice and TestScenario
3
6
 
@@ -42,12 +42,12 @@ describe('Beacon', () => {
42
42
  })
43
43
  })
44
44
 
45
- describe('check each required property of the model and validate that it is present', (done) => {
45
+ describe('check each dependent required property of the model and validate that it is present and in a valid format', () => {
46
46
  let dependentRequiredFields = rules.dependentRequiredFields
47
47
  dependentRequiredFields.forEach((parent) => {
48
48
  parent.pairings.forEach((i) => {
49
49
  i.requiredFields.forEach((field) => {
50
- it(`should throw an error with property '${parent.property}' if '${i.value}' is selected and '${field}' is invalid`, (done) => {
50
+ it(`should throw an error with property '${field}' if '${i.value}' is selected and '${field}' is not present`, (done) => {
51
51
  // delete beacon[field] i.e. txPower from beacon
52
52
  delete beacon[field]
53
53
  // set required dependent field property
@@ -57,7 +57,23 @@ describe('Beacon', () => {
57
57
  assert.notExists(res)
58
58
  assert.exists(err, 'err object should exist')
59
59
  assert.isObject(err)
60
- assert.nestedProperty(err, `errors.${parent.property}`, `error should have a ${parent.property} property`)
60
+ assert.nestedProperty(err, `errors.${field}`, `error should have a ${field} property`)
61
+ done()
62
+ })
63
+ })
64
+ })
65
+
66
+ i.requiredFields.forEach((field) => {
67
+ it(`should throw an error with property '${field}' if '${i.value}' is selected and '${field}' is not in a valid format`, (done) => {
68
+ beacon[field] = "dumpt text"
69
+ // set required dependent field property
70
+ beacon[parent.property] = i.value
71
+ let Beacon = new BeaconModel(beacon)
72
+ Beacon.validate((err, res) => {
73
+ assert.notExists(res)
74
+ assert.exists(err, 'err object should exist')
75
+ assert.isObject(err)
76
+ assert.nestedProperty(err, `errors.${field}`, `error should have a ${field} property`)
61
77
  done()
62
78
  })
63
79
  })
@@ -57,7 +57,7 @@ testCase('Zone - Action', () => {
57
57
  Zone.validate((err, res) => {
58
58
  assert.exists(err, 'err object should exist')
59
59
  assert.isObject(err)
60
- assert('geofeatures.0.beaconType' in err.errors)
60
+ assert('geofeatures.0.minor' in err.errors)
61
61
  done()
62
62
  })
63
63
  })
package/helpers.js CHANGED
@@ -168,6 +168,10 @@ helpers.validateNotNull = function(value) {
168
168
  return value !== null
169
169
  }
170
170
 
171
+ helpers.isValidMacAddress = function (value){
172
+ return /^([0-9a-f]{1,2}[\.:-]){5}([0-9a-f]{1,2})$/i.test(value);
173
+ }
174
+
171
175
  const isValidObject = data => {
172
176
  // data not null, not undefined, must be an object
173
177
  return !(data === null || typeof data === 'undefined'|| data.constructor !== Object)
package/models/Beacon.js CHANGED
@@ -6,6 +6,30 @@ const mongoose = require('mongoose')
6
6
  const uuidv4 = require('uuid/v4')
7
7
  const helpers = require('../helpers')
8
8
 
9
+ function validateMacAddress(beaconType, macAddress) {
10
+ if(isRequiredForAndroid(beaconType)){
11
+ return helpers.isValidMacAddress(macAddress)
12
+ }
13
+ return true
14
+ }
15
+
16
+
17
+ function isRequiredForAndroid(beaconType){
18
+ return beaconType.toLowerCase() === BeaconEnum.ANDROID.toLowerCase() || beaconType.toLowerCase() === BeaconEnum.BOTH.toLowerCase()
19
+ }
20
+
21
+ function validateProximityUUID(beaconType, proximityUUID) {
22
+ if(isRequiredForIOS(beaconType)){
23
+ return helpers.validateUUIDv4(proximityUUID)
24
+ }
25
+ return true
26
+ }
27
+
28
+ function isRequiredForIOS(beaconType){
29
+ return beaconType.toLowerCase() === BeaconEnum.IOS.toLowerCase() || beaconType.toLowerCase() === BeaconEnum.BOTH.toLowerCase()
30
+ }
31
+
32
+
9
33
  let schema = {
10
34
  _id: {type: String, default: uuidv4},
11
35
  name: {type: String, required: [true, 'name is required.']},
@@ -16,22 +40,17 @@ let schema = {
16
40
  beaconType: {
17
41
  type: String,
18
42
  required: true,
19
- enum: [BeaconEnum.IOS, BeaconEnum.ANDROID, BeaconEnum.BOTH],
20
- validate: {
21
- isAsync: true,
22
- validator: validateBeaconType,
23
- msg: 'Async validation. This message is not used'
24
- }
43
+ enum: [BeaconEnum.IOS, BeaconEnum.ANDROID, BeaconEnum.BOTH]
25
44
  },
26
45
  location: {type: Point, required: true},
27
46
  geometry: {type: PointGeometry},
28
47
  accountId: {type: String, required: true},
29
48
  // These must be unique
30
- major: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Major needs to be within range(0-65535)'}]},
31
- minor: {type: Number, validate: [{validator: validateMajorMinor, msg: 'Minor needs to be within range(0-65535)'}]},
49
+ major: {type: Number, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a major'], validate: [function() {return validateMajorMinor(this.beaconType, this.major)}, 'Major needs to be within range(0-65535)']},
50
+ minor: {type: Number, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a minor'], validate: [function() {return validateMajorMinor(this.beaconType, this.minor)}, 'Minor needs to be within range(0-65535)']},
32
51
  txPower: {type: Number, validate: helpers.validateNotNull},
33
- macAddress: {type: String, validate: helpers.validateNotNull},
34
- proximityUUID: {type: String, validate: helpers.validateNotNull}
52
+ macAddress: {type: String, required: [function() {return isRequiredForAndroid(this.beaconType)}, 'Please enter a Mac Address'], validate: [function () {return validateMacAddress(this.beaconType, this.macAddress)}, 'Please enter a valid Mac Address']},
53
+ proximityUUID: {type: String, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a Proximity UUID'], validate: [function () {return validateProximityUUID(this.beaconType, this.proximityUUID)}, 'Please enter a valid Proximity UUID']}
35
54
  }
36
55
 
37
56
  let BeaconSchema = new mongoose.Schema(schema, {collection: 'beacon', autoIndex: false})
@@ -49,29 +68,13 @@ BeaconSchema.post('validate', function (doc) {
49
68
  Object.assign(this.errors || {}, this.errors || {}, this.customErrors)
50
69
  })
51
70
 
52
- function validateMajorMinor (v) {
71
+ function validateMajorMinor (beaconType, value) {
53
72
  'use strict'
54
- return v >= 0 && v <= 65535
55
- }
56
73
 
57
- function validateBeaconType (v, callback) {
58
- 'use strict'
59
- const beaconType = v
60
- const { minor, major, macAddress, proximityUUID } = this;
61
-
62
- let androidRegex = new RegExp(/(android)/ig)
63
- let iosRegex = new RegExp(/(ios)/ig)
64
- let bothRegex = new RegExp(/(both)/ig)
65
-
66
- if (androidRegex.test(beaconType)) {
67
- callback((!!macAddress), "Beacon type 'Android' must have a mac address")
68
- } else if (iosRegex.test(beaconType)) {
69
- callback((!!proximityUUID && !!minor && !!major), "Beacon type 'IOS' must have 'proximityUUID', 'minor' and 'major' values")
70
- } else if (bothRegex.test(beaconType)) {
71
- callback((!!proximityUUID && !!minor && !!major && !!macAddress), "Beacon type 'Both' must have all of 'proximityUUID', 'minor', 'major', and 'mac address' values")
72
- } else {
73
- callback(true)
74
+ if(isRequiredForIOS(beaconType)){
75
+ return value >= 0 && value <= 65535
74
76
  }
77
+ return true
75
78
  }
76
79
 
77
80
  // optional mongoose
@@ -12,7 +12,6 @@ let TestScenario = mongoose.model('TestScenario', new mongoose.Schema(
12
12
  apiKey: String,
13
13
  customerApiKey: String,
14
14
  email: String,
15
- package: String,
16
15
  'creationTime': {type: Date, default: Date.now},
17
16
  'lastUpdateTime': {type: Date, default: Date.now},
18
17
  state: {type: String, default: StateEnum.ACTIVE},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.10",
3
+ "version": "5.1.12",
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",