Dredd
Dredd judges structs; it's a validator.
Dredd was forked from Justify
Inspired heavily by Ecto.Changeset, Dredd 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: "this_is_not_an_email"}
|> Dredd.validate_required(:email)
|> Dredd.validate_format(:email, ~r/\S+@\S+/)
dataset.errors #=> [email: {"has invalid format", validation: :format}]
dataset.valid? #=> false
Each validation function will return a Dredd.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 Dredd.add_error/4
function.
Example
defmodule MyValidator do
def validate_color(data, field, color) do
dataset = Dredd.Dataset.new(data)
value = Map.get(dataset.data, :field)
if value == color do
dataset
else
Dredd.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"}
|> Dredd.validation_required(:color)
|> MyValidator.validate_color(:color, "green")
dataset.errors #=> [color: {"wrong color", validation: :color}]
dataset.valid? #=> false
Supported Validations
validate_acceptance/3validate_confirmation/3validate_embed/3validate_exclusion/4validate_format/4validate_inclusion/4validate_length/3validate_required/3validate_type/4
Copyright and License
Copyright (c) 2021 Anthoniy Smith
Copyright (c) 2022 Marcus Autenrieth
Dredd is licensed under the MIT License, see LICENSE.md for details.