Skip to content

malomohq/justify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Justify

Justify makes it easy to validate unstructured data.

Inspired heavily by Ecto.Changeset, Justify allows you to pipe a plain map into a series of validation functions using a simple and familiar API. No schemas or casting required.

Example

dataset =
  %{email: "madebyanthony"}
  |> Justify.validate_required(:email)
  |> Justify.validate_format(:email, ~r/\S+@\S+/)

dataset.errors #=> [email: {"has invalid format", validation: :format}]
dataset.valid? #=> false

Each validation function will return a Justify.Dataset struct which can be passed into the next function. If a validation error is encountered the dataset will be marked as invalid and an error will be added to the struct.

Custom Validations

You can provide your own custom validations using the Justify.add_error/4 function.

Example

defmodule MyValidator do
  def validate_color(data, field, color) do
    dataset = Justify.Dataset.new(data)

    value = Map.get(dataset.data, :field)

    if value == color do
      dataset
    else
      Justify.add_error(dataset, field, "wrong color", validation: :color)
    end
  end
end

Your custom validation can be used as part of a validation pipeline.

Example

dataset =
  %{color: "brown"}
  |> Justify.validation_required(:color)
  |> MyValidator.validate_color(:color, "green")

dataset.errors #=> [color: {"wrong color", validation: :color}]
dataset.valid? #=> false

Supported Validations