diff --git a/lib/writeSchema.js b/lib/writeSchema.js index 7e3bf7e9..10703c4e 100644 --- a/lib/writeSchema.js +++ b/lib/writeSchema.js @@ -38,7 +38,7 @@ export default function writeSchema({ schemadir, origindir }) { const inputPath = schema[s.fullpath] || (origindir && path.join(origindir, fileName)); let content = schema; - if (inputPath) { + if (inputPath && fs.existsSync(inputPath)) { content = fs.readJsonSync(inputPath); if (schema[s.meta] && schema[s.meta].description) { // copy description from external file diff --git a/test/api.test.js b/test/api.test.js index a141df73..38e74f05 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -88,6 +88,30 @@ describe('Testing Public API', () => { assert.strictEqual(result.markdown.length, 31); }); + /** + * When running with no `outDir` and no `schemaOut`, links to JSON Schema + * should use `fullPath`, even if it can't be resolved. + */ + it('Public API with non-existent schema path works', async () => { + const schemasFiles = await loadSchemas('readme-1'); + const schemas = schemasFiles.map(({ fileName, fullPath }) => ({ + fileName: path.join('/my-custom-path-to-schema', fileName), + // eslint-disable-next-line global-require, import/no-dynamic-require + content: fs.readJSONSync(fullPath), + })); + + const result = jsonschema2md(schemas, { + // don't output anything + header: true, + }); + assert.strictEqual(result.readme, undefined); + assert.strictEqual(result.schema.length, 3); + assert.strictEqual(result.markdown.length, 32); + + assertMarkdown(result.markdown.find(({ fileName }) => fileName === 'simple.md').markdownAst) + .contains('[simple.schema.json](/my-custom-path-to-schema/simple.schema.json "open original schema")'); + }); + it('Public API processes from single schema', async () => { const result = jsonschema2md(example, { includeReadme: true,