class Sullivan::Validations::Hash
Public Class Methods
new(validations)
click to toggle source
# File lib/sullivan/validations/hash.rb, line 4 def initialize(validations) @validations = validations end
Public Instance Methods
validate(hash)
click to toggle source
# File lib/sullivan/validations/hash.rb, line 8 def validate(hash) return 'must be a hash' unless hash.respond_to?(:to_hash) hash = hash.to_hash errors = @validations.each_with_object({}) { |(key, validation), h| error = validation.validate(hash[key]) h[key] = error unless error.nil? } errors.merge! (hash.keys - @validations.keys).each_with_object({}) { |unexpected_key, h| h[unexpected_key] = 'is unexpected' } errors unless errors.empty? end