Skip to content

Commit

Permalink
Add .none function for uploading fields only (expressjs#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel authored and LinusU committed Aug 4, 2016
1 parent da9062a commit 01883e2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Example:
]
```

#### `.none()`

Accept only text fields. If any file upload is made, error with code
"LIMIT\_UNEXPECTED\_FILE" will be issued. This is the same as doing `upload.fields([])`.

#### `.any()`

Accepts all files that comes over the wire. An array of files will be stored in
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Multer.prototype.fields = function (fields) {
return this._makeMiddleware(fields, 'OBJECT')
}

Multer.prototype.none = function () {
return this._makeMiddleware([], 'NONE')
}

Multer.prototype.any = function () {
function setup () {
return {
Expand Down
3 changes: 3 additions & 0 deletions lib/file-appender.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function FileAppender (strategy, req) {
this.req = req

switch (strategy) {
case 'NONE': break
case 'VALUE': break
case 'ARRAY': req.files = []; break
case 'OBJECT': req.files = Object.create(null); break
Expand All @@ -23,6 +24,7 @@ FileAppender.prototype.insertPlaceholder = function (file) {
}

switch (this.strategy) {
case 'NONE': break
case 'VALUE': break
case 'ARRAY': this.req.files.push(placeholder); break
case 'OBJECT':
Expand All @@ -39,6 +41,7 @@ FileAppender.prototype.insertPlaceholder = function (file) {

FileAppender.prototype.removePlaceholder = function (placeholder) {
switch (this.strategy) {
case 'NONE': break
case 'VALUE': break
case 'ARRAY': arrayRemove(this.req.files, placeholder); break
case 'OBJECT':
Expand Down
47 changes: 47 additions & 0 deletions test/none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env mocha */

var assert = require('assert')

var util = require('./_util')
var multer = require('../')
var FormData = require('form-data')

describe('None', function () {
var parser

before(function () {
parser = multer().none()
})

it('should not allow file uploads', function (done) {
var form = new FormData()

form.append('key1', 'val1')
form.append('key2', 'val2')
form.append('file', util.file('small0.dat'))

util.submitForm(parser, form, function (err, req) {
assert.ok(err)
assert.equal(err.code, 'LIMIT_UNEXPECTED_FILE')
assert.equal(req.files, undefined)
assert.equal(req.body['key1'], 'val1')
assert.equal(req.body['key2'], 'val2')
done()
})
})

it('should handle text fields', function (done) {
var form = new FormData()

form.append('key1', 'val1')
form.append('key2', 'val2')

util.submitForm(parser, form, function (err, req) {
assert.ifError(err)
assert.equal(req.files, undefined)
assert.equal(req.body['key1'], 'val1')
assert.equal(req.body['key2'], 'val2')
done()
})
})
})

0 comments on commit 01883e2

Please sign in to comment.