Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ exports.matchesSchema = function(httpReq, specReq) {
var contentType = getMediaTypeFromHttpReq(httpReq);
var reqBody = getBodyContent(httpReq, isJson(contentType));
var result = specSchema.matchWithSchema(reqBody, specReq.schema);
logger.log('[MATCHING]'.yellow,'by request body schema', logger.stringfy(result));
logger.log('[MATCHING]'.yellow,'by request body schema', logger.stringfy(result.valid));
return result;
};

Expand Down
1 change: 0 additions & 1 deletion lib/debugRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exports.notFoundHandler = function(argv) {
}
return req.body;
}

var debugObj = {
originalUrl: req.originalUrl,
body: getBody(req),
Expand Down
30 changes: 28 additions & 2 deletions lib/handler-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var filterRequestBody = function (req) {

var filterSchema = function (req) {
return function (handler) {
return content.matchesSchema(req, handler.request);
return content.matchesSchema(req, handler.request).valid;
};
};

Expand All @@ -40,12 +40,38 @@ exports.filterHandlers = function (req, handlers, ignoreHeaders) {
if (matchRequestBodyHandlers.length > 0) {
return matchRequestBodyHandlers[0];
}
var filteredSchema = filterSchema(req);


var matchSchemaHandlers = filteredHandlers.filter(filterSchema(req));
var err;
filteredHandlers.map(function handerError(handler) {
var res = content.matchesSchema(req, handler.request);
if (res.valid === false) {
handler.response.name = '404';
handler.response.body = {
method: req.method,
headers: req.headers,
originalUrl: req.originalUrl,
error: {
params: res.errors[0].params,
message: res.errors[0].message,
dataPath: res.errors[0].dataPath,
schemaPath: res.errors[0].schemaPath
}
};
err = handler;
}
});

var matchSchemaHandlers = filteredHandlers.filter(filteredSchema);

if (matchSchemaHandlers.length > 0) {
return matchSchemaHandlers[0];
}

if (err !== null) {
return err;
}
}

return null;
Expand Down
5 changes: 2 additions & 3 deletions lib/spec-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ exports.hasSchema = function (spec){
return !!spec.schema;
};

exports.matchWithSchema = function (json, schema){
return tv4.validate(json, schema);
exports.matchWithSchema = function (json, schema) {
return tv4.validateMultiple(json, schema);
};

exports.validateAndParseSchema = function (spec){
Expand All @@ -28,4 +28,3 @@ exports.validateAndParseSchema = function (spec){
return spec;
};


2 changes: 1 addition & 1 deletion test/api/form-urlencoded-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('UrlEncoded Requests', function() {
.send('random_number=555&static=magic')

.expect(404)
.expect('Content-type', 'text/html; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.end(helper.endCb(done));
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/content-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
};

it('should returns true', function () {
assert.equal(content.matchesSchema(httpReq, specReq), true);
assert.equal(content.matchesSchema(httpReq, specReq).valid, true);
});

it('should log to console that schema is matched', function () {
Expand Down Expand Up @@ -285,7 +285,7 @@
};

it('should returns fals', function () {
assert.equal(content.matchesSchema(httpReq, specReq), false);
assert.equal(content.matchesSchema(httpReq, specReq).valid, false);
});

it('should log to console that schema is not matched', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/spec-schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ describe('Spec Schema', function() {

describe ('matchWithSchema', function() {
it('Should return true when json is validated against schema', function () {
assert.equal(specSchema.matchWithSchema({id: 1}, schema), true);
assert.equal(specSchema.matchWithSchema({id: 1}, schema).valid, true);
});

it('Should return false when json is not validated against schema', function () {
assert.equal(specSchema.matchWithSchema({idea: 1}, schema), false);
assert.equal(specSchema.matchWithSchema({idea: 1}, schema).valid, false);
});
});

Expand Down