Avro架构定义在传递两种可能的类型时抛出无效值类型错误



我试图在Avro模式中允许字段的类型字符串或类型null。下面是在执行时抛出相同错误的模式的简化。我被告知相同的模式在Python中工作而没有错误,所以我认为这个问题与Node.JS有关。我使用SWC来编译代码。如果需要更多的信息,请告诉我,谢谢。

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(".")}'`)
}
})

输出

'cat' is not a valid value (of type ["null","string"]) for 'animal'

包。json(略)

"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"
}

似乎avro-js几乎没有得到维护。有一个与你遇到的问题完全相关的开放问题。

我最终使用avsc代替,它工作得很好。

你必须告诉avro你传递的信息的类型,如果它不是空的,或者你有多个选项。

const testObject = {
"name": "Val",
"animal": {
"string": "cat"
}
}

相关内容

最新更新