Skip to content

Commit

Permalink
required fields from changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
urielaero committed Dec 14, 2015
1 parent 4c8cd64 commit 68538d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions lib/mix/tasks/swagger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ defmodule Mix.Tasks.Swagger do
end

module_json = %{"properties" => properties_json}

if :erlang.function_exported(module, :changeset, 2) do
module_struct = module.changeset(module.__struct__, %{})
required = required_fields module_struct.errors
module_json = Map.put(module_json, "required", required)
end

def_json = Map.put(def_json, "#{inspect module}", module_json)
end

Expand Down Expand Up @@ -273,4 +280,12 @@ defmodule Mix.Tasks.Swagger do
_ -> %{"type" => "string"}
end
end

def required_fields([]), do: []

def required_fields([head|tail]) do
{key, _msg} = head
[to_string(key)|required_fields(tail)]
end

end
22 changes: 17 additions & 5 deletions test/mix/tasks/swagger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,18 @@ defmodule Mix.Tasks.Swagger.Tests do

test "build_definitions - modules but no models" do
assert Swagger.build_definitions([{Mocks.DefaultPlug, ""}], %{}) == %{}
end
end

#==============================
# required_fields tests

test "required_fields - parse errors from struct, if errors is empty" do
assert Swagger.required_fields([]) == []
end

test "required_fields - parse errors from struct" do
assert Swagger.required_fields([name: "can't be blank", email: "can't be blank"]) == ["name", "email"]
end

test "build_definitions - model" do
assert Swagger.build_definitions([{Mocks.UserModel, ""}], %{}) == %{
Expand All @@ -376,16 +387,17 @@ defmodule Mix.Tasks.Swagger.Tests do
test "build_definitions - model support changeset (required_fields)" do
assert Swagger.build_definitions([{Mocks.UserRequiredModel, ""}], %{}) == %{
"Mocks.UserRequiredModel" => %{
#"required" => ["email", "name"],
"properties" => %{
"bio" => %{"type" => "string"},
"email" => %{"type" => "string"},
"id" => %{"format" => "int64", "type" => "integer"},
"inserted_at" => %{"format" => "date-time", "type" => "string"},
"name" => %{"type" => "string"},
"number_of_pets" => %{"format" => "int64", "type" => "integer"},
"updated_at" => %{"format" => "date-time", "type" => "string"}}
}
"updated_at" => %{"format" => "date-time", "type" => "string"}
},
"required" => ["name", "email"]
}
}
end

Expand Down Expand Up @@ -452,7 +464,7 @@ defmodule Mix.Tasks.Swagger.Tests do
test "run raise exception" do
:meck.new(Swagger, [:passthrough])
:meck.expect(Swagger, :get_router, fn _ -> Mocks.SimpleRouter end)
#:meck.expect(Swagger, :add_routes, fn _,_ -> raise "bad news bears" end)
:meck.expect(Swagger, :add_routes, fn _,_ -> raise "bad news bears" end)

assert Swagger.run(nil) == :ok
after
Expand Down

0 comments on commit 68538d8

Please sign in to comment.