Skip to content

Commit

Permalink
Step 4.18: Add validations to 'addMessage()' method in api
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha authored and DAB0mB committed Dec 24, 2016
1 parent 896e49b commit 572daf0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion api/server/methods.ts
@@ -1,9 +1,18 @@
import { Meteor } from 'meteor/meteor';
import {Chats, Messages} from "../collections/whatsapp-collections";
import { Chats, Messages } from "../collections/whatsapp-collections";
import { check, Match } from "meteor/check";

const nonEmptyString = Match.Where((str) => {
check(str, String);
return str.length > 0;
});

export function initMethods() {
Meteor.methods({
addMessage(chatId: string, content: string) {
check(chatId, nonEmptyString);
check(content, nonEmptyString);

const chatExists = !!Chats.collection.find(chatId).count();

if (!chatExists) throw new Meteor.Error('chat-not-exists',
Expand Down

0 comments on commit 572daf0

Please sign in to comment.