Normandy.Validate (normandy v0.2.0)
View SourceProvides changeset-style validation for schemas.
This module implements validation logic similar to Ecto changesets, allowing you to validate data before creating or updating structs.
Features
- Required field validation
- Type casting and validation
- Custom validation functions
- Error accumulation
- Change tracking
Example
changeset = Validate.validate(data, params, types, required: [:name, :email])
if changeset.valid? do
apply_changes(changeset)
else
handle_errors(changeset.errors)
end
Summary
Functions
Validates the given parameter is true.
Types
@type action() :: nil | :insert | :update | :delete | :replace | :ignore | atom()
@type data() :: map()
@type t() :: t(Normandy.Schema.t() | map() | nil)
@type t(data_type) :: %Normandy.Validate{ action: action(), changes: term(), constraints: [constraint()], data: data_type, errors: [{atom(), error()}], filters: %{optional(atom()) => term()}, params: %{optional(String.t()) => term()} | nil, prepare: [(t() -> t())], required: [atom()], types: types(), valid?: boolean(), validations: [validation()] }
@type traverse_result() :: %{required(atom()) => [term()] | traverse_result()}
@type types() :: %{ required(atom()) => Normandy.Type.t() | {:assoc, term()} | {:embed, term()} }
Functions
@spec apply_action(t(), action()) :: {:ok, Normandy.Schema.t() | data()} | {:error, t()}
@spec apply_action!(t(), action()) :: Normandy.Schema.t() | data()
@spec apply_changes(t()) :: Normandy.Schema.t() | data()
@spec empty_values() :: [empty_value()]
Validates the given parameter is true.
Note this validation only checks the parameter itself is true, never
the field in the schema. That's because acceptance parameters do not need
to be persisted, as by definition they would always be stored as true.
Options
:message- the message on failure, defaults to "must be accepted". Can also be a{msg, opts}tuple, to provide additional options when usingtraverse_errors/2.
Examples
validate_acceptance(changeset, :terms_of_service)
validate_acceptance(changeset, :rules, message: "please accept rules")