vldt
Easily create validations to validate any input data independent from the structure of your models or your database layout.
This is inspired by the clojure libary vlad.
Usage
Let's dive right in with an example. We want to validate a car.
car = {
driver: { age: 15 },
passengers: [
{ name: "Peter Parker", age: 25 },
{ name: "Yoda", age: 980 }
],
tires: [
{ condition: "happy", used_since: "2013-01-12" },
{ condition: "good", used_since: "2012-11-12" },
{ condition: "good", used_since: "2012" },
{ condition: "bad" }
]
}
module CarValidation
extend Vldt::Common
S = Vldt::String
N = Vldt::Number
A = Vldt::Array
def self.car
join(
validate(:driver, person),
validate(:passengers, chain(
A.array,
join(
A.length_between(0, 3),
each(person)))),
validate(:tires, chain(
A.array,
join(
A.length(4),
each(tire)))))
end
def self.person
join(
validate(:name, chain(
S.string,
S.length_between(4, 10))),
validate(:age, chain(
N.number,
N.integer,
N.positive)))
end
def self.tire
join(
validate(:condition, one_of("good", "bad")),
validate(:used_since, chain(
present,
with(-> date { Date.parse(date) },
greater_than(Date.new(2013, 1, 6))))))
end
end
Installation
Add this line to your application's Gemfile:
gem "vldt"
And then execute:
$ bundle
Or install it yourself as:
$ gem install vldt
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request