Valuedate

Validates values using a schema.

Source | RDoc | Metrics

Usage

schema = Valuedate.schema do
  value.is_a(String).equals("test")
end

schema.validate("test") # => true
schema.validate("fail") # => false
schema.validate(23) # => false

deep = Valuedate.schema do
  value.hash(
    :type => value.is_a(String),
    :value => value.hash(
      :src => value.is_a(String),
      :height => optional_value.is_a(Fixnum),
      :width => optional_value.is_a(Fixnum)
    )
  )
end

deep.validate(
  :type => "image",
  :value => {
    :src => "/bold.gif",
    :height => 80,
    :width => 80
  }
) # => true

# :height and width are optional
deep.validate(:type => "image", :value => { :src => "/bold.gif" }) # => true

Matchers

Following matchers are defined:

  • is_a(Class)

  • equals(expected)

  • any(list of validators)

  • in(range, array or anything that responds_to #include?)

  • is { |value| boolean }

  • not { |value| boolean }

Missing matchers?

Use #is and #not:

schema = Valuedate.schema do
  value.hash(
    :array  => value.not { |value| value.empty? },
    :rgb    => value.is { |value| value.size == 3 }
  )
end

schema.validate(:array => [1], :rgb => [1,2,3]) # => true
schema.validate(:array => [1], :rgb => [1,2]) # => false

Defining matchers

Valuedate.matcher(:equals) { |value, expected| value == expected }

Install

git clone ...
rake install

Authors

  • Peter Suschlik

TODO

  • Implement and aggregate errors