1/20/2024 0 Comments Elixir ecto has already been takenHopefully, you've now got a few more tools in your belt to try out. I hope you've enjoyed these two blog posts covering how you can keep bad data out of your Elixir applications while also making your code more expressive.Īs we've seen, there are both "plain vanilla" Elixir techniques as well as support you can get from libraries like NimbleOptions to help us reject invalid data from being processed in our code.īy introducing these approaches to our modules, we'll provide more immediate feedback to callers and also make our programs more resilient when faced with unexpected data. We've now got the best of both worlds: convenient argument types and a standardized representation post-validation! Wrapping Up # in the Account module defstruct def from_params (% Other functions in the same module can then accept such a struct instance (rather than a map) to indicate that the data has been vetted and is safe to consume. We can expose a function that will accept a plain map and, after proper vetting, will yield a struct. This approach is typically helpful when dealing with untrusted input (e.g., from an API request or as part of a module's public API). They also prevent bad data from making its way into structs (this can happen when bad data makes it past constructors or is added directly to a struct). Users can have many resources they belong to, and. I am building a sort of one-stop shop for user accounts, where users can be created, and given permissions to one or more web sites (AKA resources) that I work on. The only difference is that the first one will only take in NaiveDateTimes and the second one will 1) assume that everything read from the DB is in UTC, and 2) only accept and read DateTimes. in PostgreSQL they will use timestamp (without timezone). Schemaless changesets (basically Ecto changesets that aren't tied to a database table) are a convenient way to create data structures. I’m relatively new to Elixir/Phoenix (I have much more familiarity with RoR) but I’ve made it quite far in my new application by following tutorials and the documentation. Both :naivedatetime and :utcdatetime will map to the same database type by default, e.g. These are already in your pipeline, and you have the indexes so you’re all set. The reason you still need uniqueconstraint/3 is for the case where records have been inserted between ecto checking the database and performing the insert. In the previous part of this series about validating data at the boundary of an Elixir application, we covered a few general programming tactics to try and reject invalid and unexpected data in our software.Ĭontinuing with that subject, we'll now explore how two libraries, namely Ecto and NimbleOptions, can further assist us.Īs we've seen previously, Elixir provides many native techniques to help us guarantee data quality in our systems.īut it's also possible to leverage Ecto to cast, validate, and prune data even if there's no database interaction. This will make ecto go to the database and verify the fields are unique before attempting the insert.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |