dry-validation 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/LICENSE +1 -1
- data/README.md +9 -9
- data/dry-validation.gemspec +2 -1
- data/lib/dry/validation.rb +1 -0
- data/lib/dry/validation/input_type_compiler.rb +2 -2
- data/lib/dry/validation/schema.rb +7 -6
- data/lib/dry/validation/schema/key.rb +0 -2
- data/lib/dry/validation/version.rb +1 -1
- data/spec/integration/custom_predicates_spec.rb +1 -1
- data/spec/integration/schema_form_spec.rb +0 -2
- data/spec/unit/input_type_compiler_spec.rb +57 -0
- metadata +20 -89
- data/lib/dry/validation/predicate.rb +0 -35
- data/lib/dry/validation/predicate_set.rb +0 -22
- data/lib/dry/validation/predicates.rb +0 -125
- data/lib/dry/validation/rule.rb +0 -90
- data/lib/dry/validation/rule/check.rb +0 -15
- data/lib/dry/validation/rule/composite.rb +0 -63
- data/lib/dry/validation/rule/each.rb +0 -13
- data/lib/dry/validation/rule/group.rb +0 -21
- data/lib/dry/validation/rule/key.rb +0 -17
- data/lib/dry/validation/rule/result.rb +0 -119
- data/lib/dry/validation/rule/set.rb +0 -22
- data/lib/dry/validation/rule/value.rb +0 -13
- data/lib/dry/validation/rule_compiler.rb +0 -81
- data/spec/shared/predicates.rb +0 -35
- data/spec/unit/predicate_spec.rb +0 -27
- data/spec/unit/predicates/bool_spec.rb +0 -34
- data/spec/unit/predicates/date_spec.rb +0 -31
- data/spec/unit/predicates/date_time_spec.rb +0 -31
- data/spec/unit/predicates/decimal_spec.rb +0 -32
- data/spec/unit/predicates/empty_spec.rb +0 -38
- data/spec/unit/predicates/eql_spec.rb +0 -21
- data/spec/unit/predicates/exclusion_spec.rb +0 -35
- data/spec/unit/predicates/filled_spec.rb +0 -38
- data/spec/unit/predicates/float_spec.rb +0 -31
- data/spec/unit/predicates/format_spec.rb +0 -21
- data/spec/unit/predicates/gt_spec.rb +0 -40
- data/spec/unit/predicates/gteq_spec.rb +0 -40
- data/spec/unit/predicates/inclusion_spec.rb +0 -35
- data/spec/unit/predicates/int_spec.rb +0 -34
- data/spec/unit/predicates/key_spec.rb +0 -29
- data/spec/unit/predicates/lt_spec.rb +0 -40
- data/spec/unit/predicates/lteq_spec.rb +0 -40
- data/spec/unit/predicates/max_size_spec.rb +0 -49
- data/spec/unit/predicates/min_size_spec.rb +0 -49
- data/spec/unit/predicates/none_spec.rb +0 -28
- data/spec/unit/predicates/size_spec.rb +0 -55
- data/spec/unit/predicates/str_spec.rb +0 -32
- data/spec/unit/predicates/time_spec.rb +0 -31
- data/spec/unit/rule/check_spec.rb +0 -29
- data/spec/unit/rule/conjunction_spec.rb +0 -28
- data/spec/unit/rule/disjunction_spec.rb +0 -36
- data/spec/unit/rule/each_spec.rb +0 -20
- data/spec/unit/rule/group_spec.rb +0 -12
- data/spec/unit/rule/implication_spec.rb +0 -14
- data/spec/unit/rule/key_spec.rb +0 -27
- data/spec/unit/rule/set_spec.rb +0 -32
- data/spec/unit/rule/value_spec.rb +0 -42
- data/spec/unit/rule_compiler_spec.rb +0 -123
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'dry/validation/predicate'
|
2
|
-
|
3
|
-
module Dry
|
4
|
-
module Validation
|
5
|
-
module PredicateSet
|
6
|
-
module Methods
|
7
|
-
def predicate(name, &block)
|
8
|
-
register(name) { Predicate.new(name, &block) }
|
9
|
-
end
|
10
|
-
|
11
|
-
def import(predicate_set)
|
12
|
-
merge(predicate_set)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.extended(other)
|
17
|
-
super
|
18
|
-
other.extend(Methods, Dry::Container::Mixin)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,125 +0,0 @@
|
|
1
|
-
require 'dry/validation/predicate_set'
|
2
|
-
|
3
|
-
module Dry
|
4
|
-
module Validation
|
5
|
-
module Predicates
|
6
|
-
extend PredicateSet
|
7
|
-
|
8
|
-
def self.included(other)
|
9
|
-
super
|
10
|
-
other.extend(PredicateSet)
|
11
|
-
other.import(self)
|
12
|
-
end
|
13
|
-
|
14
|
-
predicate(:none?) do |input|
|
15
|
-
input.nil?
|
16
|
-
end
|
17
|
-
|
18
|
-
predicate(:key?) do |name, input|
|
19
|
-
input.key?(name)
|
20
|
-
end
|
21
|
-
|
22
|
-
predicate(:empty?) do |input|
|
23
|
-
case input
|
24
|
-
when String, Array, Hash then input.empty?
|
25
|
-
when nil then true
|
26
|
-
else
|
27
|
-
false
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
predicate(:filled?) do |input|
|
32
|
-
!self[:empty?].(input)
|
33
|
-
end
|
34
|
-
|
35
|
-
predicate(:bool?) do |input|
|
36
|
-
input.is_a?(TrueClass) || input.is_a?(FalseClass)
|
37
|
-
end
|
38
|
-
|
39
|
-
predicate(:date?) do |input|
|
40
|
-
input.is_a?(Date)
|
41
|
-
end
|
42
|
-
|
43
|
-
predicate(:date_time?) do |input|
|
44
|
-
input.is_a?(DateTime)
|
45
|
-
end
|
46
|
-
|
47
|
-
predicate(:time?) do |input|
|
48
|
-
input.is_a?(Time)
|
49
|
-
end
|
50
|
-
|
51
|
-
predicate(:int?) do |input|
|
52
|
-
input.is_a?(Fixnum)
|
53
|
-
end
|
54
|
-
|
55
|
-
predicate(:float?) do |input|
|
56
|
-
input.is_a?(Float)
|
57
|
-
end
|
58
|
-
|
59
|
-
predicate(:decimal?) do |input|
|
60
|
-
input.is_a?(BigDecimal)
|
61
|
-
end
|
62
|
-
|
63
|
-
predicate(:str?) do |input|
|
64
|
-
input.is_a?(String)
|
65
|
-
end
|
66
|
-
|
67
|
-
predicate(:hash?) do |input|
|
68
|
-
input.is_a?(Hash)
|
69
|
-
end
|
70
|
-
|
71
|
-
predicate(:array?) do |input|
|
72
|
-
input.is_a?(Array)
|
73
|
-
end
|
74
|
-
|
75
|
-
predicate(:lt?) do |num, input|
|
76
|
-
input < num
|
77
|
-
end
|
78
|
-
|
79
|
-
predicate(:gt?) do |num, input|
|
80
|
-
input > num
|
81
|
-
end
|
82
|
-
|
83
|
-
predicate(:lteq?) do |num, input|
|
84
|
-
!self[:gt?].(num, input)
|
85
|
-
end
|
86
|
-
|
87
|
-
predicate(:gteq?) do |num, input|
|
88
|
-
!self[:lt?].(num, input)
|
89
|
-
end
|
90
|
-
|
91
|
-
predicate(:size?) do |size, input|
|
92
|
-
case size
|
93
|
-
when Fixnum then size == input.size
|
94
|
-
when Range, Array then size.include?(input.size)
|
95
|
-
else
|
96
|
-
raise ArgumentError, "+#{size}+ is not supported type for size? predicate"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
predicate(:min_size?) do |num, input|
|
101
|
-
input.size >= num
|
102
|
-
end
|
103
|
-
|
104
|
-
predicate(:max_size?) do |num, input|
|
105
|
-
input.size <= num
|
106
|
-
end
|
107
|
-
|
108
|
-
predicate(:inclusion?) do |list, input|
|
109
|
-
list.include?(input)
|
110
|
-
end
|
111
|
-
|
112
|
-
predicate(:exclusion?) do |list, input|
|
113
|
-
!self[:inclusion?].(list, input)
|
114
|
-
end
|
115
|
-
|
116
|
-
predicate(:eql?) do |left, right|
|
117
|
-
left.eql?(right)
|
118
|
-
end
|
119
|
-
|
120
|
-
predicate(:format?) do |regex, input|
|
121
|
-
!regex.match(input).nil?
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
data/lib/dry/validation/rule.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class Rule
|
4
|
-
include Dry::Equalizer(:name, :predicate)
|
5
|
-
|
6
|
-
attr_reader :name, :predicate
|
7
|
-
|
8
|
-
class Negation < Rule
|
9
|
-
include Dry::Equalizer(:rule)
|
10
|
-
|
11
|
-
attr_reader :rule
|
12
|
-
|
13
|
-
def initialize(rule)
|
14
|
-
@rule = rule
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(*args)
|
18
|
-
rule.(*args).negated
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_ary
|
22
|
-
[:not, rule.to_ary]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def initialize(name, predicate)
|
27
|
-
@name = name
|
28
|
-
@predicate = predicate
|
29
|
-
end
|
30
|
-
|
31
|
-
def predicate_id
|
32
|
-
predicate.id
|
33
|
-
end
|
34
|
-
|
35
|
-
def type
|
36
|
-
:rule
|
37
|
-
end
|
38
|
-
|
39
|
-
def call(*args)
|
40
|
-
Validation.Result(args, predicate.call, self)
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_ary
|
44
|
-
[type, [name, predicate.to_ary]]
|
45
|
-
end
|
46
|
-
alias_method :to_a, :to_ary
|
47
|
-
|
48
|
-
def and(other)
|
49
|
-
Conjunction.new(self, other)
|
50
|
-
end
|
51
|
-
alias_method :&, :and
|
52
|
-
|
53
|
-
def or(other)
|
54
|
-
Disjunction.new(self, other)
|
55
|
-
end
|
56
|
-
alias_method :|, :or
|
57
|
-
|
58
|
-
def xor(other)
|
59
|
-
ExclusiveDisjunction.new(self, other)
|
60
|
-
end
|
61
|
-
alias_method :^, :xor
|
62
|
-
|
63
|
-
def then(other)
|
64
|
-
Implication.new(self, other)
|
65
|
-
end
|
66
|
-
alias_method :>, :then
|
67
|
-
|
68
|
-
def negation
|
69
|
-
Negation.new(self)
|
70
|
-
end
|
71
|
-
|
72
|
-
def new(predicate)
|
73
|
-
self.class.new(name, predicate)
|
74
|
-
end
|
75
|
-
|
76
|
-
def curry(*args)
|
77
|
-
self.class.new(name, predicate.curry(*args))
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
require 'dry/validation/rule/key'
|
84
|
-
require 'dry/validation/rule/value'
|
85
|
-
require 'dry/validation/rule/each'
|
86
|
-
require 'dry/validation/rule/set'
|
87
|
-
require 'dry/validation/rule/composite'
|
88
|
-
require 'dry/validation/rule/check'
|
89
|
-
require 'dry/validation/rule/group'
|
90
|
-
require 'dry/validation/rule/result'
|
@@ -1,63 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class Rule::Composite < Rule
|
4
|
-
include Dry::Equalizer(:left, :right)
|
5
|
-
|
6
|
-
attr_reader :name, :left, :right
|
7
|
-
|
8
|
-
def initialize(left, right)
|
9
|
-
@left = left
|
10
|
-
@right = right
|
11
|
-
end
|
12
|
-
|
13
|
-
def name
|
14
|
-
:"#{left.name}_#{type}_#{right.name}"
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_ary
|
18
|
-
[type, [left.to_ary, right.to_ary]]
|
19
|
-
end
|
20
|
-
alias_method :to_a, :to_ary
|
21
|
-
end
|
22
|
-
|
23
|
-
class Rule::Implication < Rule::Composite
|
24
|
-
def call(*args)
|
25
|
-
left.(*args) > right
|
26
|
-
end
|
27
|
-
|
28
|
-
def type
|
29
|
-
:implication
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class Rule::Conjunction < Rule::Composite
|
34
|
-
def call(*args)
|
35
|
-
left.(*args).and(right)
|
36
|
-
end
|
37
|
-
|
38
|
-
def type
|
39
|
-
:and
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
class Rule::Disjunction < Rule::Composite
|
44
|
-
def call(*args)
|
45
|
-
left.(*args).or(right)
|
46
|
-
end
|
47
|
-
|
48
|
-
def type
|
49
|
-
:or
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
class Rule::ExclusiveDisjunction < Rule::Composite
|
54
|
-
def call(*args)
|
55
|
-
left.(*args).xor(right)
|
56
|
-
end
|
57
|
-
|
58
|
-
def type
|
59
|
-
:xor
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class Rule::Group < Rule
|
4
|
-
attr_reader :rules
|
5
|
-
|
6
|
-
def initialize(identifier, predicate)
|
7
|
-
name, rules = identifier.to_a.first
|
8
|
-
@rules = rules
|
9
|
-
super(name, predicate)
|
10
|
-
end
|
11
|
-
|
12
|
-
def call(*input)
|
13
|
-
Validation.Result(input, predicate.(*input), self)
|
14
|
-
end
|
15
|
-
|
16
|
-
def type
|
17
|
-
:group
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class Rule::Key < Rule
|
4
|
-
def self.new(name, predicate)
|
5
|
-
super(name, predicate.curry(name))
|
6
|
-
end
|
7
|
-
|
8
|
-
def type
|
9
|
-
:key
|
10
|
-
end
|
11
|
-
|
12
|
-
def call(input)
|
13
|
-
Validation.Result(input[name], predicate.(input), self)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,119 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
def self.Result(input, value, rule)
|
4
|
-
case value
|
5
|
-
when Rule::Result then value.class.new(value.input, value.success?, rule)
|
6
|
-
when Array then Rule::Result::Set.new(input, value, rule)
|
7
|
-
else Rule::Result::Value.new(input, value, rule)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class Rule::Result
|
12
|
-
include Dry::Equalizer(:success?, :input, :rule)
|
13
|
-
|
14
|
-
attr_reader :input, :value, :rule, :name
|
15
|
-
|
16
|
-
class Rule::Result::Set < Rule::Result
|
17
|
-
def success?
|
18
|
-
value.all?(&:success?)
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_ary
|
22
|
-
indices = value.map { |v| v.failure? ? value.index(v) : nil }.compact
|
23
|
-
[:input, [rule.name, input, value.values_at(*indices).map(&:to_ary)]]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class Rule::Result::Value < Rule::Result
|
28
|
-
def to_ary
|
29
|
-
[:input, [rule.name, input, [rule.to_ary]]]
|
30
|
-
end
|
31
|
-
alias_method :to_a, :to_ary
|
32
|
-
end
|
33
|
-
|
34
|
-
class Rule::Result::Verified < Rule::Result
|
35
|
-
attr_reader :predicate_id
|
36
|
-
|
37
|
-
def initialize(result, predicate_id)
|
38
|
-
@input = result.input
|
39
|
-
@value = result.value
|
40
|
-
@rule = result.rule
|
41
|
-
@name = result.name
|
42
|
-
@predicate_id = predicate_id
|
43
|
-
end
|
44
|
-
|
45
|
-
def call
|
46
|
-
Validation.Result(input, success?, rule)
|
47
|
-
end
|
48
|
-
|
49
|
-
def to_ary
|
50
|
-
[:input, [name, input, [rule.to_ary]]]
|
51
|
-
end
|
52
|
-
alias_method :to_a, :to_ary
|
53
|
-
|
54
|
-
def success?
|
55
|
-
rule.predicate_id == predicate_id
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def initialize(input, value, rule)
|
60
|
-
@input = input
|
61
|
-
@value = value
|
62
|
-
@rule = rule
|
63
|
-
@name = rule.name
|
64
|
-
end
|
65
|
-
|
66
|
-
def call
|
67
|
-
self
|
68
|
-
end
|
69
|
-
|
70
|
-
def curry(predicate_id = nil)
|
71
|
-
if predicate_id
|
72
|
-
Rule::Result::Verified.new(self, predicate_id)
|
73
|
-
else
|
74
|
-
self
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def negated
|
79
|
-
self.class.new(input, !value, rule)
|
80
|
-
end
|
81
|
-
|
82
|
-
def >(other)
|
83
|
-
if success?
|
84
|
-
other.(input)
|
85
|
-
else
|
86
|
-
Validation.Result(input, true, rule)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def and(other)
|
91
|
-
if success?
|
92
|
-
other.(input)
|
93
|
-
else
|
94
|
-
self
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def or(other)
|
99
|
-
if success?
|
100
|
-
self
|
101
|
-
else
|
102
|
-
other.(input)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def xor(other)
|
107
|
-
Validation.Result(input, success? ^ other.(input).success?, rule)
|
108
|
-
end
|
109
|
-
|
110
|
-
def success?
|
111
|
-
@value
|
112
|
-
end
|
113
|
-
|
114
|
-
def failure?
|
115
|
-
! success?
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|