Last active
August 29, 2015 14:17
-
-
Save ssebro/3791b915ea98cccf6d9e to your computer and use it in GitHub Desktop.
Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
// all routes are bootstrapped with default swagger spec and validation | |
// e.g. categories.get registers a mustbe permission check activity with 'categories.get' | |
// the Joi schema attributes are used to evaluate body or query params depending on the verb | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
category.routes.getById() | |
.swagger({summary: 'all the lovely categories by id'}); | |
// only explicit .swagger() declaration needed when an override of standard swagger spec is wanted | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
var customJoiSchema = {query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}}; | |
category.routes.get() | |
.validate(customJoiSchema); | |
// only explicit .validate() needed when you want to override/augment defaults | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
category.routes.delete() | |
.before(beforeDelete) | |
// adding validation fn handler | |
// however this clause accepts an unbounded amount of fn handlers which get triggered before the actual delete fn handler; | |
function beforeDelete(req, res, next) { | |
// do some checks with req. | |
if ('untouchable'===req.body.categories[0].name) { | |
next(new harvester.JSONAPI_Err({status: 400, detail: 'untouchable category'})); | |
} else { | |
next(); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
// all routes are bootstrapped with default swagger spec and validation | |
// e.g. categories.get registers a mustbe permission check activity with 'categories.get' | |
// the Joi schema attributes are used to evaluate body or query params depending on the verb | |
category.routes.put(false); | |
category.routes.post(false); | |
category.routes.delete(false); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Joi = require('joi'); | |
module.exports = function (harvester, swagger, mustbeConfig) { | |
var category = harvester | |
.resource('categories', { | |
name: Joi.string().required().description('a name'), | |
links: { | |
brand: 'brands' | |
} | |
}); | |
var customJoiSchema = {query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}}; | |
category.routes.get() | |
.validate(customJoiSchema); | |
category.routes.getById() | |
.swagger({summary: 'all the lovely categories by id'}); | |
category.routes.getChangeEventsStreaming(); | |
mustbeConfig.activities(function (activities) { | |
activities.can("category.mutate", function (identity, params, cb) { | |
cb(null, true); | |
}); | |
}); | |
category.routes.delete() | |
.authorize('category.mutate') | |
.before(beforeDelete); | |
function beforeDelete(req, res, next) { | |
if ('untouchable'===req.body.categories[0].name) { | |
next(new harvester.JSONAPI_Err({status: 400, detail: 'untouchable category'})); | |
} else { | |
next(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment