Skip to content

Commit 92166dd

Browse files
committed
Check empty strings in post
1 parent 873d796 commit 92166dd

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

velog-backend/src/lib/common.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ export function generalHash(text: string) {
8787
.digest('hex');
8888
return hash;
8989
}
90+
91+
export function checkEmpty(text: string) {
92+
const replaced = text.trim().replace(/\u3164/g, '');
93+
if (replaced === '') return true;
94+
return false;
95+
}

velog-backend/src/router/posts/post/post.ctrl.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
generateSlugId,
99
escapeForUrl,
1010
extractKeys,
11+
checkEmpty,
1112
} from 'lib/common';
1213
import { diff } from 'json-diff';
1314
import {
@@ -106,6 +107,17 @@ export const updatePost = async (ctx: Context): Promise<*> => {
106107
meta,
107108
}: BodySchema = (ctx.request.body: any);
108109

110+
const stringsToCheck = [title, body, ...tags];
111+
for (let i = 0; i < stringsToCheck.length; i++) {
112+
if (checkEmpty(stringsToCheck[i])) {
113+
ctx.status = 400;
114+
ctx.body = {
115+
name: 'INVALID_TEXT',
116+
};
117+
return;
118+
}
119+
}
120+
109121
// validate tags
110122
if (tags) {
111123
for (let i = 0; i < tags.length; i++) {

velog-backend/src/router/posts/posts.ctrl.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isUUID,
1111
formatShortDescription,
1212
generalHash,
13+
checkEmpty,
1314
} from 'lib/common';
1415
import {
1516
Category,
@@ -192,6 +193,17 @@ export const writePost = async (ctx: Context): Promise<*> => {
192193
}
193194
}
194195

196+
const stringsToCheck = [title, body, ...tags];
197+
for (let i = 0; i < stringsToCheck.length; i++) {
198+
if (checkEmpty(stringsToCheck[i])) {
199+
ctx.status = 400;
200+
ctx.body = {
201+
name: 'INVALID_TEXT',
202+
};
203+
return;
204+
}
205+
}
206+
195207
if (processedSlug === '' || processedSlug.replace(/\./g, '') === '') {
196208
ctx.status = 400;
197209
ctx.body = {
@@ -283,7 +295,7 @@ export const readPost = async (ctx: Context): Promise<*> => {
283295
url_slug: urlSlug,
284296
fk_user_id: user.id,
285297
},
286-
order: ['created_at', 'DESC'],
298+
order: [['created_at', 'DESC']],
287299
});
288300
if (!history) {
289301
ctx.status = 404;

0 commit comments

Comments
 (0)