Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--format documentation
--format progress
--warnings
--color
--require spec_helper
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.1.6] - 2025-02-04

- Move `Validations::Message` to inside HeavyKeeper namespace to avoid
conflict

## [0.1.5] - 2024-06-13

- Support latest version of xx-hash
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.redis.4
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gem 'rake', '~> 13.0'
gem 'redis', '~> 4.0'

group :test do
gem 'mock_redis'
gem 'mock_redis', '< 0.47.0'
gem 'rspec'
gem 'pry-byebug'
end
4 changes: 2 additions & 2 deletions lib/heavy_keeper/top_k.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'dry-schema'
require 'securerandom'
require 'xxhash'
require_relative '../validations/message'
require_relative 'validations/message'

module HeavyKeeper
class TopK # rubocop:disable Metrics/ClassLength
Expand Down Expand Up @@ -217,7 +217,7 @@ def validate(options)
result = Validator.call(options)

if result.failure?
error = ::Validations::Message.new.build(result.errors.to_h).join('. ')
error = Validations::Message.new.build(result.errors.to_h).join('. ')
raise HeavyKeeper::Error, error
end

Expand Down
37 changes: 37 additions & 0 deletions lib/heavy_keeper/validations/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module HeavyKeeper
module Validations
class Message
CLASSIFY_SEPARATOR = '_'.freeze
TITLEIZE_SEPARATOR = ' '.freeze

# @errors [Hash | Array] output of dry-validation
# after validating params
# @parent [Nil | String] key name of a field that has `errors`
# after validating params
# Output: array of string that can be used to feed into
# Errors::InvalidParamsError
def build(errors, parent = nil)
case errors
when Hash
errors.flat_map do |key, value|
child = [parent, key].compact.join(' ')
build(value, child)
end
when Array
errors.flat_map do |error|
"#{titleize(parent.to_s)} #{build(error)}"
end
else
errors
end
end

private

def titleize(string)
# NOTE: this is not a robust implementation of titleize
string.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/heavy_keeper/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module HeavyKeeper
VERSION = '0.1.5'
VERSION = '0.1.6'
end
35 changes: 0 additions & 35 deletions lib/validations/message.rb

This file was deleted.