Deputy.Validation (Deputy v0.5.0)

Copy Markdown View Source

Shared input validation helpers used by resource modules before issuing API requests.

Summary

Functions

Verify that every key in required_fields is present in attrs with a non-nil, non-empty-string value.

Functions

required_fields(attrs, required_fields)

@spec required_fields(map(), [atom()]) ::
  :ok | {:error, Deputy.Error.ValidationError.t()}

Verify that every key in required_fields is present in attrs with a non-nil, non-empty-string value.

Accepts either atom or string keys in attrs and matches against atom field names. Returns :ok if every required field has a usable value, or {:error, %ValidationError{}} for the first missing or blank field.

Rejecting nil and "" pre-flight catches calls that would otherwise hit the Deputy API and fail server-side with less useful errors.

Examples

iex> Deputy.Validation.required_fields(%{name: "foo", id: 1}, [:name, :id])
:ok

iex> Deputy.Validation.required_fields(%{"name" => "foo"}, [:name])
:ok

iex> {:error, %Deputy.Error.ValidationError{field: :id}} =
...>   Deputy.Validation.required_fields(%{name: "foo"}, [:name, :id])

iex> {:error, %Deputy.Error.ValidationError{field: :name}} =
...>   Deputy.Validation.required_fields(%{name: nil}, [:name])

iex> {:error, %Deputy.Error.ValidationError{field: :name}} =
...>   Deputy.Validation.required_fields(%{name: ""}, [:name])