DEV Community

How to cast & validate map-typed Ecto schema field

Hoon Wee on March 01, 2024

Problem Let's go straight to the problem. Suppose you have a Post schema, defined as follows: # lib/my_app/post.ex defmodule MyApp.P...
Collapse
 
hackvan profile image
Diego Camacho

Great work!

Just I think it is necessary in the definition of the embedded schema to use embedded_schema instead :schema "locations":

defmodule MyApp.Location do
...
embedded_schema do
  field(:latitude, :float)
  field(:longitude, :float)
  field(:address, :string)
end
...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hoonweedev profile image
Hoon Wee

Thanks for pointing that out 👍

Actually the code will still work, but apparently your solution is actually more conventional way to do this (as mentioned in Ecto docs) - I'm editing the post with your solution.