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

Add post-parsing verify function #472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion lib/read.js
Expand Up @@ -50,6 +50,7 @@ function read (req, res, next, parse, debug, options) {
? opts.encoding
: null
var verify = opts.verify
var verifyParsed = opts.verifyParsed

try {
// get the content stream
Expand Down Expand Up @@ -125,7 +126,11 @@ function read (req, res, next, parse, debug, options) {
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
var parsedBody = parse(str)
if (verifyParsed) {
verifyParsed(req, res, parsedBody)
}
req.body = parsedBody
} catch (err) {
next(createError(400, err, {
body: str,
Expand Down
8 changes: 7 additions & 1 deletion lib/types/json.js
Expand Up @@ -58,11 +58,16 @@ function json (options) {
var strict = opts.strict !== false
var type = opts.type || 'application/json'
var verify = opts.verify || false
var verifyParsed = opts.verifyParsed || false

if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}

if (verifyParsed !== false && typeof verifyParsed !== 'function') {
throw new TypeError('option verifyParsed must be function')
}

// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
? typeChecker(type)
Expand Down Expand Up @@ -136,7 +141,8 @@ function json (options) {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
verify: verify,
verifyParsed: verifyParsed
})
}
}
Expand Down