hash_validator 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -2
- data/lib/hash_validator/validators/simple_type_validators.rb +14 -6
- data/lib/hash_validator/version.rb +1 -1
- data/spec/validators/simple_types_spec.rb +34 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 09fcad8b45bef6d76eff9d52feec81e116383136
|
4
|
+
data.tar.gz: 0be312504f4b8365d39cf4641a9533c2d010c209
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c2f78699dddc2bd3652ae9ef371cebdf0ad560b361621c882c8c06bc7ebdd89ad3588f18fc29f9aa7348119ea0f02b588c6cf71eff58dc9da357d04301eadc4
|
7
|
+
data.tar.gz: fb82f77a46c71ca978b19919aa511e467d9ea45265bb652f4fadbf069b5f47801582e8372f1ccd382602582375d871ffd7782e92fae7121fbc2c8b881a0c5f80
|
data/README.md
CHANGED
@@ -64,9 +64,16 @@ validator.errors
|
|
64
64
|
|
65
65
|
Define a validation hash which will be used to validate. This has can be nested as deeply as required using the following values to validate specific value types:
|
66
66
|
|
67
|
-
* `string`
|
68
|
-
* `numeric`
|
69
67
|
* `array`
|
68
|
+
* `complex`
|
69
|
+
* `float`
|
70
|
+
* `integer`
|
71
|
+
* `numeric`
|
72
|
+
* `range`
|
73
|
+
* `rational`
|
74
|
+
* `regexp`
|
75
|
+
* `string`
|
76
|
+
* `symbol`
|
70
77
|
* `time`
|
71
78
|
* `required`: just requires any value to be present for the designated key.
|
72
79
|
* hashes are validates by nesting validations, or if just the presence of a hash is required `{}` can be used.
|
@@ -1,8 +1,16 @@
|
|
1
1
|
[
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
Array,
|
3
|
+
Complex,
|
4
|
+
Float,
|
5
|
+
Integer,
|
6
|
+
Numeric,
|
7
|
+
Range,
|
8
|
+
Rational,
|
9
|
+
Regexp,
|
10
|
+
String,
|
11
|
+
Symbol,
|
12
|
+
Time
|
13
|
+
].each do |type|
|
14
|
+
name = type.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase # ActiveSupport/Inflector#underscore behaviour
|
15
|
+
HashValidator.append_validator(HashValidator::Validator::SimpleValidator.new(name, lambda { |v| v.is_a?(type) }))
|
8
16
|
end
|
@@ -5,17 +5,45 @@ describe 'Simple validator types' do
|
|
5
5
|
|
6
6
|
# Simple types
|
7
7
|
{
|
8
|
-
|
9
|
-
valid: [
|
10
|
-
invalid: [ nil,
|
8
|
+
array: {
|
9
|
+
valid: [ [], [1], ['foo'], [1,['foo'],Time.now] ],
|
10
|
+
invalid: [ nil, '', 123, '123', Time.now, '[1]' ]
|
11
|
+
},
|
12
|
+
complex: {
|
13
|
+
valid: [ Complex(1), Complex(2, 3), Complex('2/3+3/4i'), 0.3.to_c ],
|
14
|
+
invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], '2/3+3/4i', Rational(2, 3) ]
|
15
|
+
},
|
16
|
+
float: {
|
17
|
+
valid: [ 0.0, 1.1, 1.23, Float::INFINITY, Float::EPSILON ],
|
18
|
+
invalid: [ nil, '', 0, 123, '123', Time.now, '[1]', '2013-03-04' ]
|
19
|
+
},
|
20
|
+
integer: {
|
21
|
+
valid: [ 0, -1000000, 1000000 ],
|
22
|
+
invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
|
11
23
|
},
|
12
24
|
numeric: {
|
13
25
|
valid: [ 0, 123, 123.45 ],
|
14
26
|
invalid: [ nil, '', '123', Time.now ]
|
15
27
|
},
|
16
|
-
|
17
|
-
valid: [
|
18
|
-
invalid: [ nil, '',
|
28
|
+
range: {
|
29
|
+
valid: [ 0..10, 'a'..'z', 5..0 ],
|
30
|
+
invalid: [ nil, '', '123', Time.now ]
|
31
|
+
},
|
32
|
+
rational: {
|
33
|
+
valid: [ Rational(1), Rational(2, 3), 3.to_r ],
|
34
|
+
invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], Complex(2, 3) ]
|
35
|
+
},
|
36
|
+
regexp: {
|
37
|
+
valid: [ /[a-z]+/, //, //i, Regexp.new('.*') ],
|
38
|
+
invalid: [ nil, '', 123, '123', Time.now, '.*' ]
|
39
|
+
},
|
40
|
+
string: {
|
41
|
+
valid: [ '', 'Hello World', '12345' ],
|
42
|
+
invalid: [ nil, 12345, Time.now ]
|
43
|
+
},
|
44
|
+
symbol: {
|
45
|
+
valid: [ :foo, :'', 'bar'.to_sym ],
|
46
|
+
invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
|
19
47
|
},
|
20
48
|
time: {
|
21
49
|
valid: [ Time.now ],
|