Skip to content

Commit aa3ae27

Browse files
committed
(validate) add messages format validation
1 parent e7e2bb0 commit aa3ae27

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/ChatWindow/MessagesList.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ import ChatMessage from './ChatMessage'
205205
import SvgIcon from './SvgIcon'
206206
import EmojiPicker from './EmojiPicker'
207207
import emojis from 'vue-emoji-picker/src/emojis'
208+
const { messagesValid } = require('../utils/roomValidation')
208209
209210
export default {
210211
name: 'messages-list',
@@ -283,6 +284,11 @@ export default {
283284
this.resetMessage()
284285
},
285286
messages(newVal, oldVal) {
287+
newVal.forEach(message => {
288+
if (!messagesValid(message))
289+
throw 'Messages object is not valid! Must contain _id[String, Number], content[String, Number] and sender_id[String, Number]'
290+
})
291+
286292
const element = this.$refs.scrollContainer
287293
if (!element) return
288294

src/utils/roomValidation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ export function partcipantsValid(obj) {
3737

3838
return validate(obj, participantsValidate)
3939
}
40+
41+
export function messagesValid(obj) {
42+
const participantsValidate = [
43+
{ key: '_id', type: ['string', 'number'] },
44+
{ key: 'content', type: ['string', 'number'] },
45+
{ key: 'sender_id', type: ['string', 'number'] }
46+
]
47+
48+
const validate = (obj, props) => {
49+
return props.every(prop => {
50+
const validType = prop.type.find(t => t === typeof obj[prop.key])
51+
return validType && obj.hasOwnProperty(prop.key) && obj[prop.key]
52+
})
53+
}
54+
55+
return validate(obj, participantsValidate)
56+
}

0 commit comments

Comments
 (0)