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

Conversation

joulev
Copy link
Owner

@joulev joulev commented Jul 22, 2022

I was play around with the comment pages and attempted to post some comments. However, anonymous comments cannot be submitted.

POST /api/comments

{
  "author": "",
  "text": "hello, world",
  "pageId": "anything"
}

fails because author doesn't pass the check

if (
author !== undefined &&
author !== null &&
(typeof author !== "string" || !COMMENT.authorIsValid(author))
) {
throw new CustomApiError(
"'author' must be a non-empty string, undefined or null. If undefined, 'author' will be casted to null."
);
}

In fact since the field is taken from an HTML input field, author is always a string – if the user doesn't fill in then it's "" like above.

This means the validation is buggy. This PR tries to fix it.

Signed-off-by: Vu Van Dung <joulev.vvd@yahoo.com>
@joulev joulev added bug Something isn't working backend All issues related to the backend labels Jul 22, 2022
@joulev joulev requested a review from VietAnh1010 as a code owner July 22, 2022 14:09
@joulev joulev self-assigned this Jul 22, 2022
@joulev
Copy link
Owner Author

joulev commented Jul 22, 2022

This blew my mind and I feel utterly embarrassed for not knowing it in advance.

image


This can be seen as a special case of the logical OR (||) operator, which returns the right-hand side operand if the left operand is any falsy value, not only null or undefined. In other words, if you use || to provide some default value to another variable foo, you may encounter unexpected behaviors if you consider some falsy values as usable (e.g., '' or 0). See below for more examples.

image

@codecov
Copy link

codecov bot commented Jul 22, 2022

Codecov Report

Merging #213 (94ab266) into main (dd993cd) will decrease coverage by 0.00%.
The diff coverage is n/a.

❗ Current head 94ab266 differs from pull request most recent head ffd6727. Consider uploading reports for the commit ffd6727 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #213      +/-   ##
==========================================
- Coverage   96.49%   96.49%   -0.01%     
==========================================
  Files          38       38              
  Lines         856      855       -1     
  Branches      174      174              
==========================================
- Hits          826      825       -1     
  Misses         27       27              
  Partials        3        3              
Impacted Files Coverage Δ
misc/validate.ts 100.00% <ø> (ø)

Signed-off-by: Vu Van Dung <joulev.vvd@yahoo.com>
@joulev joulev marked this pull request as ready for review July 22, 2022 14:17
@joulev joulev merged commit 725837a into main Jul 22, 2022
@joulev joulev deleted the allow-no-author-name branch July 22, 2022 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend All issues related to the backend bug Something isn't working
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants