Current section
Files
Jump to
Current section
Files
lib/regc/oci/image/media_validation.ex
defmodule Regc.Oci.Image.MediaValidation do
@moduledoc false
alias Regc.Oci.{Error, Manifest, MediaType, Reference}
@spec validate(Manifest.t() | nil, Reference.t() | nil) ::
:ok | {:error, Error.t()}
def validate(nil, nil), do: :ok
def validate(%Manifest{kind: :image} = manifest, %Reference{} = reference) do
expected = MediaType.image_family!(manifest.descriptor.media_type)
with :ok <- validate_config(manifest, expected.config, reference),
:ok <- validate_layers(manifest, expected.layers, reference) do
:ok
end
end
defp validate_config(manifest, expected, reference) do
if manifest.config.media_type == expected do
:ok
else
Error.result(
:config_decode,
:unsupported_media_type,
"image config media type is not supported",
reference: reference,
details: %{
media_type: manifest.config.media_type,
expected: expected,
manifest_media_type: manifest.descriptor.media_type
}
)
end
end
defp validate_layers(manifest, supported, reference) do
case Enum.find_index(manifest.layers, &(&1.media_type not in supported)) do
nil ->
:ok
index ->
descriptor = Enum.at(manifest.layers, index)
Error.result(
:manifest_validate,
:unsupported_media_type,
"image layer media type is not supported",
reference: reference,
details: %{
field: {:layers, index},
media_type: descriptor.media_type,
manifest_media_type: manifest.descriptor.media_type
}
)
end
end
end