@bluedot-innovation/models 8.4.1 → 8.4.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
+ ## 8.4.2
2
+ - Validate Hello report recipient emails
3
+
1
4
  ## 8.4.1
2
5
  - Add reportConfigurations to Project model
3
6
 
@@ -852,6 +852,16 @@ describe('Project', () => {
852
852
  done()
853
853
  })
854
854
  })
855
+
856
+ it('Should throw an error when recipients are invalid', (done) => {
857
+ project.reportConfigurations[0].recipients = "abc@example.com, invalid-1, invalid-2@example"
858
+ let Project = new ProjectModel(project)
859
+ Project.validate((err, res) => {
860
+ assert.exists(err)
861
+ assert.match(err.message, /Invalid email address\(es\): invalid-1,invalid-2@example/)
862
+ done()
863
+ })
864
+ })
855
865
  })
856
866
  })
857
867
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "8.4.1",
3
+ "version": "8.4.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",
@@ -1,5 +1,6 @@
1
1
  let mongoose = require('mongoose')
2
2
  const moment = require('moment-timezone')
3
+ const helpers = require('../helpers')
3
4
 
4
5
  function validateReportDay(reportDay) {
5
6
  if (this.frequency === 'weekly' && (!Number.isInteger(reportDay) || !(reportDay >= 0 && reportDay <= 6))) {
@@ -17,12 +18,27 @@ function validateTimeZone(timezone) {
17
18
  return true
18
19
  }
19
20
 
21
+ function validateRecipients(value) {
22
+ const invalidEmails = []
23
+ const recipients = value.split(',').map((email) => email.trim())
24
+ recipients.forEach((email) => {
25
+ if (!helpers.validateEmail(email)) {
26
+ invalidEmails.push(email)
27
+ }
28
+ });
29
+
30
+ if (invalidEmails.length) {
31
+ throw new Error(`Invalid email address(es): ${invalidEmails.join(",")}`)
32
+ }
33
+ return true
34
+ }
35
+
20
36
  let ReportConfigurationSchema = new mongoose.Schema({
21
37
  type: {type: String, enum: ['hello'], required: true},
22
38
  frequency: {type: String, enum: ['daily', 'weekly', 'monthly'], required: true},
23
39
  reportDay: {type: Number, validate: {validator: validateReportDay}},
24
40
  timezone: {type: String, required: true, validate: { validator: validateTimeZone, msg: 'Invalid timezone'}},
25
- recipients: {type: String, required: true}
41
+ recipients: {type: String, required: true, validate: {validator: validateRecipients}}
26
42
  }, {
27
43
  _id: false
28
44
  })