Ecto Custom types and casting issue - (Ecto.ChangeError) value “” for Schema.ip_addr in insert does not match type EctoNetwork.INET

I’m attempting to track down a problem with custom ecto types that I just realized I’ve been having for years.

I’ve used the EctoNetwork library to support inserting :inet data, but had since worked around an IP string not matching the custom type.

From everything I’ve read about custom types i’m lead to believe if I set a schema’s field type to a custom type, the custom type cast functions should be called. Even the readme for EctoNetwork states, add the library as the dep, set the schema field to EctoNetwork.INET, and set the migration to :inet. Period.

Now i’ve been casting inet manually for years because if I pass in a string (“”) I get ** (Ecto.ChangeError) value “” for Schema.ip_addr in insert does not match type EctoNetwork.INET`.

What gives? The custom type has a cast function that specifically handles binary data. It’s not called at all. I’ve even tried writing a custom type manually and cast is never called.

Am I misunderstanding the purpose of cast? Why is it required if it’s not called?

For me this library works as expected. Can you please share a minimal example code to reproduce your problem?

there is here a great code from dashbitco doing the job:


Interesting project, thanks for linking to it.

Okay I see the issue now. The field in question was getting piped in via put_change/3 after the fact, so it was bypassing cast altogether.

I’ll have to play around a bit more to see if I can’t pipe it in before, I was attempting to avoid sending it to cast directly since it’s not user supplied and i’m trying to keep those concerns separate.

I may just have to live with casting it manually!

TL:DR I was putting the square block in the circular hole.

Thanks guys!