Skip to content

Commit

Permalink
fixup: codacy codesmells
Browse files Browse the repository at this point in the history
  • Loading branch information
M3lkior committed Oct 8, 2021
1 parent 39ea2ba commit e78ebe9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
1 change: 0 additions & 1 deletion tests/parse.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions tests/to-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ describe('additionalAttributesMapping()', function () {
expect(result).toEqual({type: 'string'});
});

it('support pattern, minLength and maxLength for fixed', async function () {
const result = await avroToJsonSchema({type: 'fixed', pattern: '$pattern^', minLength: 1, maxLength: 10});
expect(result).toEqual({type: 'string', pattern: '$pattern^', minLength: 1, maxLength: 10});
});

it('support minItems and maxItems for array', async function () {
const result = await avroToJsonSchema({type: 'array', items: 'long', minItems: 0, maxItems: 10});
expect(result).toMatchObject({type: 'array', items: {type: 'integer'}, minItems: 0, maxItems: 10});
Expand Down
37 changes: 24 additions & 13 deletions to-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,39 @@ const additionalAttributesMapping = (typeInput, avroDefinition, jsonSchemaInput)

exampleAttributeMapping(type, avroDefinition.example, jsonSchema);

function setAdditionalAttribute(name) {
let isValueCoherent = true;
if (name === 'minLength' || name === 'maxLength') {
isValueCoherent = avroDefinition[name] > -1;
} else if (name === 'multipleOf') {
isValueCoherent = avroDefinition[name] > 0;
}
if (avroDefinition[name] !== undefined && isValueCoherent) jsonSchema[name] = avroDefinition[name];
}

switch (type) {
case 'int':
case 'long':
case 'float':
case 'double':
if (avroDefinition.minimum !== undefined) jsonSchema.minimum = avroDefinition.minimum;
if (avroDefinition.multipleOf !== undefined && avroDefinition.multipleOf > 0) jsonSchema.multipleOf = avroDefinition.multipleOf;
if (avroDefinition.maximum !== undefined) jsonSchema.maximum = avroDefinition.maximum;
if (avroDefinition.exclusiveMinimum !== undefined) jsonSchema.exclusiveMinimum = avroDefinition.exclusiveMinimum;
if (avroDefinition.exclusiveMaximum !== undefined) jsonSchema.exclusiveMaximum = avroDefinition.exclusiveMaximum;
setAdditionalAttribute('minimum');
setAdditionalAttribute('maximum');
setAdditionalAttribute('exclusiveMinimum');
setAdditionalAttribute('exclusiveMaximum');
setAdditionalAttribute('multipleOf');
break;
case 'string':
jsonSchema.format= avroDefinition.logicalType;
case 'fixed':
if (avroDefinition.pattern) jsonSchema.pattern = avroDefinition.pattern;
if (avroDefinition.minLength !== undefined && avroDefinition.minLength > -1) jsonSchema.minLength = avroDefinition.minLength;
if (avroDefinition.maxLength !== undefined && avroDefinition.maxLength > -1) jsonSchema.maxLength = avroDefinition.maxLength;
jsonSchema.format = avroDefinition.logicalType;
setAdditionalAttribute('pattern');
setAdditionalAttribute('minLength');
setAdditionalAttribute('maxLength');
break;
case 'array':
if (avroDefinition.minItems !== undefined && avroDefinition.minItems > -1) jsonSchema.minItems = avroDefinition.minItems;
if (avroDefinition.maxItems !== undefined && avroDefinition.maxItems > -1) jsonSchema.maxItems = avroDefinition.maxItems;
if (avroDefinition.uniqueItems !== undefined) jsonSchema.uniqueItems = avroDefinition.uniqueItems;
setAdditionalAttribute('minItems');
setAdditionalAttribute('maxItems');
setAdditionalAttribute('uniqueItems');
break;
default:
break;
}
};
Expand Down

0 comments on commit e78ebe9

Please sign in to comment.