From 9e61d6a95117ef6c763538712de01a8f3920c7ea Mon Sep 17 00:00:00 2001 From: Rick Smit Date: Sat, 19 Jun 2021 20:15:41 +0200 Subject: [PATCH] add option to supply jsonSchema --- README.md | 3 +++ src/javro.js | 2 +- src/resolve_references.js | 4 ++-- test/int/simple/simple.int.test.js | 9 +++++++++ test/utils/javro-with-validation.js | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b096de7..592b877 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ const { jvro, SchemaRegistryAvroFetcher } = require('javro'); javro({ // The location of your JSON Schema jsonSchemaFile: '/path/to/your/jsonSchemaFile.json', + + // A JSON schema object. This object will be used instead of reading from `jsonSchemaFile`. + jsonSchema: jsonSchemaObject, // The 'namespace' used in the generated AVSC - the 'name' will be taken either from the 'title' in the JSON Schema or // the file name if 'title' isn't present (in this case that would be 'jsonSchemaFile') diff --git a/src/javro.js b/src/javro.js index c74cf8c..3eb5a9b 100644 --- a/src/javro.js +++ b/src/javro.js @@ -45,7 +45,7 @@ function fetchAvroCompatibility(options, res) { } function resolveReferencesAndTurnIntoAvro(options) { - return resolveReferences(options.jsonSchemaFile).then((resolvedJson) => { + return resolveReferences(options.jsonSchemaFile, options.jsonSchema).then((resolvedJson) => { const newAvsc = new JsonSchemaToAvro(resolvedJson, options.allowMultipleTypes || false) .mapObjectToRecord(options.namespace, grabAvroName(options.jsonSchemaFile, resolvedJson)); return fetchCorrespondingAvro(resolvedJson, options).then((oldAvsc) => { diff --git a/src/resolve_references.js b/src/resolve_references.js index df459c2..0243fdb 100644 --- a/src/resolve_references.js +++ b/src/resolve_references.js @@ -16,6 +16,6 @@ const $RefParser = require('json-schema-ref-parser'); -module.exports = function resolveReferences(jsonSchemaUrl) { - return $RefParser.dereference(jsonSchemaUrl); +module.exports = function resolveReferences(jsonSchemaUrl, jsonSchema) { + return $RefParser.dereference(jsonSchemaUrl, jsonSchema); }; diff --git a/test/int/simple/simple.int.test.js b/test/int/simple/simple.int.test.js index 38454bf..3e274e3 100644 --- a/test/int/simple/simple.int.test.js +++ b/test/int/simple/simple.int.test.js @@ -15,6 +15,7 @@ */ const path = require('path'); +const fs = require('fs'); const javroWithValidation = require('../../utils/javro-with-validation'); test('primitive types', () => javroWithValidation(path.resolve(__dirname, './primitives.avsc'), { @@ -59,3 +60,11 @@ test('enum', () => javroWithValidation(path.resolve(__dirname, './some_enum.avsc }).then((res) => { expect(res.actualAvro).toStrictEqual(res.expectedAvro); })); + +test('with jsonSchema', () => javroWithValidation(path.resolve(__dirname, './some_enum.avsc'), { + jsonSchemaFile: path.resolve(__dirname, './some_enum.json'), + jsonSchema: fs.readFileSync(path.resolve(__dirname, './some_enum.json')), + namespace: 'test.jsonschema.to.avro.namespace', +}).then((res) => { + expect(res.actualAvro).toStrictEqual(res.expectedAvro); +})); diff --git a/test/utils/javro-with-validation.js b/test/utils/javro-with-validation.js index 8516662..33b4f22 100644 --- a/test/utils/javro-with-validation.js +++ b/test/utils/javro-with-validation.js @@ -22,7 +22,7 @@ const $RefParser = require('json-schema-ref-parser'); const { javro } = require('../../src/javro'); module.exports = function javroWithValidation(expectedAvroFile, options) { - const deReferencedSchema = $RefParser.dereference(options.jsonSchemaFile); + const deReferencedSchema = $RefParser.dereference(options.jsonSchemaFile, options.jsonSchema); const actualAvro = javro(options); const expectedAvro = fs.readFile(expectedAvroFile, { encoding: 'UTF-8' });