Sha256: 75db86d457631ca1794d86f7c9e6e2e17bd0dd864b8ca6f00dac9e58784dd27a

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hval-0.1.1 lib/hval/validator.rb