~ihabunek/triglav

bd6c25c19250e4bfa9daebdeaf62269a2a5b90fa — Ivan Habunek 8 months ago 21a975e
Validate relation member roles
3 files changed, 54 insertions(+), 1 deletions(-)

M lib/triglav/schemas/error.ex
M lib/triglav/zet/errors.ex
M lib/triglav/zet/validator.ex
M lib/triglav/schemas/error.ex => lib/triglav/schemas/error.ex +14 -0
@@ 4,6 4,7 @@ defmodule Triglav.Schemas.Error do

  alias Triglav.Schemas.Zet.Route
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Schemas.Osmosis.RelationMember

  @type t :: %__MODULE__{}



@@ 148,4 149,17 @@ defmodule Triglav.Schemas.Error do
      relation_id: relation.id,
      params: %{way_id: way_id}
    }

  @spec unexpected_member_role(Route.t(), Relation.t(), RelationMember.t()) :: Error.t()
  def unexpected_member_role(
        %Route{} = route,
        %Relation{} = relation,
        %RelationMember{} = member
      ),
      do: %__MODULE__{
        key: "unexpected_member_role",
        route_id: route.id,
        relation_id: relation.id,
        params: Map.take(member, [:member_id, :member_role, :member_type])
      }
end

M lib/triglav/zet/errors.ex => lib/triglav/zet/errors.ex +11 -0
@@ 73,6 73,17 @@ defmodule Triglav.Zet.Errors do
    "Broken route starting at way ##{way_id}"
  end

  def render(%Error{
        key: "unexpected_member_role",
        params: %{
          "member_role" => member_role,
          "member_type" => member_type,
          "member_id" => member_id
        }
      }) do
    "Unexpected role \"#{member_role}\" on relation member #{member_type}#{member_id}"
  end

  def render(%Error{key: key}) do
    "FIXME: missing render function for error '#{key}'"
  end

M lib/triglav/zet/validator.ex => lib/triglav/zet/validator.ex +29 -1
@@ 147,7 147,8 @@ defmodule Triglav.Zet.Validator do
      validate_allowed_tags(route, relation, allowed_tags),
      validate_tag_values(route, relation, expected_tags),
      validate_continuous_ways(route, relation, ways),
      validate_has_gtfs_stop_ids(route, relation, relation_stop_ids)
      validate_has_gtfs_stop_ids(route, relation, relation_stop_ids),
      validate_member_roles(route, relation)
    ]
  end



@@ 213,6 214,33 @@ defmodule Triglav.Zet.Validator do
    end
  end

  defp validate_member_roles(route, relation) do
    expected_node_roles = [
      "platform",
      "platform_entry_only",
      "platform_exit_only",
      "stop",
      "stop_entry_only",
      "stop_exit_only"
    ]

    expected_way_roles = [
      "",
      "platform",
      "platform_entry_only",
      "platform_exit_only"
    ]

    if Relation.is_ptv2(relation) do
      for member <- relation.members do
        if (member.member_type == "N" and member.member_role not in expected_node_roles) or
             (member.member_type == "W" and member.member_role not in expected_way_roles) do
          Error.unexpected_member_role(route, relation, member)
        end
      end
    end
  end

  #
  # Generic relation validators
  #