Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Radchenko committed Aug 30, 2015
1 parent 94ed75b commit c38441d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-decorators",
"version": "0.1.1",
"version": "0.2.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c38441d

Please sign in to comment.