From b37d883aabfd315bd97aae82664b50dfa1737bde Mon Sep 17 00:00:00 2001 From: Pedro Victor Date: Fri, 21 Apr 2017 02:36:01 +0000 Subject: [PATCH] Parse subdocument schema as Object --- src/bodymen-schema.js | 4 ++++ test/index.js | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/bodymen-schema.js b/src/bodymen-schema.js index af6ddda..6ac1a0b 100644 --- a/src/bodymen-schema.js +++ b/src/bodymen-schema.js @@ -255,6 +255,10 @@ export default class BodymenSchema { options = {type: RegExp, default: options} } else if (_.isFunction(options)) { options = {type: options} + } else if (_.isObject(options)) { + if (!_.isFunction(options.type)) { + options.type = Object + } } return options || {} diff --git a/test/index.js b/test/index.js index 7a91306..841a4eb 100644 --- a/test/index.js +++ b/test/index.js @@ -40,7 +40,7 @@ test('Bodymen handler', (t) => { }) test('Bodymen middleware', (t) => { - t.plan(5) + t.plan(7) request(route()) .post('/tests') @@ -96,4 +96,23 @@ test('Bodymen middleware', (t) => { if (err) throw err t.same(res.body, {links: [{icon: 'path to icon'}]}, 'should respond with correct object') }) + + // parse subdocuments as Object + request(route(new Schema({sub: {name: String}}))) + .post('/tests') + .send({sub: {name: 'test'}}) + .expect(200) + .end((err, res) => { + if (err) throw err + t.same(res.body, {sub: {name: 'test'}}, 'should respond with correct object') + }) + + request(route(new Schema({links: [{icon: String}]}))) + .post('/tests') + .send({links: [{icon: 'path to icon'}]}) + .expect(200) + .end((err, res) => { + if (err) throw err + t.same(res.body, {links: [{icon: 'path to icon'}]}, 'should respond with correct object') + }) })