Skip to content

Commit 9f710fb

Browse files
committed
weight journal validator
1 parent c8b57a5 commit 9f710fb

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

server/controllers/weightJournalController.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request, Response } from 'express';
22
import { Logger } from '../middlewares/logger';
33
import db from '../models';
4+
import {weightJournalValidator} from '../utils/databaseValidators';
45

56
export const getWeightJournals = async (req: Request, res: Response) => {
67
try {
@@ -83,6 +84,7 @@ export const createWeightJournal = async (req: Request, res: Response) => {
8384
}
8485

8586
const { date, time, weight, height, unit, notes } = req.body;
87+
weightJournalValidator({ date, time, weight, height, unit, notes })
8688

8789
const weightJournal = await db.WeightJournal.create({
8890
uid: user.uid,
@@ -107,6 +109,7 @@ export const createWeightJournal = async (req: Request, res: Response) => {
107109
export const updateWeightJournal = async (req: Request, res: Response) => {
108110
try {
109111
const { date, time, weight, height, unit, notes } = req.body;
112+
weightJournalValidator({ date, time, weight, height, unit, notes })
110113

111114
const weightJournalId = req.params.weightJournalId;
112115

server/utils/databaseValidators.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,51 @@ const moodJournalValidator = (values: { howAreYou: string; stressSignals: stress
182182
}
183183
}
184184

185+
const weightJournalValidator = (values: { date: Date; time: Date; weight: number; height: number; unit: string; notes: string }) => {
186+
const {date, time, weight, height, unit, notes} = values;
187+
//check if date is valid
188+
if(!date || typeof date !== 'string') {
189+
Logger.error(`Invalid date: ${date}`);
190+
throw new Error(`Invalid date: ${date}`);
191+
}
192+
//check if time is valid
193+
if(!time || typeof time !== 'string') {
194+
Logger.error(`Invalid time: ${time}`);
195+
throw new Error(`Invalid time: ${time}`);
196+
}
197+
//check if weight is valid
198+
if(!weight || typeof weight !== 'number') {
199+
Logger.error(`Invalid weight: ${weight}`);
200+
throw new Error(`Invalid weight: ${weight}`);
201+
}
202+
//check if height is valid
203+
if(!height || typeof height !== 'number') {
204+
Logger.error(`Invalid height: ${height}`);
205+
throw new Error(`Invalid height: ${height}`);
206+
}
207+
//check if unit is valid
208+
if(!unit || /\d/.test(unit) || typeof unit !== 'string') {
209+
Logger.error(`Invalid unit: ${unit}`);
210+
throw new Error(`Invalid unit: ${unit}`);
211+
}
212+
//check if notes is valid
213+
if (notes !== undefined && (/\d/.test(notes) || typeof notes !== 'string')) {
214+
Logger.error(`Invalid notes: ${notes}`);
215+
throw new Error(`Invalid notes: ${notes}`);
216+
}
217+
}
218+
185219

186220

187221
export {
188222
userValidator,
189223
speedDialValidator,
190224
appointmentValidator,
191225
notificationPreferenceValidator,
192-
medicationValidator,
193-
moodJournalValidator
226+
medicationValidator,
227+
moodJournalValidator,
228+
weightJournalValidator
229+
194230

195231

196232
}

0 commit comments

Comments
 (0)