Skip to content

Commit c8b57a5

Browse files
committed
fixed moodJournal validator
1 parent 11e2ffe commit c8b57a5

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

server/test/moodJournalController.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,37 @@ const moodJournal = {
2121
id: 1,
2222
uid: 'testuid',
2323
howAreYou: 'Good',
24-
stressSignals: { tired: 'rarely' , sleep:'rarely'},
24+
stressSignals: {
25+
tired: "rarely",
26+
sleep: "sometimes",
27+
hunger: "sometimes",
28+
overeating: "rarely",
29+
depressed: "often",
30+
pressure: "rarely",
31+
anxiety: "rarely",
32+
attention: "never",
33+
anger: "never",
34+
headache: "sometimes"
35+
},
2536
date: '2023-10-08T10:00:00Z',
2637
notes: 'Sample mood entry',
2738
};
2839

2940
const updateMoodJournal = {
3041
uid: 'testuid',
3142
howAreYou: 'Horrible',
32-
stressSignals: { tired: 'rarely' , sleep:'rarely'},
43+
stressSignals: {
44+
tired: "rarely",
45+
sleep: "sometimes",
46+
hunger: "sometimes",
47+
overeating: "rarely",
48+
depressed: "often",
49+
pressure: "rarely",
50+
anxiety: "rarely",
51+
attention: "never",
52+
anger: "never",
53+
headache: "sometimes"
54+
},
3355
date: '2023-10-08T10:00:00Z',
3456
notes: 'Updated Sample mood entry',
3557
};

server/utils/databaseValidators.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@ const moodJournalValidator = (values: { howAreYou: string; stressSignals: stress
164164
Logger.error(`Invalid howAreYou: ${howAreYou}`);
165165
throw new Error(`Invalid howAreYou: ${howAreYou}`);
166166
}
167-
//check if stressSignals is valid
168-
if(!stressSignals || typeof stressSignals !== 'object') {
169-
Logger.error(`Invalid stressSignals: ${stressSignals}`);
170-
throw new Error(`Invalid stressSignals: ${stressSignals}`);
167+
const requiredKeys = ['tired', 'sleep', 'hunger', 'overeating', 'depressed', 'pressure', 'anxiety', 'attention', 'anger', 'headache'];
168+
169+
if (!stressSignals || typeof stressSignals !== 'object' || !requiredKeys.every(key => key in stressSignals)) {
170+
Logger.error(`Invalid stressSignals: ${JSON.stringify(stressSignals)}`);
171+
throw new Error(`Invalid stressSignals: ${JSON.stringify(stressSignals)}`);
171172
}
172173
//check if date is valid
173174
if(!date || typeof date !== 'string') {

0 commit comments

Comments
 (0)