NLdoc.Spec.Template (NLdoc.Spec v3.1.1)

View Source

This module is a template for creating new NLdoc spec resource schemas.

To create a new resource schema:

  1. Copy this file to a new file in the same directory, renaming it to the name of the new resource in snake_case.
  2. Replace all occurrences of Template in that file with the name of the new resource.
  3. Add the fields for this resource to the typed_embedded_schema.
  4. Update the changeset function to cast and validate the new fields.
  5. Add the new resource type to the NLdoc.Spec.Type enum.
  6. Add the t type to the NLdoc.Spec.object type.
  7. Remove this section of the moduledoc up to and including ---.
  8. Now you can use the new resource in other schemas, e.g. add it to the possible children of NLdoc.Spec.Document.

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

Summary

Functions

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

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

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.Template{
  descriptors: [NLdoc.Spec.descriptor()],
  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()

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

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

This function creates a new NLdoc.Spec.Template struct from a map of keys and values, where the keys are snake_case. Returns a tuple with either {:ok, template} 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/Template"})

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

This function creates a new NLdoc.Spec.Template struct from a map of keys and values, where the keys are snake_case. Returns the template} 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()

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.