Skip to content

Commit

Permalink
add validate api
Browse files Browse the repository at this point in the history
  • Loading branch information
yunong committed Oct 24, 2016
1 parent 28bb690 commit dc4f2ba
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 55 deletions.
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = function handler(req, res, next) {
```

## API
Synopsis: `enroute(opts, cb)`
Synopsis: `install(opts, cb)`

Installs routes as defined in opts into a restify server, invokes the callback
when done.
Expand Down Expand Up @@ -101,7 +101,7 @@ const CONFIG = {

const server = restify.createServer();
// install routes with enroute
enroute({
enroute.install({
config: CONFIG,
server: server
}, function (err) {
Expand All @@ -113,3 +113,52 @@ enroute({
}
});
```

Synopsis: `validate(opts, cb)`

Parse and validate a enroute config. This will verify that the config
is valid and return a POJO with the properties. Note only one of opts.config
or opts.configPath is needed.

* `opts` The options object containing
* `[opts.config]` The POJO of the config you want to validate.
* `[opts.configPath]` The path to the config on disk to validate.
* `cb` The callback f(err, validatedConfig). Returns `Error` if there's an
* error parsing or validating the config

### Example
```javascript
const enroute = require('restify-enroute');

const CONFIG = {
schemaVersion: 1,
routes: {
foo: {
get: {
source: './test/etc/fooGet.js'
},
post: {
source: './test/etc/fooPost.js'
},
delete: {
source: './test/etc/fooDelete.js'
},
head: {
source: './test/etc/fooHead.js'
},
}
}
};

const server = restify.createServer();
// install routes with enroute
enroute.validate({
config: CONFIG
}, function (err) {
if (err) {
console.error('unable to install routes');
} else {
console.log('config successfully validated');
}
});
```
76 changes: 41 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,45 @@ var vasync = require('vasync');
var install = require('./install');
var parser = require('./parser');

/**
* Installs configuration driven routes onto a restify server. Note only one of
* opts.config or opts.configPath is needed.
*
* exports
*
* @param {object} opts Options object.
* @param {string} [opts.config] The POJO of the config you want to validate.
* @param {string} [opts.configPath] The path to the data on disk to validate.
* @param {string} opts.server The restify server to install the routes onto.
* @param {function} cb The callback f(err, result)
* @returns {undefined}
*/
module.exports = function (opts, cb) {
assert.object(opts, 'opts');
assert.func(cb, 'cb');
// asserts of other inputs done by parse and installRoutes repectively
vasync.pipeline({arg: {}, funcs: [
function parse(ctx, _cb) {
parser.parse(opts, function (err, config) {
ctx.config = config;
return _cb(err);
});
},
function installRoutes(ctx, _cb) {
install({
enroute: ctx.config,
server: opts.server
}, function (err) {
return _cb(err);
});
}
]}, function (err, res) {
return cb(err);
});
module.exports = {
/**
* Installs configuration driven routes onto a restify server. Note only
* one of opts.config or opts.configPath is needed.
*
* exports
*
* @param {object} opts Options object.
* @param {string} [opts.config] The POJO of the config you want to
* validate.
* @param {string} [opts.configPath] The path to the data on disk to
* validate.
* @param {string} opts.server The restify server to install the routes
* onto.
* @param {function} cb The callback f(err, result)
* @returns {undefined}
*/
install: function (opts, cb) {
assert.object(opts, 'opts');
assert.func(cb, 'cb');
// asserts of other inputs done by parse and installRoutes repectively
vasync.pipeline({arg: {}, funcs: [
function parse(ctx, _cb) {
parser.parse(opts, function (err, config) {
ctx.config = config;
return _cb(err);
});
},
function installRoutes(ctx, _cb) {
install({
enroute: ctx.config,
server: opts.server
}, function (err) {
return _cb(err);
});
}
]}, function (err, res) {
return cb(err);
});
},
validate: parser.parse
};
6 changes: 3 additions & 3 deletions test/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('enroute-install', function () {
});

it('should install routes', function (done) {
enroute({
enroute.install({
config: CONFIG,
server: SERVER
}, function (err) {
Expand All @@ -86,7 +86,7 @@ describe('enroute-install', function () {
});

it('should fail if route source DNE', function (done) {
enroute({
enroute.install({
config: {
foo: {
get: 'source does not exist'
Expand All @@ -100,7 +100,7 @@ describe('enroute-install', function () {
});

it('should fail if route source is not a function', function (done) {
enroute({
enroute.install({
config: {
foo: {
get: './test/etc/notAFunction.js'
Expand Down
30 changes: 15 additions & 15 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash');
var assert = require('chai').assert;
var uuid = require('uuid');

var parser = require('../lib/parser');
var enroute = require('../lib');

var CONFIG_PATH = './test/etc/enroute.json';
var BAD_CONFIG_PATH = './test/etc/badEnroute.json';
Expand Down Expand Up @@ -67,7 +67,7 @@ var CONFIG = {

describe('enroute-parser', function () {
it('should parse config from file', function (done) {
parser.parse({
enroute.validate({
version: 1,
configPath: CONFIG_PATH
}, function (err, config) {
Expand All @@ -78,7 +78,7 @@ describe('enroute-parser', function () {
});

it('should parse config from string', function (done) {
parser.parse({
enroute.validate({
version: 1,
config: CONFIG
}, function (err, config) {
Expand All @@ -89,7 +89,7 @@ describe('enroute-parser', function () {
});

it('should error if no file', function (done) {
parser.parse({
enroute.validate({
configPath: uuid.v4()
}, function (err) {
assert.isOk(err);
Expand All @@ -98,7 +98,7 @@ describe('enroute-parser', function () {
});

it('should error if file not JSON', function (done) {
parser.parse({
enroute.validate({
configPath: BAD_CONFIG_PATH
}, function (err) {
assert.isOk(err);
Expand All @@ -110,7 +110,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
delete config.schemaVersion;

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -122,7 +122,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
config.schemaVersion = '1';

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -134,7 +134,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
config.schemaVersion = 2;

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -146,7 +146,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
delete config.routes;

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -158,7 +158,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
config.routes = {};

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -172,7 +172,7 @@ describe('enroute-parser', function () {
foo: {}
};

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -184,7 +184,7 @@ describe('enroute-parser', function () {
var config = _.cloneDeep(CONFIG);
config.foo = 'not-an-object';

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -199,7 +199,7 @@ describe('enroute-parser', function () {
source: 'foo'
};

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -212,7 +212,7 @@ describe('enroute-parser', function () {

config.routes.foo.post = {};

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand All @@ -225,7 +225,7 @@ describe('enroute-parser', function () {

config.routes.foo.post.foo = 'foo';

parser.parse({
enroute.validate({
config: config
}, function (err) {
assert.isOk(err);
Expand Down

0 comments on commit dc4f2ba

Please sign in to comment.