Skip to content

Commit

Permalink
feat!: validate Avro schemas included in AsyncAPI docs (#96)
Browse files Browse the repository at this point in the history
This commit adds validation to the avro-schema-parser so that
invalid Avro schemas will be rejected.

I had to fix a few invalid Avro schemas in the tests (which were
using the type 'uuid' which isn't in the Avro spec) so this should
potentially be considered a major breaking change to the parser.

Closes: #37

Signed-off-by: Dale Lane <dale.lane@uk.ibm.com>
  • Loading branch information
dalelane committed Nov 22, 2021
1 parent 5c01abf commit dcbb195
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 10 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ await parser.parse(asyncapiWithAvro)

## Features

### Validation of Avro schemas

Avro schemas included in parsed AsyncAPI documents are validated using [avsc](https://www.npmjs.com/package/avsc). Invalid Avro schemas will cause the `parse` function to reject the promise, with an error that explains the problem.

```js
const assert = require('assert');
const parser = require('@asyncapi/parser');
const avroSchemaParser = require('./index');

parser.registerSchemaParser(avroSchemaParser);

const asyncapiWithInvalidAvro = `
asyncapi: 2.0.0
info:
title: Example with Avro
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/vnd.apache.avro;version=1.9.0'
payload:
type: notAValidAvroType
`;

parser.parse(asyncapiWithInvalidAvro)
.catch(err => {
assert.strictEqual(err.message,
'unknown type: "notAValidAvroType"');
});
```

### Support of required attributes

Required fields are fields with no default value and without the `"null"` union element.
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@
"@semantic-release/npm",
"@semantic-release/github"
]
},
"dependencies": {
"avsc": "^5.7.3"
}
}
11 changes: 11 additions & 0 deletions tests/asyncapi-avro-broken.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
asyncapi: 2.0.0
info:
title: My Broken API
version: '1.0.0'
channels:
mychannel:
publish:
message:
schemaFormat: application/vnd.apache.avro;version=1.9.0
payload:
$ref: 'schemas/Person-broken.avsc'
11 changes: 11 additions & 0 deletions tests/asyncapi-avro-invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
asyncapi: 2.0.0
info:
title: My Invalid API
version: '1.0.0'
channels:
mychannel:
publish:
message:
schemaFormat: application/vnd.apache.avro;version=1.9.0
payload:
$ref: 'schemas/Invalid.avsc'
17 changes: 14 additions & 3 deletions tests/parse.test.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions tests/schemas/Invalid.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"isThisAnAvroSchema": "no"
}
2 changes: 1 addition & 1 deletion tests/schemas/Person-1.9.0-namespace.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"fields": [{"name": "zipcode", "type": "int", "example": 53003}]
}
},
{"name": "someid", "type": "uuid"}
{"name": "someid", "type": "string", "logicalType": "uuid"}
]
}
2 changes: 1 addition & 1 deletion tests/schemas/Person-1.9.0.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"fields": [{"name": "zipcode", "type": "int", "example": 53003}]
}
},
{"name": "someid", "type": "uuid"}
{"name": "someid", "type": "string", "logicalType": "uuid"}
]
}
21 changes: 21 additions & 0 deletions tests/schemas/Person-broken.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Person",
"type": "record",
"fields": [
{"name": "name", "type": "string", "example": "Donkey"},
{"name": "age", "type": ["null", "int"], "default": null, "example": "123"},
{
"name": "favoriteProgrammingLanguage",
"type": {"name": "ProgrammingLanguage", "type": "enum", "symbols": ["JS", "Java", "Go", "Rust", "C"], "default": "JS"}
},
{
"name": "address",
"type": {
"name": "Address",
"type": "record",
"fields": [{"name": "zipcode", "type": "notAValidAvroType", "example": "53003"}]
}
},
{"name": "someid", "type": "string", "logicalType": "uuid"}
]
}
5 changes: 0 additions & 5 deletions tests/to-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ describe('avroToJsonSchema()', function () {
expect(result).toEqual({type: 'string'});
});

it('transforms uuid values', async function () {
const result = await avroToJsonSchema({type: 'uuid'});
expect(result).toEqual({type: 'string'});
});

it('transforms fixed values', async function () {
const result = await avroToJsonSchema({type: 'fixed', size: 5});
expect(result).toEqual({type: 'string', pattern: BYTES_PATTERN, minLength: 5, maxLength: 5});
Expand Down
12 changes: 12 additions & 0 deletions to-json-schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const avsc = require('avsc');

const BYTES_PATTERN = '^[\u0000-\u00ff]*$';
const INT_MIN = Math.pow(-2, 31);
const INT_MAX = Math.pow(2, 31) - 1;
Expand Down Expand Up @@ -126,10 +128,20 @@ const additionalAttributesMapping = (typeInput, avroDefinition, jsonSchemaInput)
}
};

function validateAvroSchema(avroDefinition) {
// don't need to use the output from parsing the
// avro definition - we're just using this as a
// validator as this will throw an exception if
// there are any problems with the definition
avsc.Type.forSchema(avroDefinition);
}

async function convertAvroToJsonSchema(avroDefinition, isTopLevel) {
const jsonSchema = {};
const isUnion = Array.isArray(avroDefinition);

validateAvroSchema(avroDefinition);

if (isUnion) {
jsonSchema.oneOf = [];
let nullDef = null;
Expand Down

0 comments on commit dcbb195

Please sign in to comment.