mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

can schema support dynamic keys? #443

Closed suguanYang closed 11 months ago

suguanYang commented 11 months 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 11 months 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.