KeywordValidator

Build Status StatBuffer Version

KeywordValidator provides a simple interface to validate keyword lists in Elixir.

Installation

The package can be installed by adding keyword_validator to your list of dependencies in mix.exs:

def deps do
  [
    {:keyword_validator, "~> 0.1"}
  ]
end

Documentation

Please see HexDocs for additional documentation.

Getting Started

To begin validating, we must first create a schema that defines the rules for our keyword list.

A schema is a simple map, with each key representing a key in your keyword list. The values in the map represent the options available for validation. If the validation passes, we are returned a valid keyword list.

iex> keyword = [foo: :foo, bar: "one"]
iex> schema = %{foo: [type: :atom, required: true], bar: [inclusion: ["one", "two", "three"]]}
iex> KeywordValidator.validate(keyword, schema)
{:ok, [foo: :foo, bar: "one"]}

If validation fails on any of the keys, a keyword list of the invalid entries and the associated errors is returned.

iex> keyword = [foo: :foo, bar: :bar]
iex> schema = %{foo: [type: :binary], bar: [inclusion: [:one, :two, :three]]}
iex> KeywordValidator.validate(keyword, schema)
{:error, [bar: ["must be one of: [:one, :two, :three]"], foo: ["must be a binary"]]}

We can optionally use the KeywordValidator.validate!/2 function as well - which will raise an error that describes the invalid key entries.

** (ArgumentError) Invalid keyword given.

Keyword:

[foo: :foo, bar: :bar]

Invalid:

bar: ["must be one of: [:one, :two, :three]"]
foo: ["must be a binary"]

To view all the available validation options, please check out the relevant documentation.