Skip to content

Instantly share code, notes, and snippets.

@ricn

ricn/bookmark.ex

Created Sep 7, 2015
Embed
What would you like to do?
defmodule Bookmarks.Bookmark do
use Bookmarks.Web, :model
schema "bookmarks" do
field :href, :string
field :title, :string
field :description, :string
timestamps
end
@required_fields ~w(href)
@optional_fields ~w(title description)
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_length(:title, max: 255)
end
end
## test
test "title cannot be larger than 255 chars" do
long_title = String.duplicate("a", 280)
changeset = Bookmark.changeset(%Bookmark{href: "http://www.xyz.com", title: long_title})
refute changeset.valid?
IO.inspect(changeset)
assert changeset.errors == [title: "too long"] ## I know this will fail but I did not expect changeset.errors => []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment