Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow author field to be "" (string of length 0) #213

Merged
merged 3 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions __tests__/misc/validate.test.ts
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
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,
};
10 changes: 3 additions & 7 deletions server/middlewares/sanitizeRequests/comments.ts
Expand Up @@ -14,20 +14,16 @@ 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."
"'author' must be a string, undefined or null. If falsy, 'author' will be casted to null."
);
}
// Subject to change
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 || null, text, pageId };
next();
};

Expand Down