Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@asyncapi/avro-schema-parser

Package Overview
Dependencies
Maintainers
3
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asyncapi/avro-schema-parser - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

tests/asyncapi-avro-1.9.0-additional-attributes.yaml

4

package.json
{
"name": "@asyncapi/avro-schema-parser",
"version": "0.5.0",
"version": "0.6.0",
"description": "An AsyncAPI schema parser for Avro 1.x schemas.",

@@ -33,3 +33,3 @@ "main": "index.js",

"devDependencies": {
"@asyncapi/parser": "^1.9.0",
"@asyncapi/parser": "^1.10.0",
"@semantic-release/commit-analyzer": "^8.0.1",

@@ -36,0 +36,0 @@ "@semantic-release/github": "^7.0.4",

@@ -133,2 +133,6 @@ # Avro Schema Parser

### Support of required attributes
Required fields are fields with no default value and without the `"null"` union element.
### Support for extra attributes on top of Avro specification

@@ -142,2 +146,20 @@

For Number instances:
- `multipleOf` - Can be used to define [the multipleOf value of a numeric instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.1). The `multipleOf` MUST be a number, strictly greater than 0.
- `maximum` - Can be used to define [the maximum value of a numeric instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.2).
- `exclusiveMaximum` - Can be used to define [the exclusiveMaximum value of a numeric instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.3).
- `minimum` - Can be used to define [the minimum value of a numeric instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.4).
- `exclusiveMinimum` - Can be used to define [the exclusiveMinimum value of a numeric instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.5).
For String instances:
- `maxLength` - Can be used to define [the maxLength value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.3.1). The value of this keyword MUST be a non-negative integer.
- `minLength` - Can be used to define [the minLength value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.3.2). The value of this keyword MUST be a non-negative integer.
- `pattern` - Can be used to define [the pattern value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.3.3).
For Array instances:
- `maxItems` - Can be used to define [the maxItems value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.1). The value of this keyword MUST be a non-negative integer.
- `minItems` - Can be used to define [the minItems value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.2). The value of this keyword MUST be a non-negative integer.
- `uniqueItems` - Can be used to define [the uniqueItems value of a string instance](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.3). The value of this keyword MUST be a boolean.
### Support for names and namespaces

@@ -157,4 +179,6 @@

> Since we support extra attributes on field, you can set `minimum` and `maximum` attribute on float and double types to display number limits.
### Hardcoded key and secret
This is not a limitation of this package per se but of the [JSON Reference RFC](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03). So far, you can only hardcode the values for `key` and `secret` on the `$ref` URL.

@@ -7,13 +7,16 @@ const fs = require('fs');

const inputWithAvro182 = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.8.2.yaml'), 'utf8');
const outputWithAvro182 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-4>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-5>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.8.2","x-parser-original-payload":{"type":"record","fields":[{"name":"name","type":"string","example":"Donkey"},{"name":"age","type":["null","int"],"default":null},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"]}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":"53003"}]}}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const outputWithAvro182 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","favoriteProgrammingLanguage","address"],"properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-4>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-5>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.8.2","x-parser-original-payload":{"type":"record","fields":[{"name":"name","type":"string","example":"Donkey"},{"name":"age","type":["null","int"],"default":null},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"]}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":"53003"}]}}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const inputWithAvro190 = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0.yaml'), 'utf8');
const outputWithAvro190 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const outputWithAvro190 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","favoriteProgrammingLanguage","address","someid"],"properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const inputWithAvro190WithNamespace = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-namespace.yaml'), 'utf8');
const outputWithAvro190ithNamespace = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"com.company.Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const outputWithAvro190WithNamespace = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","favoriteProgrammingLanguage","address","someid"],"properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"com.company.Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const inputWithAvro190WithBindings = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-bindings.yaml'), 'utf8');
const outputWithAvro190WithBindings = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"com.company.Person"},"bindings":{"kafka":{"key":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"]},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123]},{"type":"null"}],"default":null},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003]}}},"someid":{"type":"string"}},"x-parser-schema-id":"com.company.Person"}},"mqtt":{"x-test":{"type":"string"}}},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"x-parser-original-bindings-kafka-key":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const outputWithAvro190WithBindings = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","favoriteProgrammingLanguage","address","someid"],"properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-1>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-3>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-2>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-5>"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-6>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"com.company.Person"},"bindings":{"kafka":{"key":{"type":"object","required":["name","favoriteProgrammingLanguage","address","someid"],"properties":{"name":{"type":"string","examples":["Donkey"]},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123]},{"type":"null"}],"default":null},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003]}}},"someid":{"type":"string"}},"x-parser-schema-id":"com.company.Person"}},"mqtt":{"x-test":{"type":"string"}}},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"x-parser-original-bindings-kafka-key":{"name":"Person","namespace":"com.company","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":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
const inputWithAvroAdditionalAttributes = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-additional-attributes.yaml'), 'utf8');
const outputWithAvroAdditionalAttributes = '{"asyncapi":"2.1.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","serialNo","favoriteProgrammingLanguage","certifications","address","weight","height","someid"],"properties":{"name":{"type":"string","examples":["Donkey"],"minLength":0,"x-parser-schema-id":"<anonymous-schema-1>"},"serialNo":{"type":"string","minLength":0,"maxLength":50,"x-parser-schema-id":"<anonymous-schema-2>"},"email":{"oneOf":[{"type":"string","examples":["donkey@asyncapi.com"],"pattern":"^[\\\\w-\\\\.]+@([\\\\w-]+\\\\.)+[\\\\w-]{2,4}$","x-parser-schema-id":"<anonymous-schema-4>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-5>"}],"x-parser-schema-id":"<anonymous-schema-3>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"exclusiveMinimum":0,"exclusiveMaximum":200,"x-parser-schema-id":"<anonymous-schema-7>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-8>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-6>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-9>"},"certifications":{"type":"array","items":{"type":"string","x-parser-schema-id":"<anonymous-schema-11>"},"minItems":1,"maxItems":500,"uniqueItems":true,"x-parser-schema-id":"<anonymous-schema-10>"},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-13>"},"country":{"oneOf":[{"type":"string","x-parser-schema-id":"<anonymous-schema-15>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-16>"}],"x-parser-schema-id":"<anonymous-schema-14>"}},"x-parser-schema-id":"<anonymous-schema-12>"},"weight":{"type":"number","format":"float","examples":["65.0"],"minimum":0,"maximum":500,"x-parser-schema-id":"<anonymous-schema-17>"},"height":{"type":"number","format":"double","examples":["1.85"],"minimum":0,"maximum":3,"x-parser-schema-id":"<anonymous-schema-18>"},"someid":{"type":"string","format":"uuid","x-parser-schema-id":"<anonymous-schema-19>"}},"x-parser-schema-id":"com.company.Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","type":"record","fields":[{"name":"name","type":"string","example":"Donkey","minLength":0},{"name":"serialNo","type":"string","minLength":0,"maxLength":50},{"name":"email","type":["null","string"],"example":"donkey@asyncapi.com","pattern":"^[\\\\w-\\\\.]+@([\\\\w-]+\\\\.)+[\\\\w-]{2,4}$"},{"name":"age","type":["null","int"],"default":null,"example":"123","exclusiveMinimum":0,"exclusiveMaximum":200},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"],"default":"JS"}},{"name":"certifications","type":{"type":"array","items":"string","minItems":1,"maxItems":500,"uniqueItems":true}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":53003},{"name":"country","type":["null","string"]}]}},{"name":"weight","type":"float","example":"65.0","minimum":0,"maximum":500},{"name":"height","type":"double","example":"1.85","minimum":0,"maximum":3},{"name":"someid","type":"string","logicalType":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.1.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';
parser.registerSchemaParser(avroSchemaParser);

@@ -32,3 +35,3 @@

const result = await parser.parse(inputWithAvro190WithNamespace, { path: __filename });
expect(JSON.stringify(result.json())).toEqual(outputWithAvro190ithNamespace);
expect(JSON.stringify(result.json())).toEqual(outputWithAvro190WithNamespace);
});

