lib/maitredee.rb in maitredee-0.8.4 vs lib/maitredee.rb in maitredee-0.8.5
- old
+ new
@@ -1,12 +1,11 @@
require "json"
require "json_schemer"
require "set"
require "active_support/concern"
require "active_support/core_ext/string/inflections"
-require "active_support/core_ext/object/json"
-require "active_support/json"
+require "active_support/core_ext/object/blank"
require "pathname"
require "maitredee/publisher"
require "maitredee/subscriber"
require "maitredee/version"
require "maitredee/adapters/base_adapter"
@@ -43,11 +42,10 @@
schema_name:,
event_name: nil,
primary_key: nil
)
raise ArgumentError, "topic_name, body or schema_name is nil" if topic_name.nil? || body.nil? || schema_name.nil?
- body = body.as_json
validate!(body, schema_name)
message = PublisherMessage.new(
message_id: SecureRandom.uuid,
topic_resource_name: topic_resource_name(topic_name),
@@ -116,11 +114,11 @@
# @param schema [String] string key to look up schema to validate against
#
# @raise [ValidationError] if validation fails
# @return [nil]
def validate!(body, schema)
- errors = schemas[schema].validate(body.as_json)
+ errors = schemas[schema].validate(deep_stringify_keys(body))
properties = errors.map do |error|
error["data_pointer"]
end.join(", ")
if errors.any?
@@ -190,9 +188,24 @@
end
# @api private
def subscriber_registry
@subscriber_registry ||= Set.new
+ end
+
+ private
+
+ def deep_stringify_keys(object)
+ case object
+ when Hash
+ object.each_with_object({}) do |(key, value), result|
+ result[key.to_s] = deep_stringify_keys(value)
+ end
+ when Array
+ object.map { |e| deep_stringify_keys(e) }
+ else
+ object
+ end
end
end
Error = Class.new(StandardError)
ValidationError = Class.new(Error)