Skip to content

Commit 11894e3

Browse files
authored
Merge pull request #125 from tomcollins/add-draft-2020-12-support
Add support for JSON Schema draft 2020-12
2 parents 9daf96f + 87078b6 commit 11894e3

File tree

10 files changed

+381
-107
lines changed

10 files changed

+381
-107
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
tests/docs
33
docs
4+
tmp
45
gh-pages/.jekyll-cache
56
gh-pages/_site

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Generates static documentation for humans based on the contents of [JSON schema]
99
## Support for JSON schema specification versions
1010

1111
Currently supports schema specified using the following [specification versions](https://json-schema.org/specification-links.html):
12-
draft-06, draft-07 and draft-2019-09.
12+
draft-06, draft-07, draft-2019-09, and draft 2020-12.
1313

1414
For complete documentation, including examples and supported keywords, see [tomcollins.github.io/json-schema-static-docs](https://tomcollins.github.io/json-schema-static-docs/).
1515

@@ -38,3 +38,26 @@ const JsonSchemaStaticDocs = require("json-schema-static-docs");
3838
console.log("Documents generated.");
3939
})();
4040
```
41+
42+
### How to use draft-2020-12
43+
44+
To use schema based on `draft-2020-12` set `options.jsonSchemaVersion` to `https://json-schema.org/draft/2020-12/schema`.
45+
46+
```javascript
47+
const JsonSchemaStaticDocs = require("json-schema-static-docs");
48+
49+
(async () => {
50+
let jsonSchemaStaticDocs = new JsonSchemaStaticDocs({
51+
inputPath: "./schema",
52+
outputPath: "./docs",
53+
jsonSchemaVersion: "https://json-schema.org/draft/2020-12/schema",
54+
ajvOptions: {
55+
allowUnionTypes: true,
56+
},
57+
});
58+
await jsonSchemaStaticDocs.generate();
59+
console.log("Documents generated.");
60+
})();
61+
```
62+
63+
All schema documentsmust use must use `draft-2020-12`, you can not combine this with earlier versions such as `draft-07`.

lib/json-schema-static-docs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const defaultOptions = {
2525
enableMetaEnum: false,
2626
addFrontMatter: false,
2727
displaySchema: true,
28+
jsonSchemaVersion: undefined,
2829
};
2930

3031
var JsonSchemaStaticDocs = function (options) {
@@ -58,7 +59,11 @@ JsonSchemaStaticDocs.prototype.generate = async function () {
5859
unresolvedSchemas.forEach((schema) => {
5960
schemas.push(schema.data);
6061
});
61-
const validator = new Validator(schemas, this._options.ajvOptions);
62+
const validator = new Validator(
63+
schemas,
64+
this._options.ajvOptions,
65+
this._options.jsonSchemaVersion
66+
);
6267

6368
if (this._options.enableMetaEnum) {
6469
validator.addMetaEnum();

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
const JsonSchemaVersions = {
2+
DRAFT_06: "https://json-schema.org/draft-06/schema",
3+
DRAFT_07: "https://json-schema.org/draft-07/schema",
4+
DRAFT_2019_09: "https://json-schema.org/draft/2019-09/schema",
5+
DRAFT_2020_12: "https://json-schema.org/draft/2020-12/schema",
6+
};
7+
18
const determineSchemaRelativePath = (schemaFilename, schemaInputPath) => {
29
let outputFilename = schemaFilename.substr(schemaInputPath.length);
310
if (outputFilename.substr(0, 1) === "/") {
@@ -7,5 +14,6 @@ const determineSchemaRelativePath = (schemaFilename, schemaInputPath) => {
714
};
815

916
module.exports = {
17+
JsonSchemaVersions,
1018
determineSchemaRelativePath: determineSchemaRelativePath,
1119
};

lib/validator.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
// const Ajv = require("ajv");
21
const Ajv = require("ajv/dist/2019");
2+
const Ajv2020 = require("ajv/dist/2020");
33
const draft7MetaSchema = require("ajv/dist/refs/json-schema-draft-07.json");
44
const draft6MetaSchema = require("ajv/dist/refs/json-schema-draft-06.json");
55
const addFormats = require("ajv-formats");
6+
const { JsonSchemaVersions } = require("./utils");
67

7-
let Validator = function (schemas, ajvOptions) {
8+
let Validator = function (schemas, ajvOptions, jsonSchemaVersion) {
89
let options = {};
910
Object.assign(options, ajvOptions);
10-
// options.schemas = schemas;
11-
this._ajv = new Ajv(options);
12-
this._ajv.addMetaSchema(draft7MetaSchema);
13-
this._ajv.addMetaSchema(draft6MetaSchema);
11+
if (jsonSchemaVersion === JsonSchemaVersions.DRAFT_2020_12) {
12+
// support draft-202-12 only
13+
this._ajv = new Ajv2020(options);
14+
} else {
15+
// support draft-06, draft-07, draft-2019-09
16+
this._ajv = new Ajv(options);
17+
this._ajv.addMetaSchema(draft7MetaSchema);
18+
this._ajv.addMetaSchema(draft6MetaSchema);
19+
}
1420
schemas.forEach((schema) => {
1521
this._ajv.addSchema(schema);
1622
});

0 commit comments

Comments
 (0)