module Hval class Validator # Return a Validator # # ==== Example # # schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer } # validator = Hval::Validator.new(schema) def initialize(hash) @validators = hash.collect do |k, schema| [k, Hval::Schema.new(schema)] end end # Return an Array with the results of the validation # # ==== Example # # schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer } # validator = Hval::Validator.new(schema) # validator.call({name: "guybrush", age: "45"}) # => [[:name, [[:type, String, true, "guybrush"], [:format, /brush/, true, "guybrush"]]], [:age, [[:type, Integer, false, "45"]]]] def call(hash) @result = @validators.collect do |key, validator| [key, validator.call(hash[key])] end end def valid? return false if @result.nil? Hval::Result.new(@result).success? end def errors(processor=Hval::Errors) processor.process(@result) end end end