From d525a4296d89b15d5f0ddcdf79bc94ee375ffe3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Menou?= <frederic.menou@transport.data.gouv.fr> Date: Wed, 11 Sep 2024 17:43:08 +0200 Subject: [PATCH 1/3] Support des erreurs sans code --- .../lib/validators/netex_validator.ex | 39 ++++++++++++------- .../gettext/en/LC_MESSAGES/netex-validator.po | 4 ++ .../gettext/fr/LC_MESSAGES/netex-validator.po | 4 ++ .../priv/gettext/netex-validator.pot | 4 ++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/apps/transport/lib/validators/netex_validator.ex b/apps/transport/lib/validators/netex_validator.ex index 7fceddbd98..87d3fe20ef 100644 --- a/apps/transport/lib/validators/netex_validator.ex +++ b/apps/transport/lib/validators/netex_validator.ex @@ -249,10 +249,15 @@ defmodule Transport.Validators.NeTEx do iex> index_messages([%{"code"=>"a", "id"=> 1}, %{"code"=>"a", "id"=> 2}, %{"code"=>"b", "id"=> 3}]) %{"a"=>[%{"code"=>"a", "id"=> 1}, %{"code"=>"a", "id"=> 2}], "b"=>[%{"code"=>"b", "id"=> 3}]} + + Sometimes the message has no code + iex> index_messages([%{"code"=>"a", "id"=> 1}, %{"code"=>"b", "id"=> 2}, %{"id"=> 3}]) + %{"a"=>[%{"code"=>"a", "id"=> 1}], "b"=>[%{"code"=>"b", "id"=> 2}], "unknown-code"=>[%{"id"=> 3}]} """ - def index_messages(messages) do - messages |> Enum.group_by(fn %{"code" => code} -> code end) - end + def index_messages(messages), do: Enum.group_by(messages, &get_code/1) + + defp get_code(%{"code" => code}), do: code + defp get_code(%{}), do: "unknown-code" # This will change with an actual versioning of the validator def validator_version, do: "saas-production" @@ -298,7 +303,8 @@ defmodule Transport.Validators.NeTEx do "longitude-mandatory" => dgettext("netex-validator", "Longitude mandatory"), "uic-operating-period" => dgettext("netex-validator", "UIC operating period"), "valid-day-bits" => dgettext("netex-validator", "Valid day bits"), - "version-any" => dgettext("netex-validator", "Version any") + "version-any" => dgettext("netex-validator", "Version any"), + "unknown-code" => dgettext("netex-validator", "Unspecified error") } @doc """ @@ -347,17 +353,20 @@ defmodule Transport.Validators.NeTEx do defp demote_non_xsd_errors(errors), do: Enum.map(errors, &demote_non_xsd_error(&1)) - defp demote_non_xsd_error(%{"criticity" => criticity, "code" => code} = error) do - criticity = - if String.starts_with?(code, "xsd-") do - criticity - else - case criticity do - "error" -> "warning" - _ -> criticity - end - end + defp demote_non_xsd_error(error) do + code = Map.get(error, "code", "") - Map.update!(error, "criticity", fn _ -> criticity end) + if String.starts_with?(code, "xsd-") do + error + else + Map.update!(error, "criticity", &demote/1) + end + end + + defp demote(criticity) do + case criticity do + "error" -> "warning" + _ -> criticity + end end end diff --git a/apps/transport/priv/gettext/en/LC_MESSAGES/netex-validator.po b/apps/transport/priv/gettext/en/LC_MESSAGES/netex-validator.po index 8fefa80452..9d2dc078e6 100644 --- a/apps/transport/priv/gettext/en/LC_MESSAGES/netex-validator.po +++ b/apps/transport/priv/gettext/en/LC_MESSAGES/netex-validator.po @@ -66,3 +66,7 @@ msgstr "" #, elixir-autogen, elixir-format msgid "warnings" msgstr "" + +#, elixir-autogen, elixir-format +msgid "Unspecified error" +msgstr "" diff --git a/apps/transport/priv/gettext/fr/LC_MESSAGES/netex-validator.po b/apps/transport/priv/gettext/fr/LC_MESSAGES/netex-validator.po index 1886f4e5d8..7b30b0cebd 100644 --- a/apps/transport/priv/gettext/fr/LC_MESSAGES/netex-validator.po +++ b/apps/transport/priv/gettext/fr/LC_MESSAGES/netex-validator.po @@ -66,3 +66,7 @@ msgstr "" #, elixir-autogen, elixir-format msgid "warnings" msgstr "avertissements" + +#, elixir-autogen, elixir-format +msgid "Unspecified error" +msgstr "" diff --git a/apps/transport/priv/gettext/netex-validator.pot b/apps/transport/priv/gettext/netex-validator.pot index 2566320ef1..82d778082e 100644 --- a/apps/transport/priv/gettext/netex-validator.pot +++ b/apps/transport/priv/gettext/netex-validator.pot @@ -66,3 +66,7 @@ msgstr "" #, elixir-autogen, elixir-format msgid "warnings" msgstr "" + +#, elixir-autogen, elixir-format +msgid "Unspecified error" +msgstr "" From 1fcf7603170135c542484984c6a2b972c6292597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Menou?= <frederic.menou@transport.data.gouv.fr> Date: Wed, 11 Sep 2024 18:13:21 +0200 Subject: [PATCH 2/3] Missing test case --- .../validators/netex_validator_test.exs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/transport/test/transport/validators/netex_validator_test.exs b/apps/transport/test/transport/validators/netex_validator_test.exs index 20312562b5..70714a1755 100644 --- a/apps/transport/test/transport/validators/netex_validator_test.exs +++ b/apps/transport/test/transport/validators/netex_validator_test.exs @@ -35,6 +35,10 @@ defmodule Transport.Validators.NeTExTest do "code" => "frame-arret-resources", "message" => "Tag frame_id doesn't match ''", "criticity" => "warning" + }, + %{ + "message" => "Reference MOBIITI:Quay:104325 doesn't match any existing Resource", + "criticity" => "error" } ] @@ -104,6 +108,12 @@ defmodule Transport.Validators.NeTExTest do "message" => "Tag frame_id doesn't match ''", "criticity" => "warning" } + ], + "unknown-code" => [ + %{ + "message" => "Reference MOBIITI:Quay:104325 doesn't match any existing Resource", + "criticity" => "warning" + } ] } end @@ -163,6 +173,12 @@ defmodule Transport.Validators.NeTExTest do "message" => "Tag frame_id doesn't match ''", "criticity" => "warning" } + ], + "unknown-code" => [ + %{ + "message" => "Reference MOBIITI:Quay:104325 doesn't match any existing Resource", + "criticity" => "warning" + } ] } From 4c9df26afd9cf5aa2174db99651942f71cf77b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Menou?= <frederic.menou@transport.data.gouv.fr> Date: Wed, 11 Sep 2024 18:13:41 +0200 Subject: [PATCH 3/3] Better function name --- apps/transport/lib/validators/netex_validator.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/transport/lib/validators/netex_validator.ex b/apps/transport/lib/validators/netex_validator.ex index 87d3fe20ef..9e9d05daf1 100644 --- a/apps/transport/lib/validators/netex_validator.ex +++ b/apps/transport/lib/validators/netex_validator.ex @@ -359,11 +359,11 @@ defmodule Transport.Validators.NeTEx do if String.starts_with?(code, "xsd-") do error else - Map.update!(error, "criticity", &demote/1) + Map.update!(error, "criticity", &demote_error/1) end end - defp demote(criticity) do + defp demote_error(criticity) do case criticity do "error" -> "warning" _ -> criticity