DryValidation (dry_validation v1.0.1)

Used to create a schema to validate input data against.

Example

iex> alias DryValidation.Types
...>
iex> schema = DryValidation.schema do
...>  required :name, Types.String
...>  optional :age, Types.Integer
...> end
...>
iex> input_data = %{"name" => "John", "age" => "15"}
iex> {:ok, output_data} = DryValidation.Validator.validate(schema, input_data)
iex> assert output_data == %{"name" => "John", "age" => 15}
...>
iex> input_data = %{"name" => 15, "age" => "nonsense"}
iex> {:error, error} = DryValidation.Validator.validate(schema, input_data)
iex> assert error == %{
...>  "name" => "15 is not a valid type; Expected type is DryValidation.Types.String",
...>  "age" => ~s("nonsense" is not a valid type; Expected type is DryValidation.Types.Integer)
...> }

Complex schemas can be crafted using the methods - required, optional, map and map_list. With the use the provided DryValidation.Types, requirements can be set and also cast values when possible.

Available Types

TypeDescription
DryValidation.Types.StringExpects a string type "some text". Will try to cast the value into a string (1001 = "1001").
DryValidation.Types.BoolExpects a boolean type [true/false]. Will cast the strings "true"/"false" to real booleans
DryValidation.Types.FloatExpects a float type [15.51]. Will try to cast a string to a float ("15.5" = 15.5).
DryValidation.Types.IntegerExpects an integer type [101]. Will try to cast a string to an integer ("100" = 100). It'll fail the cast if the string is a float.
DryValidation.Types.FuncCustom rules can be build using this, see the module docs. Example is the DryValidation.Types.Integer.greater_than(5) rule.
DryValidation.Types.ListExpects a list. Can have the list type set to one of the above, including a Func.
DryValidation.Types.AnyAccepts any value and will do no casting. Usually not used as the type can just be omitted when using optional and required

Advanced example

schema = DryValidation.schema do
  required :name, Types.String
  required :age, Types.Integer.greater_than(18)
  required :type, Types.Func.equal("users")
  optional :pets, Types.Func.member_of(["dog", "cat", "bird"])
  optional :favourite_numbers, Types.List.type(Types.Integer)

  map_list :cars do
    required :make, Types.String
    required :cc, Types.Integer
  end

  map :house, optional: true do
    required :address, Types.String
  end
end

input_data = %{
  "name" => "Jon Snow",
  "age" => 42,
  "type" => "users",
  "pet" => "dog",
  "favourite_numbers" => [],
  "cars" => [
    %{"make" => "AUDI", "cc" => 3000},
    %{"make" => "BMW", "cc" => 2000},
  ],
  "house" => %{
    "address" => "Church Road"
  }
}
{:ok, _output_data} = DryValidation.Validator.validate(schema, input_data)

Summary

Functions

Defines a map. Can be made optional.

Defines a list of maps. Can be made optional.

Defines an optional attribute in the schema. First argument is the name of the attribute, second argument is optional and defines the type.

Defines a mandatory attribute in the schema. First argument is the name of the attribute, second argument is optional and defines the type.

Creates a validation schema.

Functions

Link to this macro

map(name, opts \\ [], list)

(macro)

Defines a map. Can be made optional.

schema = DryValidation.schema do
  map :user, do
    required(:name, Types.String)
  end

  map :car, optional: true do
    required(:name, Types.String)
  end
end

input_data = %{"user" => %{"name" => "John"}}
{:ok, output_data} = DryValidation.Validator.validate(schema, input_data)
Link to this macro

map_list(name, opts \\ [], list)

(macro)

Defines a list of maps. Can be made optional.

schema = DryValidation.schema do
  map_list :users, do
    required(:name, Types.String)
  end
end

input_data = %{"users" => [%{"name" => "John"}, %{"name" => "Bob"}]}
{:ok, output_data} = DryValidation.Validator.validate(schema, input_data)
Link to this macro

optional(name, type \\ nil)

(macro)

Defines an optional attribute in the schema. First argument is the name of the attribute, second argument is optional and defines the type.

  schema = DryValidation.schema do
    required(:name)
    optional(:age)
  end

  input_data = %{"name" => "Jon"}
  {:ok, output_data} = DryValidation.Validator.validate(schema, input_data)
  output_data == %{"name" => "Jon"}
Link to this macro

required(name, type \\ nil)

(macro)

Defines a mandatory attribute in the schema. First argument is the name of the attribute, second argument is optional and defines the type.

  schema = DryValidation.schema do
    required(:name, Types.String)
    optional(:age)
  end

  input_data = %{"age" => 21}
  {:error, errors} = DryValidation.Validator.validate(schema, input_data)
  errors == %{"name" => "Is missing"}
Link to this macro

schema(list)

(macro)

Creates a validation schema.