diff --git a/README.md b/README.md index e939845..a0a89b8 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,11 @@ class Users { These are shortcuts for `@route(method, path)` where `@get('/revoke')` would be `@route('get', '/revoke')`. +### `@validate(config)` + +Add a validation object for the different types, except for the response. +`config` is an object, with keys for the different types, e.g. `payload`. + ## TODO * `@config` - Passing a whole config, solution that would encompase all future decorators in a crude way. Most control to the user. diff --git a/index.js b/index.js index f962b7b..a403a21 100644 --- a/index.js +++ b/index.js @@ -55,14 +55,39 @@ Object.keys(routeMethods).forEach(function (key) { exports[key] = route.bind(null, routeMethods[key]) }) +function validate(config) { + return function (target, key, descriptor) { + setRoute(target, key, { + config: { + validate: config + } + }) + + return descriptor + } +} + +exports.validate = validate + function setRoute(target, key, value) { if (!target.rawRoutes) { target.rawRoutes = [] } - var found = find(target.rawRoutes, 'config.id', key) + var targetName = target.constructor.name + var routeId = `${targetName}.${key}` + var defaultRoute = { + config: { + id: routeId + } + } + var found = find(target.rawRoutes, 'config.id', routeId) - target.rawRoutes.push(extend(found || {}, value)) + if (found) { + extend(true, found, value) + } else { + target.rawRoutes.push(extend(true, defaultRoute, value)) + } } function trimslash(s) { diff --git a/package.json b/package.json index c483331..6c8b174 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hapi-decorators", - "version": "0.1.1", + "version": "0.2.0", "description": "", "main": "index.js", "scripts": {