Skip to content

Coerce and validate dirty values into clean Elixir primitives

License

Notifications You must be signed in to change notification settings

moxley/coercion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coercion

Coerce dirty values to clean Elixir primitive types, with validation.

Build Status

import Coercion

{:ok, 20} = coerce(" 20 ", :integer)
{:invalid, 0} = coerce("  x", :integer)

{:ok, true} = coerce(" TRue ", :boolean)
{:ok, true} = coerce(" T ", :boolean)
{:ok, true} = coerce(" Y ", :boolean)
{:ok, true} = coerce(" Yes ", :boolean)
{:ok, true} = coerce(" 1 ", :boolean)
{:invalid, false} = coerce(" TRu ", :boolean)
{:ok, false} = coerce(" F ", :boolean)
{:ok, false} = coerce(" N ", :boolean)
{:ok, false} = coerce(" 0 ", :boolean)

{:ok, "hello"} = coerce(" hello ", :string)
{:blank, ""} = coerce("  ", :string)
{:ok, "true"} = coerce(true, :string)
{:ok, "10.5"} = coerce(10.5, :string)

{:ok, %{"one" => "one"}} == coerce(%{"one" => "one"}, :map)
{:invalid, %{}} == coerce("nope", :map)
{:ok, ["one", "two"]} == coerce(["one", "two"], :list)
{:invalid, []} == coerce("nope", :list)

{:ok, ~D[2020-04-02]} = coerce("2020-04-02", :date)
{:ok, ~U[2020-04-02 12:00:01Z]} = coerce("2020-04-02T12:00:01Z", :utc_datetime)
{:ok, ~N[2020-04-02 12:00:01]} = coerce("2020-04-02T12:00:01Z", :naive_datetime)

The primary use case is decoding values that come from external sources where everything is just a String.

Example sources:

  • URL-encoded values
    name=Kate&age=40&subscribed=Y
    
  • CSV
    name,age,subscribed
    Kate,40,T
    
  • XML
    <row>
      <name>Kate</name>
      <age>40</age>
      <subscribed>Yes</subscribed>
    </row>

Ecto.Schema is often a good fit for this type of problem, but sometimes it's overkill.

Installation

The package can be installed by adding coercion to your list of dependencies in mix.exs:

def deps do
  [{:coercion, "~> 1.2"}]
end

Then, run mix deps.get, and you're ready to go.

API Documentation: https://hexdocs.pm/coercion.

License: https://github.com/moxley/coercion/blob/master/LICENSE

About

Coerce and validate dirty values into clean Elixir primitives

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages