Skip to content

Commit

Permalink
Merge 3f5f7a7 into 4c0a560
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Feb 28, 2016
2 parents 4c0a560 + 3f5f7a7 commit 379cd99
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ let Service = function Service(Transports, config) {
utils.getLastResult.call(this, data, callback, true);
});

this.on("$htGetSchema", s.String(), (methodName, callback) => {
let method = this._methods[methodName];
if(!method) {
return callback({
error: "unknown-method",
method: methodName
});
}
let schema = method.schema;
if(!schema) {
return callback();
}
// We allow non ht-schema validation schemas
// too, so make sure it has a document fn
if(typeof schema.document !== 'function') {
return callback({
error: "incompatible-schema"
});
}
return callback(null, schema.document());
});

Transports.forEach((transport) => {
this.addTransport(transport);
});
Expand Down
79 changes: 79 additions & 0 deletions test-src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,85 @@ describe("Service", function() {

});

describe("$htGetSchema", function() {

it("should return unknown-method if called with unknown method", function(done) {

let service = new Service();
let _method = "hello-world";

service.call("$htGetSchema", _method, function(err) {
assert.equal(err.error, "unknown-method");
assert.equal(err.method, _method);
done();
});

});

it("should return nothing if method has no schema", function(done) {

let service = new Service();
let _method = "hello-world";

service.on(_method, function(data, callback) {
return callback(null, data);
});

service.call("$htGetSchema", _method, function(err, response) {
assert.ifError(err);
assert.strictEqual(response, undefined);
done();
});

});

it("should return incompatible-schema if called with a schema with no document fn", function(done) {

let service = new Service();
let _method = "hello-world";

let schema = function() {
return {
validate: function(data, callback) {
return callback(null, data);
}
}
}

service.call("$htGetSchema", _method, function(err) {
assert.equal(err.error, "unknown-method");
assert.equal(err.method, _method);
done();
});

});

it("should return schema if called with known method", function(done) {

let service = new Service();
let _method = "hello-world";

let schema = s.Object({
string: s.String(),
number: s.Number(),
array: s.Array([ s.String(), s.Number() ]),
opt: s.String({ opt: true })
});

service.on(_method, schema, function(data, callback) {
return callback(null, data);
});

service.call("$htGetSchema", _method, function(err, _schema) {
assert.ifError(err);
assert.deepEqual(_schema, schema.document());
done();
});

});

});

});

function mockTransport(fns) {
Expand Down

0 comments on commit 379cd99

Please sign in to comment.