Skip to content

Instantly share code, notes, and snippets.

@ricn
Created September 7, 2015 21:01
Show Gist options
  • Save ricn/d018000fea48eb170cda to your computer and use it in GitHub Desktop.
Save ricn/d018000fea48eb170cda to your computer and use it in GitHub Desktop.
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