New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

express-openapi-validation

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-openapi-validation - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

test/data-driven/accept-a-required-query-param.js

42

index.js
var convert = require('openapi-jsonschema-parameters');
var JsonschemaValidator = require('jsonschema').Validator;
var loggingKey = 'express-openapi-validation';
var LOCAL_DEFINITION_REGEX = /^#\/([^\/]+)\/([^\/]+)$/;

@@ -7,2 +9,10 @@ module.exports = validate;

function validate(args) {
if (!args) {
throw new Error(loggingKey + ': missing args argument');
}
if (!Array.isArray(args.parameters)) {
throw new Error(loggingKey + ': args.parameters must be an Array');
}
var schemas = convert(args.parameters);

@@ -15,6 +25,26 @@ var errorTransformer = args.errorTransformer || toOpenapiValidationError;

if (Array.isArray(args.definitions)) {
definitions.forEach(function(definition) {
if (definition.id) {
validator.addSchema(definition, definition.id);
if (Array.isArray(args.schemas)) {
args.schemas.forEach(function(schema) {
var id = schema.id;
if (id) {
var localSchemaPath;
if (bodySchema) {
localSchemaPath = LOCAL_DEFINITION_REGEX.exec(id);
}
if (localSchemaPath) {
var localSchemas = bodySchema[localSchemaPath[1]];
if (!localSchemas) {
localSchemas = bodySchema[localSchemaPath[1]] = {};
}
localSchemas[localSchemaPath[2]] = schema;
}
v.addSchema(schema, id);
} else {
console.warn(loggingKey, 'igorning schema without id property');
}

@@ -28,2 +58,6 @@ });

if (req.body && bodySchema) {
errors.push.apply(errors, v.validate(req.body, bodySchema).errors);
}
if (req.query && querySchema) {

@@ -30,0 +64,0 @@ errors.push.apply(errors, v.validate(req.query, querySchema).errors);

2

package.json
{
"name": "express-openapi-validation",
"version": "0.0.2",
"version": "0.1.0",
"description": "Express middleware for openapi parameter validation.",

@@ -5,0 +5,0 @@ "scripts": {

# express-openapi-validation [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url]
> Express middleware for openapi parameter validation.
If validation errors occur, `next` is called with `{status: 400, errors: [<validation errors>]}`.
## Highlights

@@ -11,2 +13,3 @@

* Currently supports openapi 2.0 (a.k.a. swagger 2.0) parameter lists.
* Supports `$ref` in body schemas i.e. `#/definitions/SomeType`.

@@ -28,3 +31,3 @@ ## Example

],
definitions: null, // an optional array of jsonschema definitions
schemas: null, // an optional array of jsonschemas used to dereference $ref
version: 'swagger-2.0', // default optional value for future versions of openapi

@@ -42,2 +45,37 @@ errorTransformer: null // an optional transformer function to format errors

## API
### validate(args)
#### args.parameters
An array of openapi parameters.
#### args.schemas
An array of schemas. Each schema must have an `id` property. See `./test/data-driven/`
for tests with `schemas`. Ids may be schema local (i.e. `#/definitions/SomeType`),
or URL based (i.e. `/SomeType`). When supplied, `$ref` usage will map exactly to the
Id e.g. if `id` is `/SomeType`, `$ref` must be `/SomeType`.
#### args.version
An optional string that currently does nothing. This will ensure nothing breaks
for new versions of openapi drafts that get added in the future.
#### args.errorTransformer
A function that transforms errors.
E.G.
```
errorTransformer: function(error) {
return {
message: error.message
};
}
```
See the error format in [jsonschema](https://www.npmjs.com/package/jsonschema).
## LICENSE

@@ -44,0 +82,0 @@ ``````

@@ -16,2 +16,9 @@ var bodyParser = require('body-parser');

it('should ' + testName, function(done) {
if (fixture.constructorError) {
expect(function() {
sut(fixture.validateArgs);
}).to.throw(fixture.constructorError);
return done();
}
var test = request(sampleApp(sut(fixture.validateArgs)));

@@ -18,0 +25,0 @@ var path = '/test' + fixture.path;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc