Skip to content

Commit

Permalink
fixup: refactor of setAdditionalAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
M3lkior committed Oct 14, 2021
1 parent 89a3081 commit 1a849a5
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions to-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,31 @@ 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];
function setAdditionalAttribute(...names) {
names.forEach(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 'int': // int, long, float, and double must support the attributes bellow
case 'long':
case 'float':
case 'double':
setAdditionalAttribute('minimum');
setAdditionalAttribute('maximum');
setAdditionalAttribute('exclusiveMinimum');
setAdditionalAttribute('exclusiveMaximum');
setAdditionalAttribute('multipleOf');
setAdditionalAttribute('minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf');
break;
case 'string':
jsonSchema.format = avroDefinition.logicalType;
setAdditionalAttribute('pattern');
setAdditionalAttribute('minLength');
setAdditionalAttribute('maxLength');
setAdditionalAttribute('pattern', 'minLength', 'maxLength');
break;
case 'array':
setAdditionalAttribute('minItems');
setAdditionalAttribute('maxItems');
setAdditionalAttribute('uniqueItems');
setAdditionalAttribute('minItems', 'maxItems', 'uniqueItems');
break;
default:
break;
Expand Down Expand Up @@ -182,7 +176,7 @@ async function convertAvroToJsonSchema(avroDefinition, isTopLevel) {
case 'enum':
jsonSchema.enum = avroDefinition.symbols;
break;
case 'float':
case 'float': // float and double must support the format attribute from the avro type
case 'double':
jsonSchema.format = type;
break;
Expand Down

0 comments on commit 1a849a5

Please sign in to comment.