Comparing version 0.2.0 to 0.3.0
@@ -7,2 +7,3 @@ 'use strict' | ||
const Waterline = require('waterline') | ||
const validator = require('./validator.js') | ||
@@ -63,4 +64,2 @@ class Koaw { | ||
validate () {} | ||
/** | ||
@@ -265,3 +264,15 @@ * Set a before middleware to HTTP method | ||
_route (method, path, handler) { | ||
let model = this._getModel() | ||
let defaultHandlers = this._handlers | ||
let args = [path] | ||
args.push(function *(next) { | ||
let body = this.request.body | ||
if (method === 'post' || method === 'put') { | ||
if (!defaultHandlers[method]) { | ||
let v = yield validator(body, model._attributes) | ||
this.modelParams = v | ||
} | ||
} | ||
yield next | ||
}) | ||
@@ -268,0 +279,0 @@ // Add before middlewares |
{ | ||
"name": "koaw", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Create APIs using Koa and Waterline", | ||
@@ -51,2 +51,3 @@ "main": "./lib", | ||
"i": "^0.3.3", | ||
"jsonschema": "^1.0.2", | ||
"koa-router": "^5.3.0", | ||
@@ -53,0 +54,0 @@ "waterline": "^0.10.28" |
@@ -9,4 +9,9 @@ 'use strict' | ||
attributes: { | ||
name: 'string', | ||
description: 'string' | ||
name: { | ||
type: 'string', | ||
required: true | ||
}, | ||
description: { | ||
type: 'string' | ||
} | ||
} | ||
@@ -13,0 +18,0 @@ }) |
@@ -28,3 +28,3 @@ 'use strict' | ||
it('should have only this set of public methods', function () { | ||
let fns = ['methods', 'override', 'validate', 'before', 'after', 'route', 'register'] | ||
let fns = ['methods', 'override', 'before', 'after', 'route', 'register'] | ||
@@ -31,0 +31,0 @@ assert.equal(fns.length, this.publicMethods.length) |
@@ -15,3 +15,2 @@ 'use strict' | ||
}) | ||
after(function () { | ||
@@ -18,0 +17,0 @@ this.waterline.teardown() |
@@ -9,2 +9,3 @@ 'use strict' | ||
const waterline = require('../../fixtures/waterline') | ||
const faker = require('faker') | ||
@@ -50,3 +51,6 @@ describe('post middleware', function () { | ||
// Create document | ||
yield request(this.server.listen()).post(controller._path) | ||
yield request(this.server.listen()).post(controller._path).send({ | ||
name: faker.lorem.words()[0], | ||
description: faker.lorem.paragraph() | ||
}) | ||
@@ -59,2 +63,70 @@ assert.equal(spy.callCount, 4) | ||
}) | ||
it('should have validator middleware', function *() { | ||
let spy = sinon.spy() | ||
let controller = new Koaw({ | ||
orm: this.waterline, | ||
model: 'store' | ||
}) | ||
.methods('post') | ||
.before('post', function *(next) { | ||
spy(this.modelParams) | ||
yield next | ||
}) | ||
.register(this.server) | ||
yield request(this.server.listen()).post(controller._path).send({ | ||
name: faker.lorem.words()[0], | ||
description: faker.lorem.paragraph(), | ||
active: true | ||
}).expect(201) | ||
assert.equal(2, Object.keys(spy.args[0][0]).length) | ||
}) | ||
it('should reject if properties are incorrect', function *() { | ||
let spy = sinon.spy() | ||
let controller = new Koaw({ | ||
orm: this.waterline, | ||
model: 'store' | ||
}) | ||
.methods('post') | ||
.before('post', function *(next) { | ||
spy(this.modelParams) | ||
yield next | ||
}) | ||
.register(this.server) | ||
yield request(this.server.listen()).post(controller._path).send({ | ||
name: 1, | ||
description: faker.lorem.paragraph(), | ||
active: true | ||
}).expect(500) | ||
}) | ||
it('should not reject when is a custom route', function *() { | ||
let spy = sinon.spy() | ||
let controller = new Koaw({ | ||
orm: this.waterline, | ||
model: 'store' | ||
}) | ||
.route('post', 'custom', function *(next) { | ||
spy('should be executed') | ||
this.status = 201 | ||
yield next | ||
}) | ||
.register(this.server) | ||
yield request(this.server.listen()).post(`${controller._path}/custom`).send({ | ||
name: 1, | ||
description: faker.lorem.paragraph(), | ||
active: true | ||
}).expect(201) | ||
}) | ||
}) |
@@ -9,2 +9,3 @@ 'use strict' | ||
const waterline = require('../../fixtures/waterline') | ||
const faker = require('faker') | ||
@@ -50,3 +51,6 @@ describe('put middleware', function () { | ||
// Create document | ||
yield request(this.server.listen()).put(controller._path + '/124') | ||
yield request(this.server.listen()).put(controller._path + '/124').send({ | ||
name: faker.lorem.words()[0], | ||
description: faker.lorem.paragraph() | ||
}) | ||
@@ -53,0 +57,0 @@ assert.equal(spy.callCount, 4) |
@@ -8,2 +8,3 @@ 'use strict' | ||
const waterline = require('./fixtures/waterline') | ||
const faker = require('faker') | ||
@@ -61,2 +62,6 @@ describe('controller.route()', function () { | ||
.put(`/${this.controller.collection}/another`) | ||
.send({ | ||
name: faker.lorem.words()[0], | ||
description: faker.lorem.paragraph() | ||
}) | ||
.expect(200) | ||
@@ -63,0 +68,0 @@ |
45719
23
1266
4
+ Addedjsonschema@^1.0.2
+ Addedjsonschema@1.5.0(transitive)