Class: Dry::Monads::Validated

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/monads/validated.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb

Overview

Validated is similar to Result and represents an outcome of a validation. The difference between Validated and Result is that the former implements #apply in a way that concatenates errors. This means that the error type has to have + implemented (be a semigroup). This plays nice with arrays and lists. Also, List#traverse implicitly uses a block that wraps errors with a list so that you don't have to do it manually.

Examples:

using with List

List::Validated[Valid('London'), Invalid(:name_missing), Invalid(:email_missing)]
# => Invalid(List[:name_missing, :email_missing])

with valid results

List::Validated[Valid('London'), Valid('John')]
# => Valid(List['London', 'John'])

Direct Known Subclasses

Invalid, Valid

Defined Under Namespace

Modules: Mixin Classes: Invalid, Valid

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pure(value) ⇒ Validated::Valid .pure(&block) ⇒ Validated::Valid

Wraps a value with Valid.

Overloads:


36
37
38
# File 'lib/dry/monads/validated.rb', line 36

def pure(value = Undefined, &block)
  Valid.new(Undefined.default(value, block))
end

Instance Method Details

#bindObject

Bind/flat_map is not implemented

Raises:

  • (NotImplementedError)

50
51
52
53
# File 'lib/dry/monads/validated.rb', line 50

def bind(*)
  # See https://typelevel.org/cats/datatypes/validated.html for details on why
  raise NotImplementedError, "Validated is not a monad because it would violate the monad laws"
end

#to_monadValidated::Valid, Validated::Invalid

Returns self.


44
45
46
# File 'lib/dry/monads/validated.rb', line 44

def to_monad
  self
end