# `ASDF.Schema.Refs`
[🔗](https://github.com/baoulo/asdf-elixir/blob/v0.1.0/lib/asdf/schema/refs.ex#L1)

Resolves `$ref` inside the vendored schema set.

A seen-set stops a cycle. An `http` or `https` target that is not one of
the vendored ids is `reason: :remote_ref` and is not fetched. References
into unit, time, FITS, or WCS schemas are recorded as exclusions and
replaced with a schema that accepts nothing, so those branches are not
reported as valid.

# `detect`

```elixir
@spec detect(term(), MapSet.t(), (String.t() -&gt; term() | nil)) ::
  :ok | {:error, ASDF.Error.t()}
```

Follows absolute `$ref` values.

`schema` is a JSON Schema term. `seen` is a `MapSet` of ids already
entered. `fetch` is a one-argument function from id to schema, or `nil`
when the id is outside the set.

Returns `:ok`. Re-entering an id is `{:error, %ASDF.Error{reason: :cyclic_ref}}`.
A ref `fetch` does not know is `reason: :remote_ref`. A fragment ref
(`#...`) stays in the document and is not walked. Does not raise.

## Examples

    iex> ASDF.Schema.Refs.detect(%{"type" => "object"}, MapSet.new(), fn _id -> nil end)
    :ok

    iex> seen = MapSet.new(["http://example.test/a"])
    ...> {:error, error} = ASDF.Schema.Refs.detect(%{"$ref" => "http://example.test/a"}, seen, fn _ -> nil end)
    ...> error.reason
    :cyclic_ref

    iex> ref = %{"$ref" => "http://example.test/a"}
    ...> {:error, error} = ASDF.Schema.Refs.detect(ref, MapSet.new(), fn _ -> nil end)
    ...> error.reason
    :remote_ref

# `rewrite`

```elixir
@spec rewrite(term(), String.t(), MapSet.t()) ::
  {:ok, term(), [String.t()]} | {:error, ASDF.Error.t()}
```

Rewrites `$ref` values in `schema` against `ids`.

`schema` is a JSON Schema term. `base_id` is the URI of the document that
contains it. `ids` is a `MapSet` of vendored URIs. Relative refs are joined
onto `base_id`. A ref in `ids` is rewritten to that absolute URI. A fragment
(`#...`) is left in place. A ref whose URL contains `/unit/`, `/fits/`,
`/wcs/`, or `/time/` is removed and the node becomes `%{"not" => %{}}`.
Any other URL is `{:error, %ASDF.Error{reason: :remote_ref}}`.

Returns `{:ok, schema, excluded}` where `excluded` lists the replaced URLs.
Does not raise and does not fetch.

## Examples

    iex> ids = MapSet.new(["http://example.test/a"])
    ...> {:ok, schema, []} =
    ...>   ASDF.Schema.Refs.rewrite(%{"$ref" => "http://example.test/a"}, "http://example.test/a", ids)
    ...> schema
    %{"$ref" => "http://example.test/a"}

    iex> missing = %{"$ref" => "http://example.test/missing"}
    ...> {:error, error} = ASDF.Schema.Refs.rewrite(missing, "http://example.test/a", MapSet.new())
    ...> error.reason
    :remote_ref

    iex> unit = %{"$ref" => "http://example.test/unit/a"}
    ...> {:ok, schema, [url]} = ASDF.Schema.Refs.rewrite(unit, "http://example.test/a", MapSet.new())
    ...> {schema, String.contains?(url, "/unit/")}
    {%{"not" => %{}}, true}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
