Skip to content

Commit

Permalink
Adds jsonStrict option (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikAugust authored and MarkHerhold committed Jun 4, 2018
1 parent fc4ff40 commit 50d6699
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -102,6 +102,7 @@ console.log('curl -i http://localhost:3000/users -d "name=test"');
- `urlencoded` **{Boolean}** Parse urlencoded bodies, default `true`
- `text` **{Boolean}** Parse text bodies, default `true`
- `json` **{Boolean}** Parse json bodies, default `true`
- `jsonStrict` **{Boolean}** Toggles co-body strict mode; if set to true - only parses arrays or objects, default `true`
- `formidable` **{Object}** Options to pass to the formidable multipart parser
- `onError` **{Function}** Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
- `strict` **{Boolean}** If enabled, don't parse GET, HEAD, DELETE requests, default `true`
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Expand Up @@ -107,6 +107,11 @@ declare namespace koaBody {
*/
json?: boolean;

/**
* Toggles co-body strict mode; if true, only parses arrays or objects, default true
*/
jsonStrict?: boolean;

/**
* {Object} Options to pass to the formidable multipart parser
*/
Expand Down
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -40,6 +40,7 @@ function requestbody(opts) {
opts.text = 'text' in opts ? opts.text : true;
opts.encoding = 'encoding' in opts ? opts.encoding : 'utf-8';
opts.jsonLimit = 'jsonLimit' in opts ? opts.jsonLimit : '1mb';
opts.jsonStrict = 'jsonStrict' in opts ? opts.jsonStrict : true;
opts.formLimit = 'formLimit' in opts ? opts.formLimit : '56kb';
opts.queryString = 'queryString' in opts ? opts.queryString : null;
opts.formidable = 'formidable' in opts ? opts.formidable : {};
Expand All @@ -54,7 +55,8 @@ function requestbody(opts) {
if (opts.json && ctx.is('json')) {
bodyPromise = buddy.json(ctx, {
encoding: opts.encoding,
limit: opts.jsonLimit
limit: opts.jsonLimit,
strict: opts.jsonStrict
});
} else if (opts.urlencoded && ctx.is('urlencoded')) {
bodyPromise = buddy.form(ctx, {
Expand Down

0 comments on commit 50d6699

Please sign in to comment.