FeaturevisorSchemas (featurevisor_schemas v2.1.0)

Copy Markdown View Source

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.

Summary

Types

t()

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

Functions

Removes the module from the Featurevisor instance.

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

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

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

Whether the datafile carries a schema.

The keys of the schemas the datafile carries, sorted.

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

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

Types

message()

@type message() ::
  String.t() | (map(), FeaturevisorSchemas.MessageInfo.t() -> String.t())

t()

@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.

Functions

close(featurevisor_schemas)

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

Removes the module from the Featurevisor instance.

create(featurevisor, options \\ [])

@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(issues)

@spec flatten([FeaturevisorSchemas.Issue.t()]) :: %{
  form_errors: [String.t()],
  field_errors: %{required(String.t()) => [String.t()]}
}

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

get(schemas, key, options \\ [])

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

has?(featurevisor_schemas, key)

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

Whether the datafile carries a schema.

keys(featurevisor_schemas)

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

The keys of the schemas the datafile carries, sorted.

valid?(schemas, key, value, options \\ [])

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

Whether a value passes.

validate(schemas, key, value, options \\ [])

@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!(schemas, key, value, options \\ [])

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

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