Skip to content

Commit

Permalink
workaround for dry-rb#618
Browse files Browse the repository at this point in the history
  • Loading branch information
fkostovski authored and Tadeusz Niemiec committed Aug 19, 2020
1 parent 11175ae commit 67b2798
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/dry/validation/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ def base?
@base ||= path.compact.empty?
end

# Build full message
#
# @return [Message]
#
# @api public
def full_message
if base?
self
else
Message["#{path.compact.join(',')} #{text}", path, meta]
end
end

# Dump error to a string
#
# @return [String]
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/validation/message_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def with(other, new_options = EMPTY_HASH)
return self if new_options.empty? && other.eql?(messages)

self.class.new(
other | select { |err| err.is_a?(Message) },
other | select { |err| err.is_a?(Message) }
.map {|err| new_options.fetch(:full, false) ? err.full_message : err },
options.merge(source: source_messages, **new_options)
).freeze
end
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@
tags: ['must have at least 1 element']
)
end

it 'shows the correct error message' do
contract_class.rule(:tags) do
key.failure('must have at least 1 element') unless value.size > 0
end

expect(contract.(tags: [], name: '').errors.to_h).to eql(
tags: ['must have at least 1 element']
)

expect(contract.(tags: [], name: '').errors(full: true).to_h).to eql(
tags: ['tags must have at least 1 element']
)
end
end
end

0 comments on commit 67b2798

Please sign in to comment.