Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/javro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/resolve_references.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
9 changes: 9 additions & 0 deletions test/int/simple/simple.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'), {
Expand Down Expand Up @@ -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);
}));
2 changes: 1 addition & 1 deletion test/utils/javro-with-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down