NLdoc.Spec.Asset (NLdoc.Spec v3.1.1)

View Source

This module defines the Ecto schema for the NLdoc spec Asset object.

Summary

Functions

Converts base64 to Asset resource.

This function creates a new NLdoc.Spec.Asset struct from a map of keys and values, where the keys are snake_case. Returns a tuple with either {:ok, asset} or {:error, changeset}.

This function creates a new NLdoc.Spec.Asset struct from a map of keys and values, where the keys are snake_case. Returns the asset} object or raises an Ecto.CastError.

Converts Asset resource to base64.

This validation function can be used in changeset/2 implementations to ensure that the value for a specific field is a valid URL.

Types

t()

@type t() :: %NLdoc.Spec.Asset{
  content_type: String.t(),
  data: String.t(),
  descriptors: [NLdoc.Spec.descriptor()],
  encoding: :base64,
  id: Ecto.UUID.t(),
  type: String.t()
}

validate_url_opt()

@type validate_url_opt() :: {:message, String.t()} | {:allow_relative, boolean()}

Functions

changeset(object, attrs)

@spec changeset(
  struct(),
  map()
) :: Ecto.Changeset.t()

from_base64(id, arg)

@spec from_base64(id :: String.t(), String.t()) :: t()

Converts base64 to Asset resource.

Examples

iex> NLdoc.Spec.Asset.from_base64("123", "data:image/gif;base64,R0lGODlhAQABAAAAACw=")
%NLdoc.Spec.Asset{
  id: "123",
  encoding: :base64,
  content_type: "image/gif",
  data: "R0lGODlhAQABAAAAACw="
}

new(template \\ %{type: "https://spec.nldoc.nl/Resource/Asset"})

@spec new(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

This function creates a new NLdoc.Spec.Asset struct from a map of keys and values, where the keys are snake_case. Returns a tuple with either {:ok, asset} or {:error, changeset}.

Note: If you want to use a map with camelCase keys, use NLdoc.Util.Recase.to_snake/1 to convert them to snake_case before passing them to the constructor.

new!(template \\ %{type: "https://spec.nldoc.nl/Resource/Asset"})

@spec new!(map()) :: t()

This function creates a new NLdoc.Spec.Asset struct from a map of keys and values, where the keys are snake_case. Returns the asset} object or raises an Ecto.CastError.

Note: If you want to use a map with camelCase keys, use NLdoc.Util.Recase.to_snake/1 to convert them to snake_case before passing them to the constructor.

resource_type()

to_base64(asset)

@spec to_base64(asset :: t()) :: String.t()

Converts Asset resource to base64.

Examples

iex> NLdoc.Spec.Asset.to_base64(%NLdoc.Spec.Asset{
...>   id: "123",
...>   encoding: :base64,
...>   content_type: "image/gif",
...>   data: "R0lGODlhAQABAAAAACw="
...> })
"data:image/gif;base64,R0lGODlhAQABAAAAACw="

iex> NLdoc.Spec.Asset.to_base64(%NLdoc.Spec.Asset{
...>   id: "123",
...>   content_type: "image/gif",
...>   encoding: nil,
...>   data: <<71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 0, 0, 0, 44>>
...> })
"data:image/gif;base64,R0lGODlhAQABAAAAACw="

validate_url(changeset, field, opts \\ [])

@spec validate_url(Ecto.Changeset.t(), atom(), [validate_url_opt()]) ::
  Ecto.Changeset.t()

This validation function can be used in changeset/2 implementations to ensure that the value for a specific field is a valid URL.

Options

  • :message - The error message to use when the URL is invalid.
  • :allow_relative - If set to true, relative URLs are allowed. Default is false.