Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from MugeSo/support-external-schemas
Browse files Browse the repository at this point in the history
Support external schemas
  • Loading branch information
jsdevel committed Apr 1, 2016
2 parents de2bcd8 + d758869 commit 69f5ef6
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -36,6 +36,12 @@ function validateResponseMiddlewareFactory(args) {
});
}

if (args.externalSchemas) {
Object.keys(args.externalSchemas).forEach(function(id) {
v.addSchema(args.externalSchemas[id], id);
});
}

return function(req, res, next) {
res.validateResponse = validateResponse;
next();
Expand Down
34 changes: 34 additions & 0 deletions test/data-driven/accept-external-references-through-local-ref.js
@@ -0,0 +1,34 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: '#/definitions/foo'
}
}
}
}
},

definitions: {
foo: {
$ref: 'http://example.com/schema'
}
},

externalSchemas: {
'http://example.com/schema': {
type: 'string'
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 'asdf'},

expectedValidationError: ''
};

@@ -0,0 +1,38 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: '#/definitions/foo'
}
}
}
}
},

definitions: {
foo: {
$ref: 'http://example.com/schema#/definitions/foo'
}
},

externalSchemas: {
'http://example.com/schema': {
definitions: {
foo: {
type: 'string'
}
}
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 'asdf'},

expectedValidationError: ''
};

34 changes: 34 additions & 0 deletions test/data-driven/accept-external-references-with-path.js
@@ -0,0 +1,34 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: 'http://example.com/schema#/definitions/foo'
}
}
}
}
},

definitions: null,

externalSchemas: {
'http://example.com/schema': {
definitions: {
foo: {
type: 'string'
}
}
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 'asdf'},

expectedValidationError: ''
};

30 changes: 30 additions & 0 deletions test/data-driven/accept-external-references.js
@@ -0,0 +1,30 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: 'http://example.com/schema'
}
}
}
}
},

definitions: null,

externalSchemas: {
'http://example.com/schema': {
type: 'string'
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 'asdf'},

expectedValidationError: ''
};

@@ -0,0 +1,44 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: '#/definitions/foo'
}
}
}
}
},

definitions: {
foo: {
$ref: 'http://example.com/schema'
}
},

externalSchemas: {
'http://example.com/schema': {
type: 'string'
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 2345},

expectedValidationError: {
status: 500,
message: 'The response was not valid.',
errors: [
{
path: 'foo',
errorCode: 'type.openapi.responseValidation',
message: 'foo is not of a type(s) string'
}
]
}
};

@@ -0,0 +1,48 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: '#/definitions/foo'
}
}
}
}
},

definitions: {
foo: {
$ref: 'http://example.com/schema#/definitions/foo'
}
},

externalSchemas: {
'http://example.com/schema': {
definitions: {
foo: {
type: 'string'
}
}
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 2345},

expectedValidationError: {
status: 500,
message: 'The response was not valid.',
errors: [
{
path: 'foo',
errorCode: 'type.openapi.responseValidation',
message: 'foo is not of a type(s) string'
}
]
}
};

44 changes: 44 additions & 0 deletions test/data-driven/fail-with-external-references-with-path.js
@@ -0,0 +1,44 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: 'http://example.com/schema#/definitions/foo'
}
}
}
}
},

definitions: null,

externalSchemas: {
'http://example.com/schema': {
definitions: {
foo: {
type: 'string'
}
}
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 2345},

expectedValidationError: {
status: 500,
message: 'The response was not valid.',
errors: [
{
path: 'foo',
errorCode: 'type.openapi.responseValidation',
message: 'foo is not of a type(s) string'
}
]
}
};

40 changes: 40 additions & 0 deletions test/data-driven/fail-with-external-references.js
@@ -0,0 +1,40 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
type: 'object',
properties: {
foo: {
$ref: 'http://example.com/schema'
}
}
}
}
},

definitions: null,

externalSchemas: {
'http://example.com/schema': {
type: 'string'
}
}
},

inputStatusCode: 200,
inputResponseBody: {foo: 2345},

expectedValidationError: {
status: 500,
message: 'The response was not valid.',
errors: [
{
path: 'foo',
errorCode: 'type.openapi.responseValidation',
message: 'foo is not of a type(s) string'
}
]
}
};

0 comments on commit 69f5ef6

Please sign in to comment.