I am trying to allow for either type string or type null for a field in an Avro schema. Below is a simplification of the schema which throws the same error when executed. I am told the same schema works in Python without error so I am led to believe this issue is pertaining to Node.JS. I am using SWC to compile the code. Please let me know if any more information would be of use, thanks.
testSchema.js
export default {
name: 'Pet',
type: 'record',
fields: [
{name: 'name', type: 'string'},
{name: 'animal', type: ['null', 'string']}
]
}
index.js
import avro from 'avro-js'
import testSchema from './testSchema.js'
const testSchemaAvro = avro.parse(testSchema)
const testObject = {"name": "Val", "animal": "cat"}
testSchemaAvro.isValid(testObject, {
errorHook(path, any, type) {
console.error(`'${any}' is not a valid value (of type ${type}) for '${path.join(".")}'`)
}
})
Output
'cat' is not a valid value (of type ["null","string"]) for 'animal'
package.json (Abbreviated)
"scripts": {
"build": "swc src -d dist",
"build-dev": "npm run build && node --inspect dist/index.js",
"dev": "nodemon --exec \"npm run build-dev\"",
"start": "npm run build && node dist/index.js"
},
"devDependencies": {
"@swc/cli": "^0.1.57",
"@swc/core": "^1.2.181"
},
"dependencies": {
"avro-js": "^1.11.0",
"aws-sdk": "^2.1131.0",
"axios": "^0.27.2",
"kafkajs": "^2.1.0",
"pg": "^8.7.3"
}
It seems that avro-js is barely maintained. There's an open issue related exactly to the problem you encountered.
I ended up using avsc instead, it works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With