api-schema-builder
Advanced tools
Comparing version 1.0.10 to 1.1.0
{ | ||
"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 | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53812
777
179
5
1
+ Addedjs-yaml@^3.13.1
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcharenc@0.0.2(transitive)
+ Addedclone@2.1.2(transitive)
+ Addedcrypt@0.0.2(transitive)
+ Addeddag-map@1.0.2(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-abstract@1.23.3(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-to-primitive@1.2.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-extglob@1.0.0(transitive)
+ Addedis-glob@2.0.1(transitive)
+ Addedis-invalid-path@0.1.0(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-valid-path@0.1.1(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedjson-schema-deref-sync@0.10.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmd5@2.2.1(transitive)
+ Addedmemory-cache@0.2.0(transitive)
+ Addedobject-inspect@1.13.2(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedregexp.prototype.flags@1.5.2(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedtraverse@0.6.10(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.2(transitive)
+ Addedtyped-array-length@1.0.6(transitive)
+ Addedtypedarray.prototype.slice@1.0.3(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addedvalid-url@1.0.9(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
Updatedajv@^6.10.0
Updatedswagger-parser@^6.0.5