Skip to content

Commit d59447f

Browse files
authored
Merge pull request #76 from tomcollins/allow-enum-meta-in-strict-mode
Allow meta:enum to be used in strict mode.
2 parents bbb8a70 + 33d2465 commit d59447f

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,31 @@ const JsonSchemaStaticDocs = require('json-schema-static-docs');
2828

2929
## Describing Enums
3030

31-
Json-schema allows a set of enumeration values to be defined for a string property but does not allow descriptions to be defined for each value. Descriptions within documentation ca.n be very useful
31+
Json-schema allows a set of enumeration values to be defined for a string property but does not allow descriptions to be defined for each value. Descriptions within documentation can be very useful.
3232

3333
This library supports the `meta:enum` convention (inspired by [adobe/jsonschema2md](https://github.com/adobe/jsonschema2md) to allow descriptions to be defined for enums.
3434

35+
You will need to enable this feature using the `enableMetaEnum` option:
36+
37+
```javascript
38+
let jsonSchemaStaticDocs = new JsonSchemaStaticDocs({
39+
inputPath: "./schema",
40+
outputPath: "./docs",
41+
enableMetaEnum: true,
42+
});
43+
await jsonSchemaStaticDocs.generate();
44+
```
45+
46+
_This allows the `meta:enum` keyword to be used when applying strict validation._
47+
48+
And then define the `meta:enum` descriptions adjacent to your `enum` e.g.
49+
3550
```yml
36-
property1: {
51+
property1:
3752
title: "Property 1",
3853
type: "string",
3954
enum: ["foo", 42],
40-
meta:enum: {
55+
meta:enum:
4156
foo: Description for foo
4257
42: Description for 42
4358
```

lib/json-schema-static-docs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const defaultOptions = {
1818
linkBasePath: "./",
1919
skipTemplates: false,
2020
ajvOptions: {},
21+
enableMetaEnum: false,
2122
};
2223

2324
var JsonSchemaStaticDocs = function (options) {
@@ -46,6 +47,10 @@ JsonSchemaStaticDocs.prototype.generate = async function () {
4647
});
4748
const validator = new Validator(schemas, this._options.ajvOptions);
4849

50+
if (this._options.enableMetaEnum) {
51+
validator.addMetaEnum();
52+
}
53+
4954
unresolvedSchemas.forEach((schema) => {
5055
try {
5156
validator.validateSchema(schema.data);

lib/validator.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ let Validator = function (schemas, ajvOptions) {
99
addFormats(this._ajv);
1010
};
1111

12+
Validator.prototype.addMetaEnum = function () {
13+
// @todo this should really perform validation to ensure that the input is safe
14+
this._ajv.addKeyword({
15+
keyword: "meta:enum",
16+
errors: false,
17+
});
18+
};
19+
1220
Validator.prototype.validateSchema = function (schema) {
1321
let validate = this._ajv.compile(schema);
1422
return true;
1523
};
1624

1725
Validator.prototype.validateSchemaAndData = function (schema, data) {
18-
// let ajv = new Ajv(options);
1926
var valid = this._ajv.validate(schema, data);
2027
if (!valid) {
2128
throw this._ajv.errors;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-static-docs",
3-
"version": "0.17.0",
3+
"version": "0.17.1",
44
"description": "",
55
"main": "lib/json-schema-static-docs.js",
66
"bin": {

0 commit comments

Comments
 (0)