@bluedot-innovation/models 5.1.16 → 5.1.17-alpha.2

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.17
2
+ - Add validation for `txPower` to Beacon collection
3
+
1
4
  ## 5.1.16
2
5
  - Added option field `superAdmin` to User collection
3
6
 
@@ -116,5 +116,27 @@ describe('Beacon', () => {
116
116
  })
117
117
  })
118
118
  })
119
+
120
+ describe('should fail validation if the txPower is not a number in the range from -100 to 0', () => {
121
+ const beaconTypes = ['Android', 'Both'];
122
+ const invalidTxPowerValues = ['dump_text', 10]
123
+
124
+ beaconTypes.forEach(beaconType => {
125
+ invalidTxPowerValues.forEach(value => {
126
+ it(`should throw an error with txPower with value: ${value} and beaconType: ${beaconType}`, (done) => {
127
+ let Beacon = new BeaconModel(beacon);
128
+ Beacon.beaconType = beaconType;
129
+ Beacon.txPower = value
130
+ Beacon.validate((err, res) => {
131
+ assert.notExists(res)
132
+ assert.exists(err, 'err object should exist')
133
+ assert.isObject(err)
134
+ assert.nestedProperty(err, 'errors.txPower' ,`error should have a txPower property`)
135
+ done()
136
+ })
137
+ })
138
+ })
139
+ })
140
+ })
119
141
  })
120
142
  })
@@ -85,6 +85,16 @@ describe('Zone', () => {
85
85
  done()
86
86
  })
87
87
  })
88
+
89
+ it('should set tags to lowercase and remove the duplicated tag', (done) => {
90
+ zone.tags = ["TAG", "TaG"]
91
+
92
+ let Zone = new ZoneModel(zone)
93
+ assert.exists(Zone._doc)
94
+ assert.exists(Zone._doc.tags)
95
+ assert.strictEqual(Zone._doc.tags[0], "tag")
96
+ done()
97
+ })
88
98
  })
89
99
 
90
100
  describe('Invalid Zone ', () => {
package/models/Beacon.js CHANGED
@@ -30,6 +30,13 @@ function isRequiredForIOS(beaconType){
30
30
  return beaconType.toLowerCase() === BeaconEnum.IOS.toLowerCase() || beaconType.toLowerCase() === BeaconEnum.BOTH.toLowerCase()
31
31
  }
32
32
 
33
+ function validateTxPower(beaconType, txPower) {
34
+ if(isRequiredForAndroid(beaconType)){
35
+ return txPower >= -100 && txPower <= 0
36
+ }
37
+ return true
38
+ }
39
+
33
40
 
34
41
  let schema = {
35
42
  _id: {type: String, default: uuidv4},
@@ -49,7 +56,7 @@ let schema = {
49
56
  // These must be unique
50
57
  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)']},
51
58
  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)']},
52
- txPower: {type: Number, validate: helpers.validateNotNull},
59
+ txPower: {type: Number, validate: [function() {return validateTxPower(this.beaconType, this.txPower)}, 'TxPower needs to be within range(-100 to 0)']},
53
60
  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']},
54
61
  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']}
55
62
  }
package/models/Zone.js CHANGED
@@ -37,6 +37,10 @@ let customDataValidators = [
37
37
  {validator: helpers.isValidCustomDataCount, msg: 'Only 5 custom data fields are allowed to be added per zone'}
38
38
  ];
39
39
 
40
+ const tagsFormation = (tags) => {
41
+ return [...new Set(tags.map(tag => typeof tag === 'string' ? tag.toLowerCase(): tag))]
42
+ }
43
+
40
44
  let actionSchema = new Schema({},
41
45
  { discriminatorKey: 'type', _id: false })
42
46
 
@@ -59,7 +63,7 @@ let zoneSchema = new Schema({
59
63
  message: '{VALUE} is not a number'
60
64
  }
61
65
  },
62
- tags: {type: [String], validate: helpers.validateNotNull},
66
+ tags: {type: [String], set : tagsFormation, validate: helpers.validateNotNull},
63
67
  conditions: {
64
68
  type: [conditionSchema]
65
69
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.16",
3
+ "version": "5.1.17-alpha.2",
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",