Arc.Validation (cratis_arc v0.1.0)

Copy Markdown View Source

Builds validation results and evaluates declarative field rules.

Field rules come from field/3 options in a schema block (required, min_length, max_length, email, matches, min, max, greater_than, less_than). The same rules are written into the generated TypeScript validators, so the browser rejects what the server would reject.

A command can add cross-field rules by defining validate/2, returning :ok or {:error, results} built with error/2.

Summary

Functions

Builds an error result for the given members.

The client-visible rule descriptors for a field's options, as %{name: "minLength", args: [3]}.

Evaluates the declarative rules of every field in fields against struct.

Functions

error(message, members \\ [], opts \\ [])

@spec error(String.t(), [atom() | String.t()], keyword()) :: Arc.ValidationResult.t()

Builds an error result for the given members.

Arc.Validation.error("Already registered", [:name])

rules(opts)

@spec rules(keyword()) :: [map()]

The client-visible rule descriptors for a field's options, as %{name: "minLength", args: [3]}.

validate_fields(struct, fields)

@spec validate_fields(struct(), [Arc.Schema.Field.t()]) :: [Arc.ValidationResult.t()]

Evaluates the declarative rules of every field in fields against struct.

warning(message, members \\ [], opts \\ [])

@spec warning(String.t(), [atom() | String.t()], keyword()) ::
  Arc.ValidationResult.t()

Builds a warning result.