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

Support for checking empty JSON body. #374

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

chainhead
Copy link

Requirement

Let the JSON body parser package raise an error if the payload is empty. This middleware maybe added for certain POST or PUT requests.

Solution

Check for an empty string and raise a new error named as createEmptyBodyError which is similar to function createStrictSyntaxError in parse function at lib/tpyes/json.js. This is done when the option of strict checking is set to true - the default.

Notes

  1. When the body is empty, then length is not set correctly at lib/read.js, if Content-Length is not passed in the request. However, adding a check for Content-Length header may conflict prior installations.*
  2. For requests that rely on a payload, a 'strict' setting should mean a valid and a non-empty JSON payload. In other words, an extra options value to test for empty/non-empty payloads may not be required.
    • For my purpose, I have a middleware before JSON body parser that looks for multiple headers including Content-Length for this requirment.

@wesleytodd
Copy link
Member

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?

Copy link
Contributor

@dougwilson dougwilson left a 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
/*
Copy link
Contributor

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 ',
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants