Socket
Socket
Sign inDemoInstall

api-schema-builder

Package Overview
Dependencies
Maintainers
2
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-schema-builder - npm Package Compare versions

Comparing version 1.0.10 to 1.1.0

22

package.json
{
"name": "api-schema-builder",
"version": "1.0.10",
"version": "1.1.0",
"description": "build schema with validators for each endpoint",

@@ -47,5 +47,7 @@ "main": "src/index.js",

"dependencies": {
"ajv": "^6.6.2",
"ajv": "^6.10.0",
"clone-deep": "^4.0.1",
"swagger-parser": "^6.0.2"
"js-yaml": "^3.13.1",
"json-schema-deref-sync": "^0.10.0",
"swagger-parser": "^6.0.5"
},

@@ -56,13 +58,13 @@ "devDependencies": {

"chai-as-promised": "^7.1.1",
"coveralls": "^3.0.3",
"eslint": "^5.15.1",
"coveralls": "^3.0.4",
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-chai-friendly": "^0.4.1",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-standard": "^4.0.0",
"lodash.get": "^4.4.2",
"mocha": "^6.0.2",
"nyc": "^13.3.0",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"uuid": "^3.3.2"

@@ -69,0 +71,0 @@ },

@@ -20,6 +20,7 @@

- [How to use](#how-to-use)
- [api-schema-builder.buildSchema(PathToSwaggerFile, options)](#express-ajv-swagger-validationgetSchemapathtoswaggerfile-options)
- [api-schema-builder.buildSchemaSync(PathToSwaggerFile, options)](#express-ajv-swagger-validationgetSchemapathtoswaggerfile-options)
- [Arguments](#arguments)
- [Options](#options)
- [Response](#response)
- [api-schema-builder.buildSchema(PathToSwaggerFile, options)](#express-ajv-swagger-validationgetSchemaAsyncpathtoswaggerfile-options)
- [Usage Example](#usage-example)

@@ -42,10 +43,10 @@ - [Important Notes](#important-notes)

```js
var apiSchemaBuilder = require('api-schema-builder');
const apiSchemaBuilder = require('api-schema-builder');
```
### api-schema-builder.buildSchema(PathToSwaggerFile, options)
### api-schema-builder.buildSchemaSync(PathToSwaggerFile, options)
Build schema that contains ajv validators for each endpoint, it base on swagger definition.
Synchronously build schema that would contain ajv validators for each endpoint, based on swagger definition.
The function return Promise.
The function returns schema object.

@@ -95,2 +96,11 @@ #### Arguments

### api-schema-builder.buildSchema(PathToSwaggerFile, options)
Asynchronously build schema that would contain ajv validators for each endpoint, based on swagger definition.
The function returns Promise that resolves with a schema object.
s
Arguments, options and response are the same as for the `buildSchemaSync` method.
## Usage Example

@@ -100,49 +110,44 @@

```js
apiSchemaBuilder.buildSchema('test/unit-tests/input-validation/pet-store-swagger.yaml')
.then(function (schema) {
let schemaEndpoint = schema['/pet']['post'];
const schema = apiSchemaBuilder.buildSchemaSync('test/unit-tests/input-validation/pet-store-swagger.yaml');
let schemaEndpoint = schema['/pet']['post'];
//validate request's parameters
let isParametersMatch = schemaEndpoint.parameters.validate({ query: {},
headers: { 'public-key': '1.0'},path: {},files: undefined });
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
expect(isParametersMatch).to.be.true;
//validate request's parameters
let isParametersMatch = schemaEndpoint.parameters.validate({ query: {},
headers: { 'public-key': '1.0'},path: {},files: undefined });
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
expect(isParametersMatch).to.be.true;
//validate request's body
let isBodysMatch =schemaEndpoint.body.validate({'bark': 111});
expect(schemaEndpoint.body.errors).to.be.eql([{
'dataPath': '.bark',
'keyword': 'type',
'message': 'should be string',
'params': {
'type': 'string'
},
'schemaPath': '#/properties/bark/type'}
])
expect(isBodysMatch).to.be.false;
});
//validate request's body
let isBodysMatch =schemaEndpoint.body.validate({'bark': 111});
expect(schemaEndpoint.body.errors).to.be.eql([{
'dataPath': '.bark',
'keyword': 'type',
'message': 'should be string',
'params': {
'type': 'string'
},
'schemaPath': '#/properties/bark/type'}
])
expect(isBodysMatch).to.be.false;
```
### Validate response
```js
apiSchemaBuilder.buildSchema('test/unit-tests/input-validation/pet-store-swagger.yaml')
.then(function (schema) {
let schemaEndpoint = schema['/pet']['post'].responses['201'];
//validate response's body and headers
let isValid = schemaEndpoint.validate({
body :{ id:11, 'name': 111},
headers:{'x-next': '321'}
})
expect(schemaEndpoint.errors).to.be.eql([
const schema = apiSchemaBuilder.buildSchemaSync('test/unit-tests/input-validation/pet-store-swagger.yaml');
let schemaEndpoint = schema['/pet']['post'].responses['201'];
//validate response's body and headers
let isValid = schemaEndpoint.validate({
body :{ id:11, 'name': 111},
headers:{'x-next': '321'}
})
expect(schemaEndpoint.errors).to.be.eql([
{
'dataPath': '.body.name',
'keyword': 'type',
'message': 'should be string',
'params': {
'type': 'string'
},
'schemaPath': '#/body/properties/name/type'
}])
expect(isValid).to.be.false;
});
'dataPath': '.body.name',
'keyword': 'type',
'message': 'should be string',
'params': {
'type': 'string'
},
'schemaPath': '#/body/properties/name/type'
}])
expect(isValid).to.be.false;
```

@@ -149,0 +154,0 @@

'use strict';
var SwaggerParser = require('swagger-parser'),
schemaPreprocessor = require('./utils/schema-preprocessor'),
const schemaPreprocessor = require('./utils/schema-preprocessor'),
oai3 = require('./parsers/open-api3'),

@@ -12,3 +11,7 @@ oai2 = require('./parsers/open-api2'),

createContentTypeHeaders = require('./utils/createContentTypeHeaders'),
get = require('lodash.get');
get = require('lodash.get'),
deref = require('json-schema-deref-sync'),
fs = require('fs'),
yaml = require('js-yaml'),
SwaggerParser = require('swagger-parser');

@@ -19,2 +22,3 @@ const DEFAULT_SETTINGS = {

};
function buildSchema(swaggerPath, options) {

@@ -29,2 +33,9 @@ return Promise.all([

function buildSchemaSync(swaggerPath, options) {
const parsed = yaml.load(fs.readFileSync(swaggerPath), 'utf8');
const dereferenced = deref(parsed);
return buildValidations(parsed, dereferenced, options);
}
function buildValidations(referenced, dereferenced, receivedOptions) {

@@ -206,4 +217,5 @@ const options = Object.assign({}, DEFAULT_SETTINGS, receivedOptions);

module.exports = {
buildSchemaSync,
buildSchema,
buildValidations
};
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