Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate @derive Jason.Encoder options #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/encoder.ex
Original file line number Diff line number Diff line change
@@ -138,6 +138,7 @@ defimpl Jason.Encoder, for: Any do
end

defp fields_to_encode(struct, opts) do
Keyword.validate!(opts, [:only, :except])
fields = Map.keys(struct)

cond do
13 changes: 13 additions & 0 deletions test/encode_test.exs
Original file line number Diff line number Diff line change
@@ -190,6 +190,19 @@ defmodule Jason.EncoderTest do
end
end

test "@derive validate `invalid:`" do
message = "unknown keys [:invalid] in [invalid: [:__meta__]], the allowed keys are: [:only, :except]"

assert_raise ArgumentError, message, fn ->
Code.eval_string("""
defmodule InvalidOption do
@derive {Jason.Encoder, invalid: [:__meta__]}
defstruct name: "", size: 0
end
""")
end
end

defmodule KeywordTester do
defstruct [:baz, :foo, :quux]
end