Skip to content

Commit fb51cfb

Browse files
committed
(refacto) data validation file
1 parent 70cfa00 commit fb51cfb

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/ChatWindow/ChatWindow.vue

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ import Room from './Room/Room'
7979
8080
import locales from '../locales'
8181
import { defaultThemeStyles, cssThemeVars } from '../themes'
82-
const { roomsValid, partcipantsValid } = require('../utils/room-validation')
82+
const {
83+
roomsValidation,
84+
partcipantsValidation
85+
} = require('../utils/data-validation')
8386
8487
export default {
8588
name: 'ChatContainer',
@@ -212,22 +215,12 @@ export default {
212215
},
213216
214217
room(val) {
215-
if (!val) return
218+
if (!val || Object.entries(val).length === 0) return
216219
217-
if (Object.entries(val).length === 0) return
218-
219-
if (!roomsValid(val)) {
220-
throw new Error(
221-
'Rooms object is not valid! Must contain roomId[String, Number], roomName[String] and users[Array]'
222-
)
223-
}
220+
roomsValidation(val)
224221
225222
val.users.forEach(user => {
226-
if (!partcipantsValid(user)) {
227-
throw new Error(
228-
'Participants object is not valid! Must contain _id[String, Number] and username[String]'
229-
)
230-
}
223+
partcipantsValidation(user)
231224
})
232225
},
233226

src/ChatWindow/Message/Message.vue

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ import MessageActions from './MessageActions'
189189
import MessageReactions from './MessageReactions'
190190
import AudioPlayer from './AudioPlayer'
191191
192-
const { messagesValid } = require('../../utils/room-validation')
192+
const { messagesValidation } = require('../../utils/data-validation')
193193
const {
194194
isImageFile,
195195
isVideoFile,
@@ -289,12 +289,7 @@ export default {
289289
},
290290
291291
mounted() {
292-
if (!messagesValid(this.message)) {
293-
throw new Error(
294-
'Messages object is not valid! Must contain _id[String, Number], content[String, Number] and senderId[String, Number]'
295-
)
296-
}
297-
292+
messagesValidation(this.message)
298293
this.$emit('message-added', { message: this.message, index: this.index })
299294
},
300295

src/utils/room-validation.js renamed to src/utils/data-validation.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function roomsValid(obj) {
1+
export function roomsValidation(obj) {
22
const roomsValidate = [
33
{ key: 'roomId', type: ['string', 'number'] },
44
{ key: 'roomName', type: ['string'] },
@@ -19,10 +19,14 @@ export function roomsValid(obj) {
1919
})
2020
}
2121

22-
return validate(obj, roomsValidate)
22+
if (!validate(obj, roomsValidate)) {
23+
throw new Error(
24+
'Rooms object is not valid! Must contain roomId[String, Number], roomName[String] and users[Array]'
25+
)
26+
}
2327
}
2428

25-
export function partcipantsValid(obj) {
29+
export function partcipantsValidation(obj) {
2630
const participantsValidate = [
2731
{ key: '_id', type: ['string', 'number'] },
2832
{ key: 'username', type: ['string'] }
@@ -35,11 +39,15 @@ export function partcipantsValid(obj) {
3539
})
3640
}
3741

38-
return validate(obj, participantsValidate)
42+
if (!validate(obj, participantsValidate)) {
43+
throw new Error(
44+
'Participants object is not valid! Must contain _id[String, Number] and username[String]'
45+
)
46+
}
3947
}
4048

41-
export function messagesValid(obj) {
42-
const participantsValidate = [
49+
export function messagesValidation(obj) {
50+
const messagesValidate = [
4351
{ key: '_id', type: ['string', 'number'] },
4452
{ key: 'content', type: ['string', 'number'] },
4553
{ key: 'senderId', type: ['string', 'number'] }
@@ -52,7 +60,11 @@ export function messagesValid(obj) {
5260
})
5361
}
5462

55-
return validate(obj, participantsValidate)
63+
if (!validate(obj, messagesValidate)) {
64+
throw new Error(
65+
'Messages object is not valid! Must contain _id[String, Number], content[String, Number] and senderId[String, Number]'
66+
)
67+
}
5668
}
5769

5870
function checkObjectValid(obj, key) {

0 commit comments

Comments
 (0)