Hash type that symbolize keys



I have a struct with an attribute the only thing I know about it is that it is going to be a Hash with string/symbol keys. I would like to coerce all its keys to symbols on initialization.

As far as I see, right now it is not possible to do that with dry-types. Types::Coercible::Hash has nothing to do with coercing string keys to symbol, while there is no hash schema suited for this scenario. The only one that symbolizes keys is Types::Hash.symbolized, but it ignores keys not defined in the schema, and I don’t know which keys are going to be present.

Am I missing something? If not, would you consider adding this to dry-types? If so, how do you think it should be added? As an option to Types::Hash? As a new schema type?

Thanks in advance


Hmm, well, giving a second thought, maybe it doesn’t make much sense to symbolize keys without an explicit schema… Maybe it makes sense if you don’t expect nested hashes to also be symbolized, but it is not my case. My hash could have nested hashes as values, and having to iterate through all of them would be quite rough…


Both validation and types require explicit attribute definitions, so we’ll never have a feature which blindly coerces everything it received. I know folks have use cases where a hash may contain partly known keys, so we’ll add a feature where you can check if input keys match certain criteria (ie all strings matching specific regex etc.).

For now you have to specify all keys explicitly.


Oks, thanks for your answer :slight_smile: