Skip to content

Commit 8b38e9b

Browse files
authored
Merge pull request #3 from Kaligo/chore/move-validation-message
Move Validations::Message to inside HeavyKeeper namespace
2 parents 9f03bd8 + bf3933d commit 8b38e9b

File tree

7 files changed

+48
-40
lines changed

7 files changed

+48
-40
lines changed

.rspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
--format documentation
1+
--format progress
2+
--warnings
23
--color
34
--require spec_helper

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.1.6] - 2025-02-04
2+
3+
- Move `Validations::Message` to inside HeavyKeeper namespace to avoid
4+
conflict
5+
16
## [0.1.5] - 2024-06-13
27

38
- Support latest version of xx-hash

Gemfile.redis.4

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ gem 'rake', '~> 13.0'
1111
gem 'redis', '~> 4.0'
1212

1313
group :test do
14-
gem 'mock_redis'
14+
gem 'mock_redis', '< 0.47.0'
1515
gem 'rspec'
1616
gem 'pry-byebug'
1717
end

lib/heavy_keeper/top_k.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'dry-schema'
44
require 'securerandom'
55
require 'xxhash'
6-
require_relative '../validations/message'
6+
require_relative 'validations/message'
77

88
module HeavyKeeper
99
class TopK # rubocop:disable Metrics/ClassLength
@@ -217,7 +217,7 @@ def validate(options)
217217
result = Validator.call(options)
218218

219219
if result.failure?
220-
error = ::Validations::Message.new.build(result.errors.to_h).join('. ')
220+
error = Validations::Message.new.build(result.errors.to_h).join('. ')
221221
raise HeavyKeeper::Error, error
222222
end
223223

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module HeavyKeeper
2+
module Validations
3+
class Message
4+
CLASSIFY_SEPARATOR = '_'.freeze
5+
TITLEIZE_SEPARATOR = ' '.freeze
6+
7+
# @errors [Hash | Array] output of dry-validation
8+
# after validating params
9+
# @parent [Nil | String] key name of a field that has `errors`
10+
# after validating params
11+
# Output: array of string that can be used to feed into
12+
# Errors::InvalidParamsError
13+
def build(errors, parent = nil)
14+
case errors
15+
when Hash
16+
errors.flat_map do |key, value|
17+
child = [parent, key].compact.join(' ')
18+
build(value, child)
19+
end
20+
when Array
21+
errors.flat_map do |error|
22+
"#{titleize(parent.to_s)} #{build(error)}"
23+
end
24+
else
25+
errors
26+
end
27+
end
28+
29+
private
30+
31+
def titleize(string)
32+
# NOTE: this is not a robust implementation of titleize
33+
string.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
34+
end
35+
end
36+
end
37+
end

lib/heavy_keeper/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module HeavyKeeper
4-
VERSION = '0.1.5'
4+
VERSION = '0.1.6'
55
end

lib/validations/message.rb

-35
This file was deleted.

0 commit comments

Comments
 (0)