# `FeaturevisorSchemas`
[🔗](https://github.com/featurevisor/featurevisor-elixir/blob/v2.1.0/lib/featurevisor_schemas.ex#L1)

Validates data against the schemas a Featurevisor datafile carries, with the
same rules, messages and conditions as `@featurevisor/schemas` in JavaScript.

    schemas = FeaturevisorSchemas.create(f)

    case FeaturevisorSchemas.validate(schemas, "signUp", params) do
      {:ok, params} -> create_account(params)
      {:error, issues} -> render_errors(FeaturevisorSchemas.flatten(issues))
    end

It holds no copy of the schemas: every call reads the current datafile, so a
new datafile takes effect at the next validation. The struct is a plain term,
safe to share between processes.

# `message`

```elixir
@type message() ::
  String.t() | (map(), FeaturevisorSchemas.MessageInfo.t() -&gt; String.t())
```

# `t`

```elixir
@type t() :: %FeaturevisorSchemas{
  messages: map(),
  store: struct(),
  translate: function() | nil
}
```

A Schemas instance. Read it through this module's functions, since its fields may change.

# `close`

```elixir
@spec close(t()) :: :ok
```

Removes the module from the Featurevisor instance.

# `create`

```elixir
@spec create(Featurevisor.t(), keyword() | map()) :: t()
```

Schemas for a Featurevisor instance, not a child. Pass a child's context to validate instead.

Options:

  * `:messages` - the application's messages by issue code, such as
    `%{"required" => "{title} is required"}`, text with placeholders or a
    function of the params and the `FeaturevisorSchemas.MessageInfo`.
  * `:translate` - a function of the issue and the `MessageInfo` that words
    every issue, returning nil to keep the chosen message.

# `flatten`

```elixir
@spec flatten([FeaturevisorSchemas.Issue.t()]) :: %{
  form_errors: [String.t()],
  field_errors: %{required(String.t()) =&gt; [String.t()]}
}
```

Issues' messages grouped by field, in the shape of Zod's flattenError:
`%{form_errors: [...], field_errors: %{"items.0.name" => [...]}}`.

# `get`

```elixir
@spec get(t(), String.t(), keyword()) :: FeaturevisorSchemas.Handle.t()
```

A schema's handle, or one with a form's messages by field path (`:messages`),
refinements (`:refine`) and option sources (`:option_sources`).

# `has?`

```elixir
@spec has?(t(), String.t()) :: boolean()
```

Whether the datafile carries a schema.

# `keys`

```elixir
@spec keys(t()) :: [String.t()]
```

The keys of the schemas the datafile carries, sorted.

# `valid?`

```elixir
@spec valid?(t(), String.t(), term(), keyword()) :: boolean()
```

Whether a value passes.

# `validate`

```elixir
@spec validate(t(), String.t(), term(), keyword()) ::
  {:ok, term()} | {:error, [FeaturevisorSchemas.Issue.t()]}
```

Validates a value: `{:ok, value}` when it passes, or `{:error, issues}`.

A `:context` given is merged over the instance's, for rules that change by segment.

# `validate!`

```elixir
@spec validate!(t(), String.t(), term(), keyword()) :: term()
```

The value when it passes, or raises `FeaturevisorSchemas.ValidationError` with the issues.

---

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