mtth / avsc

Avro for JavaScript :zap:
MIT License
1.29k stars 148 forks source link

can schema support dynamic keys? #443

Closed suguanYang closed 1 year ago

suguanYang commented 1 year ago

I have a data object like this:

const person = {
    name: 'John',
    age: 30,
    address1: 'xxxx',
    address2: 'xxxx',
    address3: 'xxxx',
}

the question is how to support the fields address${number} in schema definition?

const type = avsc.Type.forSchema({
    name: 'PersonList',
    type: 'array',
    items: {
        name: 'Person',
        type: 'record',
        fields: [
            { name: 'name', type: 'string' },
            { name: 'age', type: 'int' },
        ]
    }
});
mtth commented 1 year ago

Hi @suguanYang. AFAIK Avro doesn't allow arbitrary keys in records so you'll need to use a map. If possible, I'd recommend splitting the addresses into their own map so you can tighten the values' type.