Skip to content

Commit

Permalink
allow author field to be "" (string of length 0)
Browse files Browse the repository at this point in the history
Signed-off-by: Vu Van Dung <joulev.vvd@yahoo.com>
  • Loading branch information
joulev committed Jul 22, 2022
1 parent dd993cd commit 72161ae
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 12 deletions.
5 changes: 0 additions & 5 deletions __tests__/misc/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ describe("Test comment validation", () => {
expect(COMMENT.textIsValid("a")).toBe(true);
expect(COMMENT.textIsValid("a b")).toBe(true);
});
it("Check authorIsValid", () => {
expect(COMMENT.authorIsValid("")).toBe(false);
expect(COMMENT.authorIsValid("a")).toBe(true);
expect(COMMENT.authorIsValid("a b")).toBe(true);
});
it("Check pageIdIsValid", () => {
expect(COMMENT.pageIdIsValid("")).toBe(false);
expect(COMMENT.pageIdIsValid("a")).toBe(true);
Expand Down
1 change: 0 additions & 1 deletion misc/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const PAGE = {

export const COMMENT = {
textIsValid: (content: string) => content.length > 0,
authorIsValid: (author: string) => author.length > 0,
// server-only
pageIdIsValid: idIsValid,
};
8 changes: 2 additions & 6 deletions server/middlewares/sanitizeRequests/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export const sanitizeCreateCommentRequest: ApiMiddleware = (req, _, next) => {
if (typeof text !== "string" || !COMMENT.textIsValid(text)) {
throw new CustomApiError("'text' must not be a non-empty string");
}
if (
author !== undefined &&
author !== null &&
(typeof author !== "string" || !COMMENT.authorIsValid(author))
) {
if (author !== undefined && author !== null && typeof author !== "string") {
throw new CustomApiError(
"'author' must be a non-empty string, undefined or null. If undefined, 'author' will be casted to null."
);
Expand All @@ -27,7 +23,7 @@ export const sanitizeCreateCommentRequest: ApiMiddleware = (req, _, next) => {
if (typeof pageId !== "string" || !COMMENT.pageIdIsValid(pageId)) {
throw new CustomApiError("'pageId' must be a non-empty string");
}
req.body = { author: author ?? null, text, pageId };
req.body = { author: author && (author as string).length > 0 ? author : null, text, pageId };
next();
};

Expand Down

0 comments on commit 72161ae

Please sign in to comment.