valentine 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +6 -0
- data/README.txt +48 -0
- data/Rakefile +12 -0
- data/lib/valentine.rb +122 -0
- data/spec/valentine_spec.rb +64 -0
- metadata +71 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= valentine
|
2
|
+
|
3
|
+
* FIX (url)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Valentine is a simple validation library for JSON and ordinary Hash based on an easy Hash based syntax.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
Currently, Valentine handles the basic task of validatiing hashes very well. In the future, it should have custom error message support and the ability to apply its logic to arbitary objects, not just hashes.
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
Valentine::validate({:a=>1, :b=>{:c =>[1,2]}}, {:a=> Numeric, :b=>{:c=>[Numeric, Numeric]}}, false) => true
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
json
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
gem install valentine
|
24
|
+
|
25
|
+
== LICENSE:
|
26
|
+
|
27
|
+
(The MIT License)
|
28
|
+
|
29
|
+
Copyright (c) 2008
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
Hoe.new('valentine', '0.2.1') do |p|
|
6
|
+
p.developer('Mark Baran', 'mebaran@gmail.com')
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run specs"
|
10
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
11
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
12
|
+
end
|
data/lib/valentine.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
module Valentine
|
4
|
+
VERSION = "0.3.0"
|
5
|
+
DEFAULT_MESSAGES = {
|
6
|
+
:proc => "Failed to pass validation test.",
|
7
|
+
:array => "Collection failed to pass validation.",
|
8
|
+
:true => "Required key missing.",
|
9
|
+
:false => "Key forbidden in this context",
|
10
|
+
:value => "Invalid value."
|
11
|
+
}
|
12
|
+
|
13
|
+
## Convenience class for composing rules and syntactic sugar for supplying custom messages
|
14
|
+
class ValentineNote
|
15
|
+
attr_accessor :defendant, :rules
|
16
|
+
def initialize(defendant, rules={}, msgs={}, &transformer)
|
17
|
+
@rules, @report = defendant, rules
|
18
|
+
@transformer = transformer
|
19
|
+
case defendant
|
20
|
+
when Hash then @defendant = defendant
|
21
|
+
when String then @defendant = transform(defendant)
|
22
|
+
when ValentineNote
|
23
|
+
@defendant = defendant.defendant
|
24
|
+
@rules = defendant.rules.merge rules
|
25
|
+
end
|
26
|
+
@messages = msgs.merge! DEFAULT_MESSAGES
|
27
|
+
end
|
28
|
+
|
29
|
+
def revalidate
|
30
|
+
validate!
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate(msgs={})
|
34
|
+
@validation ||= validate!
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid?
|
38
|
+
Valentine::judge(self.validate)
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def validate!(msgs={})
|
44
|
+
Valentine::validate(@defendant, @rules, true, msgs.merge!(@messages))
|
45
|
+
end
|
46
|
+
|
47
|
+
def transform(s)
|
48
|
+
@transformer ? @transformer.call(s) : raise("No transformed defined.")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
## Module Utility Methods follow.
|
54
|
+
def self::validate(defendant, rules={}, report=true, msgs={})
|
55
|
+
return defendant.validate if defendant.is_a? ValentineNote
|
56
|
+
errors = {}
|
57
|
+
rules.each do |a, rule|
|
58
|
+
begin
|
59
|
+
result = Valentine::doublecheck(defendant[a], rule, report, a, msgs)
|
60
|
+
errors[a] = result
|
61
|
+
rescue Exception => e
|
62
|
+
errors[a] = e
|
63
|
+
end
|
64
|
+
end
|
65
|
+
report ? errors : self::judge(errors)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self::expectation(r, m="", msg=true, at=nil)
|
69
|
+
if r
|
70
|
+
true
|
71
|
+
else
|
72
|
+
msg ? ValentineError.new([at, m] * ': ') : false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def self::doublecheck(value, rule, msg=true, at=nil, msgs={})
|
77
|
+
msgs.merge!(DEFAULT_MESSAGES)
|
78
|
+
case rule
|
79
|
+
when Hash then value.respond_to?(:[]) && self::validate(value, rule)
|
80
|
+
when Proc
|
81
|
+
begin
|
82
|
+
r = rule.call(value)
|
83
|
+
if r.is_a? Exception
|
84
|
+
return r
|
85
|
+
else
|
86
|
+
Valentine::expectation r, msgs[:proc], msg, at
|
87
|
+
end
|
88
|
+
rescue Exception => e
|
89
|
+
return e
|
90
|
+
end
|
91
|
+
when Array
|
92
|
+
valid = true
|
93
|
+
value.is_a?(Array) && rule.each_index do |i|
|
94
|
+
valid &&= self::doublecheck(value[i], rule[i])
|
95
|
+
end
|
96
|
+
self::expectation valid, msgs[:array], msg, at
|
97
|
+
when true
|
98
|
+
self::expectation !value.nil?, msgs[:true], msg, at
|
99
|
+
when false
|
100
|
+
self::expectation value.nil?, msgs[:false], msg, at
|
101
|
+
when nil then true
|
102
|
+
else
|
103
|
+
self::expectation rule === value, msgs[:value], msg, at
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self::judge(valentine)
|
108
|
+
if valentine.is_a? ValentineNote
|
109
|
+
self::judge(valentine.validate)
|
110
|
+
else
|
111
|
+
valentine.values.inject(true) do |s, v|
|
112
|
+
case v
|
113
|
+
when Hash then s && self::judge(v)
|
114
|
+
when Exception then s && false
|
115
|
+
else s && v
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class ValentineError < Exception; end
|
122
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require './lib/valentine'
|
2
|
+
|
3
|
+
describe Valentine do
|
4
|
+
|
5
|
+
it "should validate true against an empty hash" do
|
6
|
+
Valentine::validate({:a=>1}, {}, false).should == true
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should validate integers against ranges" do
|
10
|
+
Valentine::validate({:a=>3}, {:a =>Numeric}, false).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should check arrays for the proper type" do
|
14
|
+
Valentine::validate({:a=>[1,2,3,4]}, {:a=>[Numeric, Numeric, String]}, false) == false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be able to validate arrays" do
|
18
|
+
Valentine::validate({:a=>[1,2,3,4]}, {:a=>[Numeric, proc{|x| x > 1}, Numeric]}, false).should == true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should handle complex cases against the blank ruleset" do
|
22
|
+
Valentine::validate({:a=>8, :b=>{:c=>4, :d => [1, {:e => "f"}]}}, {}, false).should == true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should use lambdas as rules" do
|
26
|
+
Valentine::validate({:a=>"superstring"}, {:a=> lambda{|s| s.length > 5}}, false).should == true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should recurse for hashes" do
|
30
|
+
Valentine::validate({:a=>8, :b=>{:c=>4, :d => [1, {:e => "f"}]}},
|
31
|
+
{:b=>{:d=>[Numeric, {:e=>String}]}}, false).should == true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should enforce exclusion requirements if false is passed" do
|
35
|
+
Valentine::validate({:a=>0}, {:a=>false}, false).should == false
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should ensure attributes marked true are present" do
|
39
|
+
Valentine::validate({:b => false}, {:a=>true}, false).should == false
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should use exceptions to indicate validation failures" do
|
43
|
+
Valentine::validate({:a=>4}, {:a=>lambda{|x| x < 3 ? true : Exception.new("Too big!")}}, false).should == false
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should catch raised errors as validation problems" do
|
47
|
+
Valentine::validate({:a=>3}, {:a=>lambda{|x| x < 2 ? true : raise("Too Big")}}, false).should == false
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should produce valid reports containing only truth, false, or Exception messages" do
|
51
|
+
tt = lambda do |h, eo|
|
52
|
+
h.values.inject(true) do |s, v|
|
53
|
+
if v.is_a? Hash
|
54
|
+
tt.call(v, eo)
|
55
|
+
else
|
56
|
+
s && (v == true || (!eo && v == false) || v.is_a?(Exception))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
rep = Valentine::validate({:a=>8, :b=>{:c=>4, :d => [1, {:e => "f"}]}},
|
61
|
+
{:a=>8, :b=>{:c=> Numeric, :d=>String}})
|
62
|
+
tt.call(rep, true).should == true
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valentine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Baran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-02 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.2
|
24
|
+
version:
|
25
|
+
description: Valentine is a simple validation library for JSON and ordinary Hash based on an easy Hash based syntax.
|
26
|
+
email:
|
27
|
+
- mebaran@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/valentine.rb
|
42
|
+
- spec/valentine_spec.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: FIX (url)
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: valentine
|
66
|
+
rubygems_version: 1.3.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: Valentine is a simple validation library for JSON and ordinary Hash based on an easy Hash based syntax.
|
70
|
+
test_files: []
|
71
|
+
|