Skip to content

Commit a16f01c

Browse files
authored
Update README.md
1 parent 3afc219 commit a16f01c

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

README.md

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# OpenAPI Schema to JSON Schema
22

3-
A little NodeJS package to convert OpenAPI Schema Object to JSON Schema. Currently converts from [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) to [JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4).
3+
A little NodeJS package to convert OpenAPI Schema Object to JSON Schema.
4+
5+
Currently converts from [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) to [JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4).
46

57
## Why?
68

@@ -18,6 +20,7 @@ The purpose of this project is to fill the grap by doing the conversion between
1820
* deletes `nullable` and adds `"null"` to `type` array if `nullable` is `true`
1921
* supports deep structures with nested `allOf`s etc.
2022
* 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.
23+
* optionally supports `patternProperties` with `x-patternProperties` in the Schema Object
2124

2225
**NOTE**: `$ref`s are not dereferenced. Use a dereferences such as [json-schema-ref-parser](https://www.npmjs.com/package/json-schema-ref-parser) prior to using this package.
2326

@@ -59,31 +62,47 @@ Provide the function the schema object with possible options.
5962

6063
### Options
6164

62-
The function accepts `options` object as the second argument. Currently, the following options are supported:
63-
* `cloneSchema` (boolean)
64-
* If set to `false`, converts the provided schema in place. If `true`, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to `true`.
65-
* `dateToDateTime` (boolean)
66-
* This is `false` by default and leaves `date` type/format as is. If set to `true`, sets `type: "string"` and `format: "date-time"` if
67-
* `type: "string"` AND `format: "date"` OR
68-
* `type: "date"`
69-
* For example
70-
71-
```js
72-
var schema = {
73-
type: 'date',
74-
};
75-
76-
var convertedSchema = toJsonSchema(schema, {dateToDateTime: true});
77-
78-
console.log(convertedSchema);
79-
```
80-
81-
prints
82-
83-
```js
84-
{
85-
type: 'string',
86-
format: 'date-time',
87-
'$schema': 'http://json-schema.org/draft-04/schema#'
88-
}
89-
```
65+
The function accepts `options` object as the second argument.
66+
67+
#### `cloneSchema` (boolean)
68+
69+
If set to `false`, converts the provided schema in place. If `true`, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to `true`.
70+
71+
#### `dateToDateTime` (boolean)
72+
73+
This is `false` by default and leaves `date` type/format as is. If set to `true`, sets `type: "string"` and `format: "date-time"` if
74+
* `type: "string"` AND `format: "date"` OR
75+
* `type: "date"`
76+
77+
For example
78+
79+
```js
80+
var schema = {
81+
type: 'date'
82+
};
83+
84+
var convertedSchema = toJsonSchema(schema, {dateToDateTime: true});
85+
86+
console.log(convertedSchema);
87+
```
88+
89+
prints
90+
91+
```js
92+
{
93+
type: 'string',
94+
format: 'date-time',
95+
'$schema': 'http://json-schema.org/draft-04/schema#'
96+
}
97+
```
98+
99+
#### `supportPatternProperties` (boolean)
100+
101+
If set to `true` and `x-patternProperties` property is present, change `x-patternProperties` to `patternProperty` and call `patternPropertiesHandler`. If `patternPropertiesHandler` is not defined, call the default handler. See `patternPropertiesHandler` for more information.
102+
103+
#### `patternPropertiesHandler` (function)
104+
105+
Provide a function to handle pattern properties. The function takes the schema where `x-patternProperties` is defined on the root level. At this point `x-patternProperties` is changed to `patternProperties`.
106+
107+
If the handler is not provided, the default handler is used. If `additionalProperties` is set and is an object, the default handler sets it to false if the root level `type` is the same as one of the types in `patternProperties` unless `type` is `object`. This is because we might want to define `additionalProperties` in OpenAPI spec file, but want to validate against a pattern. The pattern would turn out to be useless if `additionalProperties` of the same type were allowed.
108+

0 commit comments

Comments
 (0)