Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
Updated README for 1.2 and 2.0 validation examples
Browse files Browse the repository at this point in the history
* Minor refactoring of swagger-metadata middleware for consistency
  • Loading branch information
whitlockjc committed Aug 13, 2014
1 parent cb0e76b commit 0acc168
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ Here is an example showing how to use both versions of the `validate` function *
documented)*:

```javascript
var spec = require('swagger-tools').v1_2;
var swagger = require('swagger-tools');

// 1.2 Example
var spec1 = swagger.specs.v1_2;
var petJson = require('./samples/1.2/pet.json');
var rlJson = require('./samples/1.2/resource-listing.json');
var results = spec.validate(rlJson, [petJson]);
var results1 = spec1.validate(rlJson, [petJson]);

// 2.0 Example
var petStoreJson = require('./samples/2.0/petstore.json');
var spec2 = swagger.specs.v2_0;
var results2 = spec2.validate(petStoreJson);
```

Here is an example of using the Swagger middleware for validating requests based on your Swagger resource documents:
Expand Down
12 changes: 6 additions & 6 deletions middleware/swagger-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ exports = module.exports = function swaggerMetadataMiddleware (resourceList, res
});
});

return function swagger (req, res, next) {
return function swaggerMetadata (req, res, next) {
var path = parseurl(req).pathname;
var match;
var api = _.find(apis, function (api) {
match = api.re.exec(path);
return _.isArray(match);
});
var swaggerMetadata = {
var metadata = {
api: api ? api.api : undefined,
authorizations: resourceList.authorizations || {},
models: api ? resources[api.resourceIndex].models || {} : {},
Expand All @@ -125,9 +125,9 @@ exports = module.exports = function swaggerMetadataMiddleware (resourceList, res
};

// Collect the parameter values
if (!_.isUndefined(swaggerMetadata.operation)) {
if (!_.isUndefined(metadata.operation)) {
try {
_.each(swaggerMetadata.operation.parameters, function (param) {
_.each(metadata.operation.parameters, function (param) {
var val;

// Get the value to validate based on the operation parameter type
Expand Down Expand Up @@ -168,14 +168,14 @@ exports = module.exports = function swaggerMetadataMiddleware (resourceList, res
val = param.defaultValue;
}

swaggerMetadata.params[param.name] = {
metadata.params[param.name] = {
schema: param,
value: val
};
});

// Attach Swagger metadata to the request
req.swagger = swaggerMetadata;
req.swagger = metadata;
} catch (err) {
return next(err.message);
}
Expand Down

0 comments on commit 0acc168

Please sign in to comment.