Skip to content

Allow AJV options to be passed in. #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![npm version](https://badge.fury.io/js/json-schema-static-docs.svg)](https://badge.fury.io/js/json-schema-static-docs) [![CircleCI](https://circleci.com/gh/tomcollins/json-schema-static-docs/tree/master.svg?style=svg)](https://circleci.com/gh/tomcollins/json-schema-static-docs/tree/master) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)


## Installation

```
Expand All @@ -17,7 +16,10 @@ const JsonSchemaStaticDocs = require('json-schema-static-docs');
( async () => {
let jsonSchemaStaticDocs = new JsonSchemaStaticDocs({
inputPath: './schema',
outputPath: './docs'
outputPath: './docs',
ajvOptions: {
allowUnionTypes: true
}
});
await jsonSchemaStaticDocs.generate();
console.log('Documents generated.');
Expand All @@ -26,9 +28,9 @@ const JsonSchemaStaticDocs = require('json-schema-static-docs');

## Custom Templates

Templates are authored in [handlebars.js](https://handlebarsjs.com).
Templates are authored in [handlebars.js](https://handlebarsjs.com).

The default template is [templates/markdown/schema.hbs](https://github.com/tomcollins/json-schema-static-docs/blob/master/templates/markdown/schema.hbs).
The default template is [templates/markdown/schema.hbs](https://github.com/tomcollins/json-schema-static-docs/blob/master/templates/markdown/schema.hbs).

You can provide your own custom templates using the `templatePath` option.

Expand Down
3 changes: 2 additions & 1 deletion lib/json-schema-static-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultOptions = {
additionalDataSources: {},
linkBasePath: "./",
skipTemplates: false,
ajvOptions: {},
};

var JsonSchemaStaticDocs = function (options) {
Expand All @@ -43,7 +44,7 @@ JsonSchemaStaticDocs.prototype.generate = async function () {
unresolvedSchemas.forEach((schema) => {
schemas.push(schema.data);
});
const validator = new Validator(schemas);
const validator = new Validator(schemas, this._options.ajvOptions);

unresolvedSchemas.forEach((schema) => {
try {
Expand Down
4 changes: 4 additions & 0 deletions lib/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const resolveRef = (
};

const mergeProperty = (unresolvedSchema, mergedSchema, key) => {
if (!unresolvedSchema.data.properties || !mergedSchema.data.properties) {
return;
}

let unresolvedProperty = unresolvedSchema.data.properties[key];
let mergedProperty = mergedSchema.data.properties[key];

Expand Down
7 changes: 5 additions & 2 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const Ajv = require("ajv");
const addFormats = require("ajv-formats");

let Validator = function (schemas) {
this._ajv = new Ajv({ schemas });
let Validator = function (schemas, ajvOptions) {
let options = {};
Object.assign(options, ajvOptions);
options.schemas = schemas;
this._ajv = new Ajv(options);
addFormats(this._ajv);
};

Expand Down
14 changes: 9 additions & 5 deletions tests/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const Validator = require("../lib/validator.js");
let schema = {
$id: "1",
title: "1 unresolved",
type: "object",
properties: {
property1: {
type: "string",
type: ["string", "number"],
},
additionalProperties: false,
},
additionalProperties: false,
};
Expand All @@ -24,15 +26,17 @@ let dataInvalidType = {
property1: null,
};

const defaultOptions = { allowUnionTypes: true };

test("validates schemas and data", () => {
const validator = new Validator({ schemas: [schema] });
const validator = new Validator([schema], defaultOptions);
const result = validator.validateSchemaAndData(schema, dataValid);
expect(result).toBe(true);
});

test("fails with additional properties", () => {
expect.assertions(2);
const validator = new Validator({ schemas: [schema] });
const validator = new Validator([schema], defaultOptions);
let result;
try {
result = validator.validateSchemaAndData(schema, dataInvalidAdditional);
Expand All @@ -42,9 +46,9 @@ test("fails with additional properties", () => {
}
});

test("XXX fails with invalid type", () => {
test("fails with invalid type", () => {
expect.assertions(3);
const validator = new Validator({ schemas: [schema] });
const validator = new Validator([schema], defaultOptions);
let result;
try {
result = validator.validateSchemaAndData(schema, dataInvalidType);
Expand Down