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
Support for checking empty JSON body. #374
base: master
Are you sure you want to change the base?
Conversation
Hi @chainhead, can you write more about your use case? Is there a reason this module should be doing this, and not some other middleware specifically for the purpose? One other small note, this PR also includes unrelated style changes, making it more difficult to tell what is being changed. Can you remove those? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your pull request. Besides the comments above from me and from @wesleytodd , the pull request also needs tests added and documentation updated.
@@ -104,12 +108,15 @@ function json (options) { | |||
|
|||
req.body = req.body || {} | |||
|
|||
// BEGIN - Check empty body | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this has been left commented out on accident -- this is how all the parsers work, they only parse bodies and this does not parse when there is no body at all (this is different from a zero-length body, i.e. empty string, which AFAIK is that you are adding an error for).
var index = str.indexOf(char) | ||
var partial = str.substring(0, index) + '#' | ||
|
||
try { | ||
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation') | ||
} catch (e) { | ||
return normalizeJsonSyntaxError(e, { | ||
message: e.message.replace('#', char), | ||
message: e.message.replace('#', char) + ' NS ', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this message here. NS
?
0ad1d88
to
2a2f471
Compare
Requirement
Let the JSON body parser package raise an error if the payload is empty. This middleware maybe added for certain
POST
orPUT
requests.Solution
Check for an empty string and raise a new error named as
createEmptyBodyError
which is similar tofunction createStrictSyntaxError
inparse
function atlib/tpyes/json.js
. This is done when the option of strict checking is set totrue
- the default.Notes
length
is not set correctly atlib/read.js
, ifContent-Length
is not passed in the request. However, adding a check forContent-Length
header may conflict prior installations.*options
value to test for empty/non-empty payloads may not be required.Content-Length
for this requirment.