class Hval::Validator

Public Class Methods

new(hash) click to toggle source

Return a Validator

Example

schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer } validator = Hval::Validator.new(schema)

# File lib/hval/validator.rb, line 11
def initialize(hash)
  @validators = hash.collect do |k, schema|
    [k, Hval::Schema.new(schema)]
  end
end

Public Instance Methods

call(hash) click to toggle source

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”]]]]

# File lib/hval/validator.rb, line 25
def call(hash)
  @result = @validators.collect do |key, validator|
    [key, validator.call(hash[key])]
  end
end
errors(processor=Hval::Errors) click to toggle source
# File lib/hval/validator.rb, line 36
def errors(processor=Hval::Errors)
  processor.process(@result)
end
valid?() click to toggle source
# File lib/hval/validator.rb, line 31
def valid?
  return false if @result.nil?      
  Hval::Result.new(@result).success?
end