ElixirExtensions.Ecto.ChangesetExt (elixir_extentions v0.1.1)
Extend Changeset with some useful functions. Add this line to your schema file:
defmodule MyApp.Accounts.User do use Ecto.Schema import Ecto.Changeset alias ElixirExtensions.Ecto.ChangesetExt ... end
Some extra validations for you to use:
trim(changeset, :field) trim(changeset, [:field1, :field2]) validate_email(changeset, :field) validate_url(changeset, field)
Summary
Functions
Trims the whitespace off both ends of the string. " John Doe " -> "John Doe"
Trim whitespace on either end of a string. Account for nil
Validate emails. Will check mx records in production (see the email_checker library).
Validate URLs. Adds https if a scheme doesn't exist. eg. "google.com" -> "https://google.com"
Functions
email_valid?(email)
ensure_trimmed(changeset, field)
Trims the whitespace off both ends of the string. " John Doe " -> "John Doe"
Examples:
defp changeset(user) do
user
|> cast([:first_name, :last_name, :email])
|> trim([:first_name, :last_name])
|> trim(:email)
end
trim(str)
Trim whitespace on either end of a string. Account for nil
validate_email(changeset, field)
Validate emails. Will check mx records in production (see the email_checker library).
Examples:
defp changeset(user) do
user
|> cast([:email])
|> validate_email(:email)
end
validate_url(changeset, field)
Validate URLs. Adds https if a scheme doesn't exist. eg. "google.com" -> "https://google.com"
Examples:
defp changeset(user) do
user
|> cast([:website])
|> validate_and_fix_url(:website)
end