@@ -39,2 +42,6 @@ it('should parse Avro schema in kafka bindings', async function() {

});
it('should handle additional Avro attributes like', async function() {
const result = await parser.parse(inputWithAvroAdditionalAttributes, { path: __filename });
expect(JSON.stringify(result.json())).toEqual(outputWithAvroAdditionalAttributes);
});
});

@@ -1,2 +0,2 @@

const { avroToJsonSchema } = require('../to-json-schema');
const {avroToJsonSchema} = require('../to-json-schema');

@@ -11,71 +11,84 @@ const BYTES_PATTERN = '^[\u0000-\u00ff]*$';

it('transforms null values', async function () {
const result = await avroToJsonSchema({ type: 'null' });
expect(result).toEqual({ type: 'null' });
const result = await avroToJsonSchema({type: 'null'});
expect(result).toEqual({type: 'null'});
});
it('transforms boolean values', async function () {
const result = await avroToJsonSchema({ type: 'boolean' });
expect(result).toEqual({ type: 'boolean' });
const result = await avroToJsonSchema({type: 'boolean'});
expect(result).toEqual({type: 'boolean'});
});
it('transforms int values', async function () {
const result = await avroToJsonSchema({ type: 'int' });
expect(result).toEqual({ type: 'integer', minimum: INT_MIN, maximum: INT_MAX });
const result = await avroToJsonSchema({type: 'int'});
expect(result).toEqual({type: 'integer', minimum: INT_MIN, maximum: INT_MAX});
});
it('transforms long values', async function () {
const result = await avroToJsonSchema({ type: 'long' });
expect(result).toEqual({ type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX });
const result = await avroToJsonSchema({type: 'long'});
expect(result).toEqual({type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX});
});
it('transforms float values', async function () {
const result = await avroToJsonSchema({ type: 'float' });
expect(result).toEqual({ type: 'number' });
const result = await avroToJsonSchema({type: 'float'});
expect(result).toEqual({type: 'number', format: 'float'});
});
it('transforms double values', async function () {
const result = await avroToJsonSchema({ type: 'double' });
expect(result).toEqual({ type: 'number' });
const result = await avroToJsonSchema({type: 'double'});
expect(result).toEqual({type: 'number', format: 'double'});
});
it('transforms bytes values', async function () {
const result = await avroToJsonSchema({ type: 'bytes' });
expect(result).toEqual({ type: 'string', pattern: BYTES_PATTERN });
const result = await avroToJsonSchema({type: 'bytes'});
expect(result).toEqual({type: 'string', pattern: BYTES_PATTERN});
});
it('transforms string values', async function () {
const result = await avroToJsonSchema({ type: 'string' });
expect(result).toEqual({ type: 'string' });
const result = await avroToJsonSchema({type: 'string'});
expect(result).toEqual({type: 'string'});
});
it('transforms uuid values', async function () {
const result = await avroToJsonSchema({ type: 'uuid' });
expect(result).toEqual({ type: 'string' });
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 });
const result = await avroToJsonSchema({type: 'fixed', size: 5});
expect(result).toEqual({type: 'string', pattern: BYTES_PATTERN, minLength: 5, maxLength: 5});
});
it('transforms union values', async function () {
const result = await avroToJsonSchema(['null', 'int']);
expect(result).toEqual({ oneOf: [{ type: 'integer', minimum: INT_MIN, maximum: INT_MAX }, { type: 'null' }] });
expect(result).toEqual({oneOf: [{type: 'integer', minimum: INT_MIN, maximum: INT_MAX}, {type: 'null'}]});
});
it('transforms map values', async function () {
const result = await avroToJsonSchema({ type: 'map', values: 'long' });
expect(result).toEqual({ type: 'object', additionalProperties: { type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX } });
const result = await avroToJsonSchema({type: 'map', values: 'long'});
expect(result).toEqual({
type: 'object',
additionalProperties: {type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX}
});
});
it('transforms array values', async function () {
const result = await avroToJsonSchema({ type: 'array', items: 'long' });
expect(result).toEqual({ type: 'array', items: { type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX } });
const result = await avroToJsonSchema({type: 'array', items: 'long'});
expect(result).toEqual({type: 'array', items: {type: 'integer', minimum: LONG_MIN, maximum: LONG_MAX}});
});
it('transforms enum values', async function () {
const result = await avroToJsonSchema({ type: 'enum', doc: 'My test enum', symbols: ['one', 'two', 'three'], default: 'one' });
expect(result).toEqual({ type: 'string', enum: ['one', 'two', 'three'], default: 'one', description: 'My test enum' });
const result = await avroToJsonSchema({
type: 'enum',
doc: 'My test enum',
symbols: ['one', 'two', 'three'],
default: 'one'
});
expect(result).toEqual({
type: 'string',
enum: ['one', 'two', 'three'],
default: 'one',
description: 'My test enum'
});
});
it('transforms record values', async function () {

@@ -87,4 +100,4 @@ const result = await avroToJsonSchema({

fields: [
{ name: 'key1', type: 'long', doc: 'Key1 docs' },
{ name: 'key2', type: 'string', default: 'value2', doc: 'Key2 docs' },
{name: 'key1', type: 'long', doc: 'Key1 docs'},
{name: 'key2', type: 'string', default: 'value2', doc: 'Key2 docs'},
]

@@ -96,2 +109,3 @@ });

description: 'My test record',
required: ['key1'],
properties: {

@@ -115,18 +129,132 @@ key1: {

expect(
await avroToJsonSchema({ type: 'int', default: 1 })
).toEqual({ type: 'integer', minimum: INT_MIN, maximum: INT_MAX, default: 1 });
await avroToJsonSchema({type: 'int', default: 1})
).toEqual({type: 'integer', minimum: INT_MIN, maximum: INT_MAX, default: 1});
expect(
await avroToJsonSchema({ type: 'record', fields: [{ name: 'field1', type: 'string', default: 'AsyncAPI rocks!' }] })
).toEqual({ type: 'object', properties: { field1: { type: 'string', default: 'AsyncAPI rocks!' } } });
await avroToJsonSchema({type: 'record', fields: [{name: 'field1', type: 'string', default: 'AsyncAPI rocks!'}]})
).toEqual({
type: 'object',
properties: {field1: {type: 'string', default: 'AsyncAPI rocks!'}}
});
});
it('treats array Avro documents as unions', async function () {
expect(
await avroToJsonSchema([{ type: 'int', default: 1 }, 'string'])
).toEqual({ oneOf: [
{ type: 'integer', minimum: INT_MIN, maximum: INT_MAX, default: 1 },
{ type: 'string' },
] });
await avroToJsonSchema([{type: 'int', default: 1}, 'string'])
).toEqual({
oneOf: [
{type: 'integer', minimum: INT_MIN, maximum: INT_MAX, default: 1},
{type: 'string'},
]
});
});
});
describe('supportExampleAttribute', function () {
it('transforms example on union values', async function () {
const result = await avroToJsonSchema({
type: 'record',
name: 'MyName',
fields: [
{name: 'example', type: ['null', 'int'], example: 3}
]
});
expect(result).toEqual({
type: 'object',
'x-parser-schema-id': 'MyName',
properties: {
example: {
oneOf: [{
type: 'integer',
minimum: INT_MIN,
maximum: INT_MAX,
examples: [3]
}, {
type: 'null'
}
]
}
}
});
});
});
describe('requiredAttributesMapping()', function () {
it('support required fields', async function () {
const result = await avroToJsonSchema({
type: 'record',
doc: 'My test record',
name: 'MyName',
fields: [
{name: 'required1', type: ['int', 'long']},
{name: 'required2', type: ['long']},
{name: 'notRequiredBecauseDefault', type: 'string', default: 'value2'},
{name: 'notRequiredBecauseUnionWithNull', type: ['null', 'string']},
]
});
expect(result).toMatchObject({
type: 'object',
'x-parser-schema-id': 'MyName',
description: 'My test record',
required: ['required1', 'required2']
});
});
});
describe('additionalAttributesMapping()', function () {
it('support minimum and maximum for float', async function () {
const result = await avroToJsonSchema({type: 'float', minimum: 0, maximum: 10});
expect(result).toEqual({type: 'number', format: 'float', minimum: 0, maximum: 10});
});
it('support exclusiveMinimum and exclusiveMaximum for float', async function () {
const result = await avroToJsonSchema({type: 'float', exclusiveMinimum: 0, exclusiveMaximum: 10});
expect(result).toEqual({type: 'number', format: 'float', exclusiveMinimum: 0, exclusiveMaximum: 10});
});
it('support minimum and maximum for double', async function () {
const result = await avroToJsonSchema({type: 'double', minimum: 0, maximum: 10});
expect(result).toEqual({type: 'number', format: 'double', minimum: 0, maximum: 10});
});
it('support exclusiveMinimum and exclusiveMaximum for double', async function () {
const result = await avroToJsonSchema({type: 'double', exclusiveMinimum: 0, exclusiveMaximum: 10});
expect(result).toMatchObject({type: 'number', format: 'double', exclusiveMinimum: 0, exclusiveMaximum: 10});
});
it('support minimum and maximum for long and int', async function () {
let result = await avroToJsonSchema({type: 'long', minimum: 0, maximum: 10});
expect(result).toEqual({type: 'integer', minimum: 0, maximum: 10});
result = await avroToJsonSchema({type: 'int', minimum: 0, maximum: 10});
expect(result).toEqual({type: 'integer', minimum: 0, maximum: 10});
});
it('long and int type support exclusiveMinimum and exclusiveMaximum', async function () {
let result = await avroToJsonSchema({type: 'long', exclusiveMinimum: 0, exclusiveMaximum: 10});
expect(result).toMatchObject({type: 'integer', exclusiveMinimum: 0, exclusiveMaximum: 10});
result = await avroToJsonSchema({type: 'int', exclusiveMinimum: 0, exclusiveMaximum: 10});
expect(result).toMatchObject({type: 'integer', exclusiveMinimum: 0, exclusiveMaximum: 10});
});
it('support pattern, minLength and maxLength for string', async function () {
const result = await avroToJsonSchema({type: 'string', pattern: '$pattern^', minLength: 1, maxLength: 10});
expect(result).toEqual({type: 'string', pattern: '$pattern^', minLength: 1, maxLength: 10});
});
it('handle non-negative integer value for minLength and maxLength', async function () {
const result = await avroToJsonSchema({type: 'string', minLength: -1, maxLength: -110});
expect(result).toEqual({type: 'string'});
});
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});
});
it('support uniqueItems for array', async function () {
const result = await avroToJsonSchema({type: 'array', items: 'long', uniqueItems: true});
expect(result).toMatchObject({type: 'array', items: {type: 'integer'}, uniqueItems: true});
});
});

@@ -48,6 +48,21 @@ const BYTES_PATTERN = '^[\u0000-\u00ff]*$';

const exampleAttributeMapping = (typeInput, example, jsonSchemaInput) => {
/**
* Enrich the parent's required attribute with the required record attributes
* @param fieldDefinition the actual field definition
* @param parentJsonSchema the parent json schema which contains the required property to enrich
* @param haveDefaultValue we assure that a required field does not have a default value
*/
const requiredAttributesMapping = (fieldDefinition, parentJsonSchema, haveDefaultValue) => {
const isUnionWithNull = Array.isArray(fieldDefinition.type) && fieldDefinition.type.includes('null');
// we assume that a union type without null and a field without default value is required
if (!isUnionWithNull && !haveDefaultValue) {
parentJsonSchema.required = parentJsonSchema.required || [];
parentJsonSchema.required.push(fieldDefinition.name);
}
};
function extractNonNullableTypeIfNeeded(typeInput, jsonSchemaInput) {
let type = typeInput;
let jsonSchema = jsonSchemaInput;
// Map example to first non-null type

@@ -59,3 +74,6 @@ if (Array.isArray(typeInput) && typeInput.length > 0) {

}
return {type, jsonSchema};
}
const exampleAttributeMapping = (type, example, jsonSchema) => {
if (example === undefined || jsonSchema.examples || Array.isArray(type)) return;

@@ -75,2 +93,40 @@

const additionalAttributesMapping = (typeInput, avroDefinition, jsonSchemaInput) => {
const __ret = extractNonNullableTypeIfNeeded(typeInput, jsonSchemaInput);
const type = __ret.type;
const jsonSchema = __ret.jsonSchema;
exampleAttributeMapping(type, avroDefinition.example, jsonSchema);
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': // int, long, float, and double must support the attributes bellow
case 'long':
case 'float':
case 'double':
setAdditionalAttribute('minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf');
break;
case 'string':
jsonSchema.format = avroDefinition.logicalType;
setAdditionalAttribute('pattern', 'minLength', 'maxLength');
break;
case 'array':
setAdditionalAttribute('minItems', 'maxItems', 'uniqueItems');
break;
default:
break;
}
};
async function convertAvroToJsonSchema(avroDefinition, isTopLevel) {

@@ -90,3 +146,2 @@ const jsonSchema = {};

}
if (nullDef) jsonSchema.oneOf.push(nullDef);

@@ -128,2 +183,6 @@

break;
case 'float': // float and double must support the format attribute from the avro type
case 'double':
jsonSchema.format = type;
break;
case 'record':

@@ -134,4 +193,5 @@ const propsMap = new Map();

requiredAttributesMapping(field, jsonSchema, field.default !== undefined);
commonAttributesMapping(field, def, false);
exampleAttributeMapping(field.type, field.example, def);
additionalAttributesMapping(field.type, field, def);

@@ -145,3 +205,3 @@ propsMap.set(field.name, def);

commonAttributesMapping(avroDefinition, jsonSchema, isTopLevel);
exampleAttributeMapping(type, avroDefinition.example, jsonSchema);
additionalAttributesMapping(type, avroDefinition, jsonSchema);

@@ -153,2 +213,2 @@ return jsonSchema;

return convertAvroToJsonSchema(avroDefinition, true);
};
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc