Skip to content

Commit 1c42da7

Browse files
author
Phil Sturgeon
committed
chore: tidying up the package
1 parent c8aec60 commit 1c42da7

29 files changed

+3414
-2923
lines changed

.editorconfig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
root = true
2+
13
[*]
24
charset = utf-8
3-
end_of_line = lf
4-
insert_final_newline = true
5+
6+
[*.{ts,js,yaml,scenario,md}]
57
indent_style = space
6-
indent_size = tab
7-
max_line_length = 80
8+
indent_size = 2
9+
10+
[*.{ts,js}]
811
trim_trailing_whitespace = true
912
insert_final_newline = true

.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es6: true,
6+
node: true
7+
},
8+
extends: [
9+
"eslint:recommended"
10+
],
11+
globals: {
12+
Atomics: 'readonly',
13+
SharedArrayBuffer: 'readonly'
14+
},
15+
parserOptions: {
16+
ecmaVersion: 2018
17+
},
18+
rules: {
19+
}
20+
}

.eslintrc.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ Currently converts from [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specificati
66

77
## Why?
88

9-
OpenAPI is a specification for describing RESTful APIs. OpenAPI 3.0 allows us to describe the structures of request and response payloads in a detailed manner. This would, theoretically, mean that we should be able to automatically validate request and response payloads. However, as of writing there aren't many validators around.
9+
OpenAPI is a specification for describing RESTful APIs. OpenAPI v3.0 allows us to describe the structures of request and response payloads in a detailed manner. This would, theoretically, mean that we should be able to automatically validate request and response payloads. However, as of writing there aren't many validators around.
1010

11-
The good news is that there are many validators for JSON Schema for different languages. The bad news is that OpenAPI 3.0 is not entirely compatible with JSON Schema. The Schema Object of OpenAPI 3.0 is an extended subset of JSON Schema Specification Wright Draft 00 with some differences.
12-
13-
The purpose of this project is to fill the gap by doing the conversion between these two formats.
11+
The good news is that there are many validators for JSON Schema for different languages. The bad news is that OpenAPI v3.0 is [not entirely compatible with JSON Schema](https://stoplight.io/blog/openapi-json-schema/). The Schema Object of OpenAPI v3.0 is an extended subset of JSON Schema Specification Wright Draft 00 with some differences. This will be resolved in OpenAPI v3.1, but until then... this tool will fill that gap.
1412

1513
There is also a [CLI tool](https://github.com/mikunn/openapi2schema) for creating a JSON of schemas from the whole API specification.
1614

17-
If you need to do the conversion in reverse, checkout [json-schema-to-openapi-schema](https://github.com/philsturgeon/json-schema-to-openapi-schema).
15+
If you need to do the conversion in reverse, checkout [json-schema-to-openapi-schema](https://github.com/openapi-contrib/json-schema-to-openapi-schema).
1816

1917
## Features
2018

21-
* converts OpenAPI 3.0 Schema Object to JSON Schema Draft 4
22-
* converts OpenAPI 3.0 Parameter Object to JSON Schema Draft 4
23-
* ~~converts [common named data types](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types) to `type` and `format`~~ *(removed in version 2.0.0)*
24-
* ~~for example `type: "dateTime"` becomes `type: "string"` with `format: "date-time"`~~
19+
* converts OpenAPI v3.0 Schema Object to JSON Schema Draft 4
20+
* converts OpenAPI v3.0 Parameter Object to JSON Schema Draft 4
2521
* deletes `nullable` and adds `"null"` to `type` array if `nullable` is `true`
2622
* supports deep structures with nested `allOf`s etc.
2723
* removes [OpenAPI specific properties](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20) such as `discriminator`, `deprecated` etc. unless specified otherwise
2824
* optionally supports `patternProperties` with `x-patternProperties` in the Schema Object
2925

30-
**NOTE**: `$ref`s are not dereferenced. Use a dereferencer such as [json-schema-ref-parser](https://www.npmjs.com/package/json-schema-ref-parser) prior to using this package.
26+
**NOTE**: `$ref`s are not handled in any way, so please use a resolver such as [json-ref-resolver](https://github.com/stoplightio/json-ref-resolver) prior to using this package.
3127

3228
## Installation
3329

3430
```
35-
npm install --save openapi-schema-to-json-schema
31+
npm install --save @openapi-contrib/openapi-schema-to-json-schema
3632
```
3733

3834
## Converting OpenAPI schema
@@ -41,8 +37,7 @@ Here's a small example to get the idea:
4137

4238
```js
4339

44-
var toJsonSchema = require('openapi-schema-to-json-schema');
45-
// OR: toJsonSchema = require('openapi-schema-to-json-schema').fromSchema;
40+
var toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema');
4641

4742
var schema = {
4843
type: 'string',
@@ -87,7 +82,7 @@ var schema = {
8782
format: 'date'
8883
};
8984

90-
var convertedSchema = toJsonSchema(schema, {dateToDateTime: true});
85+
var convertedSchema = toJsonSchema(schema, { dateToDateTime: true });
9186

9287
console.log(convertedSchema);
9388
```

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ function resolveOptions(options) {
6262
}
6363

6464
function patternPropertiesHandler(schema) {
65-
var pattern
66-
, patternsObj = schema.patternProperties
67-
, additProps = schema.additionalProperties
68-
;
65+
var pattern;
66+
var patternsObj = schema.patternProperties;
67+
var additProps = schema.additionalProperties;
6968

7069
if (typeof additProps !== 'object') {
7170
return schema;
@@ -82,9 +81,8 @@ function patternPropertiesHandler(schema) {
8281
}
8382

8483
function resolveNotSupported(notSupported, toRetain) {
85-
var i = 0
86-
, index
87-
;
84+
var i = 0;
85+
var index;
8886

8987
for (i; i < toRetain.length; i++) {
9088
index = notSupported.indexOf(toRetain[i]);

lib/convert.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var convertFromSchema = require('./converters/schema');
2-
var convertFromParameter = require('./converters/parameter');
1+
var convertFromSchema = require('./converters/schema')
2+
var convertFromParameter = require('./converters/parameter')
33

44
module.exports = {
5-
fromSchema: convertFromSchema,
6-
fromParameter: convertFromParameter,
7-
};
5+
fromSchema: convertFromSchema,
6+
fromParameter: convertFromParameter
7+
}

lib/converters/parameter.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
var convertFromSchema = require('./schema');
2-
var InvalidInputError = require('../errors/invalid-input-error');
1+
var convertFromSchema = require('./schema')
2+
var InvalidInputError = require('../errors/invalid-input-error')
33

4-
module.exports = convertFromParameter;
4+
module.exports = convertFromParameter
55

66
// Convert from OpenAPI 3.0 `ParameterObject` to JSON schema v4
7-
function convertFromParameter(parameter, options) {
7+
function convertFromParameter (parameter, options) {
88
if (parameter.schema !== undefined) {
9-
return convertParameterSchema(parameter, parameter.schema, options);
9+
return convertParameterSchema(parameter, parameter.schema, options)
1010
} else if (parameter.content !== undefined) {
11-
return convertFromContents(parameter, options);
11+
return convertFromContents(parameter, options)
1212
} else {
1313
if (options.strictMode) {
14-
throw new InvalidInputError('OpenAPI parameter must have either a \'schema\' or a \'content\' property');
14+
throw new InvalidInputError('OpenAPI parameter must have either a \'schema\' or a \'content\' property')
1515
}
16-
return convertParameterSchema(parameter, {}, options);
16+
return convertParameterSchema(parameter, {}, options)
1717
}
1818
}
1919

20-
function convertFromContents(parameter, options) {
21-
var schemas = {};
20+
function convertFromContents (parameter, options) {
21+
var schemas = {}
2222

2323
for (var mime in parameter.content) {
24-
schemas[mime] = convertParameterSchema(parameter, parameter.content[mime].schema, options);
24+
schemas[mime] = convertParameterSchema(parameter, parameter.content[mime].schema, options)
2525
}
2626

27-
return schemas;
27+
return schemas
2828
}
2929

30-
function convertParameterSchema(parameter, schema, options) {
31-
var jsonSchema = convertFromSchema(schema || {}, options);
30+
function convertParameterSchema (parameter, schema, options) {
31+
var jsonSchema = convertFromSchema(schema || {}, options)
3232

3333
if (parameter.description) {
34-
jsonSchema.description = parameter.description;
34+
jsonSchema.description = parameter.description
3535
}
3636

37-
return jsonSchema;
37+
return jsonSchema
3838
}

0 commit comments

Comments
 (0)