Skip to content

Commit

Permalink
added XML check to HTTP and Auth Step (#1629)
Browse files Browse the repository at this point in the history
* added XML check to HTTP and Auth Step

* updated blorb

* version bump and format
  • Loading branch information
nicholas-goodwin authored Mar 17, 2023
1 parent 06e2cdf commit fa2fe39
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,17 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStep do

defp validate_body_format(%{changes: %{body: body}} = changeset) do
case Jason.decode(body) do
{:ok, _} -> changeset
{:error, _} -> Changeset.add_error(changeset, :body, "could not parse json", validation: :format)
{:ok, _} ->
changeset

{:error, _} ->
try do
SweetXml.parse(body)
changeset
catch
:exit, _ ->
Changeset.add_error(changeset, :body, "could not parse json or xml", validation: :format)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStep do
alias Andi.InputSchemas.Ingestions.ExtractHeader
alias Andi.InputSchemas.StructTools
alias Ecto.Changeset
alias SweetXml

@primary_key false
embedded_schema do
Expand Down Expand Up @@ -95,8 +96,17 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStep do

defp validate_body_format(%{changes: %{body: body}} = changeset) do
case Jason.decode(body) do
{:ok, _} -> changeset
{:error, _} -> Changeset.add_error(changeset, :body, "could not parse json", validation: :format)
{:ok, _} ->
changeset

{:error, _} ->
try do
SweetXml.parse(body)
changeset
catch
:exit, _ ->
Changeset.add_error(changeset, :body, "could not parse json or xml", validation: :format)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion apps/andi/lib/andi_web/views/error_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule AndiWeb.ErrorHelpers do
defp interpret_error_message(message, :datasetLink, _), do: message
defp interpret_error_message("is required", field, _), do: default_error_message(field)
defp interpret_error_message(message, :format, _), do: "Error: " <> get_format_error_message(message)
defp interpret_error_message(_message, :body, _), do: "Please enter valid JSON"
defp interpret_error_message(_message, :body, _), do: "Please enter valid JSON or XML"

defp interpret_error_message(message, field, _) when field in [:topLevelSelector, :cadence, :dataName, :license, :orgName],
do: "Error: #{message}"
Expand Down
2 changes: 1 addition & 1 deletion apps/andi/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Andi.MixProject do
def project do
[
app: :andi,
version: "2.5.68",
version: "2.5.69",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStepTest do
[nil],
["[]"],
["[{}]"],
["[{\"bob\": 1}]"]
["[{\"bob\": 1}]"],
["<note><to>bob</to><heading>Reminder</heading></note>"]
])
end

Expand All @@ -56,7 +57,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStepTest do
ExtractAuthStep.changeset(ExtractAuthStep.get_module(), changes)
|> ExtractAuthStep.validate()

assert changeset.errors[:body] == {"could not parse json", [validation: :format]}
assert changeset.errors[:body] == {"could not parse json or xml", [validation: :format]}

where([
[:value],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStepTest do
[nil],
["[]"],
["[{}]"],
["[{\"bob\": 1}]"]
["[{\"bob\": 1}]"],
["<note><to>bob</to><heading>Reminder</heading></note>"]
])
end

Expand All @@ -46,7 +47,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStepTest do
ExtractHttpStep.changeset(ExtractHttpStep.get_module(), changes)
|> ExtractHttpStep.validate()

assert changeset.errors[:body] == {"could not parse json", [validation: :format]}
assert changeset.errors[:body] == {"could not parse json or xml", [validation: :format]}

where([
[:value],
Expand Down

0 comments on commit fa2fe39

Please sign in to comment.