dry-validation 1.0.0.beta2 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/lib/dry/validation.rb +4 -0
- data/lib/dry/validation/contract.rb +15 -8
- data/lib/dry/validation/extensions/hints.rb +61 -0
- data/lib/dry/validation/message_set.rb +3 -3
- data/lib/dry/validation/result.rb +16 -6
- data/lib/dry/validation/rule.rb +10 -1
- data/lib/dry/validation/version.rb +1 -1
- metadata +11 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97e07b84e6d69b8ff4c887a633dba3862ebb44219d1aa36b0a4394e08761b8e0
|
4
|
+
data.tar.gz: 18a796e79f03f85f288307038a29c913a0340e6a1c77e2b6b138b96397b0aff9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3f8fd949d909e6823c5e5aced8fb055eec20263f06247b98e066187efc57a1e987b0c016f472b99693a79191aafc64c63b0ec68d64ef8148b2f4901a861335f
|
7
|
+
data.tar.gz: c146b67aaab383aa39f908b3e487fa249f7eeb9d13b7e2c165a2c845cb5450775c52f3fc3811a900d6a1c2a2e36fc79fc002b2c04a843d0fa1e635888abb7e6a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
# v1.0.0.rc1 2019-04-26
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* `:hints` extension is back (solnic)
|
6
|
+
* `Result` objects have access to the context object which is shared between rules (flash-gordon)
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
|
10
|
+
* Multiple hint messages no longer crash message set (flash-gordon)
|
11
|
+
* `Contract#inspect` no longer crashes (solnic)
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
|
15
|
+
* Dependency on `dry-schema` was bumped to `~> 0.6` - this pulls in `dry-types 1.0.0` and `dry-logic 1.0.0` (solnic)
|
16
|
+
* Dependency on `dry-initializer` was bumped to `~> 3.0` (solnic)
|
17
|
+
|
18
|
+
[Compare v1.0.0.beta2...v1.0.0.rc1](https://github.com/dry-rb/dry-validation/compare/v1.0.0.beta2...v1.0.0.rc1)
|
19
|
+
|
1
20
|
# v1.0.0.beta2 2019-04-04
|
2
21
|
|
3
22
|
### Added
|
data/lib/dry/validation.rb
CHANGED
@@ -25,7 +25,7 @@ module Dry
|
|
25
25
|
# your rules.
|
26
26
|
#
|
27
27
|
# @example
|
28
|
-
# class
|
28
|
+
# class NewUserContract < Dry::Validation::Contract
|
29
29
|
# params do
|
30
30
|
# required(:email).filled(:string)
|
31
31
|
# required(:age).filled(:integer)
|
@@ -35,11 +35,11 @@ module Dry
|
|
35
35
|
# end
|
36
36
|
#
|
37
37
|
# rule(:password) do
|
38
|
-
# failure('is required') if values[:login] && !values[:password]
|
38
|
+
# key.failure('is required') if values[:login] && !values[:password]
|
39
39
|
# end
|
40
40
|
#
|
41
41
|
# rule(:age) do
|
42
|
-
# failure('must be greater or equal 18') if values[:age] < 18
|
42
|
+
# key.failure('must be greater or equal 18') if values[:age] < 18
|
43
43
|
# end
|
44
44
|
# end
|
45
45
|
#
|
@@ -48,7 +48,7 @@ module Dry
|
|
48
48
|
#
|
49
49
|
# @api public
|
50
50
|
class Contract
|
51
|
-
include Dry::Equalizer(:schema, :rules, :messages)
|
51
|
+
include Dry::Equalizer(:schema, :rules, :messages, inspect: false)
|
52
52
|
|
53
53
|
extend Dry::Initializer
|
54
54
|
extend ClassInterface
|
@@ -86,18 +86,25 @@ module Dry
|
|
86
86
|
#
|
87
87
|
# @api public
|
88
88
|
def call(input)
|
89
|
-
Result.new(schema.(input), locale: locale) do |result|
|
90
|
-
context = Concurrent::Map.new
|
91
|
-
|
89
|
+
Result.new(schema.(input), Concurrent::Map.new, locale: locale) do |result|
|
92
90
|
rules.each do |rule|
|
93
91
|
next if rule.keys.any? { |key| result.error?(key) }
|
94
92
|
|
95
|
-
rule.(self, result, context).failures.each do |failure|
|
93
|
+
rule.(self, result, result.context).failures.each do |failure|
|
96
94
|
result.add_error(message_resolver[failure])
|
97
95
|
end
|
98
96
|
end
|
99
97
|
end
|
100
98
|
end
|
99
|
+
|
100
|
+
# Return a nice string representation
|
101
|
+
#
|
102
|
+
# @return [String]
|
103
|
+
#
|
104
|
+
# @api public
|
105
|
+
def inspect
|
106
|
+
%(#<#{self.class} schema=#{schema.inspect} rules=#{rules.inspect}>)
|
107
|
+
end
|
101
108
|
end
|
102
109
|
end
|
103
110
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/monads/result'
|
4
|
+
|
5
|
+
module Dry
|
6
|
+
module Validation
|
7
|
+
# Hints extension for contract results
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# Dry::Validation.load_extensions(:hints)
|
11
|
+
#
|
12
|
+
# contract = Dry::Validation::Contract.build do
|
13
|
+
# schema do
|
14
|
+
# required(:name).filled(:string, min_size?: 2..4)
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# contract.call(name: "fo").hints
|
19
|
+
# # {:name=>["size must be within 2 - 4"]}
|
20
|
+
#
|
21
|
+
# contract.call(name: "").messages
|
22
|
+
# # {:name=>["must be filled", "size must be within 2 - 4"]}
|
23
|
+
#
|
24
|
+
# @api public
|
25
|
+
module Hints
|
26
|
+
module ResultExtensions
|
27
|
+
# Return error messages excluding hints
|
28
|
+
#
|
29
|
+
# @return [MessageSet]
|
30
|
+
#
|
31
|
+
# @api public
|
32
|
+
def errors(new_opts = EMPTY_HASH)
|
33
|
+
opts = new_opts.merge(hints: false)
|
34
|
+
@errors.with(schema_errors(opts), opts)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Return errors and hints
|
38
|
+
#
|
39
|
+
# @return [MessageSet]
|
40
|
+
#
|
41
|
+
# @api public
|
42
|
+
def messages(new_opts = EMPTY_HASH)
|
43
|
+
errors.with(hints.to_a, options.merge(**new_opts))
|
44
|
+
end
|
45
|
+
|
46
|
+
# Return hint messages
|
47
|
+
#
|
48
|
+
# @return [MessageSet]
|
49
|
+
#
|
50
|
+
# @api public
|
51
|
+
def hints(new_opts = EMPTY_HASH)
|
52
|
+
values.hints(new_opts)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Dry::Schema.load_extensions(:hints)
|
57
|
+
|
58
|
+
Result.prepend(ResultExtensions)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -25,7 +25,7 @@ module Dry
|
|
25
25
|
# @api private
|
26
26
|
def initialize(messages, options = EMPTY_HASH)
|
27
27
|
@locale = options.fetch(:locale, :en)
|
28
|
-
@source_messages = options.fetch(:source
|
28
|
+
@source_messages = options.fetch(:source) { messages.dup }
|
29
29
|
super
|
30
30
|
end
|
31
31
|
|
@@ -38,7 +38,7 @@ module Dry
|
|
38
38
|
return self if new_options.empty?
|
39
39
|
|
40
40
|
self.class.new(
|
41
|
-
other + select { |err| err.is_a?(Message) },
|
41
|
+
(other + select { |err| err.is_a?(Message) }).uniq,
|
42
42
|
options.merge(source: source_messages, **new_options)
|
43
43
|
).freeze
|
44
44
|
end
|
@@ -101,7 +101,7 @@ module Dry
|
|
101
101
|
def messages_map
|
102
102
|
@messages_map ||= reduce(placeholders) { |hash, msg|
|
103
103
|
node = msg.path.reduce(hash) { |a, e| a.is_a?(Hash) ? a[e] : a.last[e] }
|
104
|
-
(node.
|
104
|
+
(node[0].is_a?(::Array) ? node[0] : node) << msg.dump
|
105
105
|
hash
|
106
106
|
}
|
107
107
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'concurrent/map'
|
3
4
|
require 'dry/equalizer'
|
4
5
|
|
5
6
|
require 'dry/validation/constants'
|
@@ -11,14 +12,14 @@ module Dry
|
|
11
12
|
#
|
12
13
|
# @api public
|
13
14
|
class Result
|
14
|
-
include Dry::Equalizer(:values, :errors)
|
15
|
+
include Dry::Equalizer(:values, :context, :errors)
|
15
16
|
|
16
17
|
# Build a new result
|
17
18
|
#
|
18
19
|
# @param [Dry::Schema::Result]
|
19
20
|
#
|
20
21
|
# @api private
|
21
|
-
def self.new(values, options = EMPTY_HASH)
|
22
|
+
def self.new(values, context = ::Concurrent::Map.new, options = EMPTY_HASH)
|
22
23
|
result = super
|
23
24
|
yield(result) if block_given?
|
24
25
|
result.freeze
|
@@ -29,6 +30,11 @@ module Dry
|
|
29
30
|
# @api private
|
30
31
|
attr_reader :values
|
31
32
|
|
33
|
+
# @!attribute [r] context
|
34
|
+
# @return [Concurrent::Map]
|
35
|
+
# @api public
|
36
|
+
attr_reader :context
|
37
|
+
|
32
38
|
# @!attribute [r] options
|
33
39
|
# @return [Hash]
|
34
40
|
# @api private
|
@@ -37,8 +43,9 @@ module Dry
|
|
37
43
|
# Initialize a new result
|
38
44
|
#
|
39
45
|
# @api private
|
40
|
-
def initialize(values, options)
|
46
|
+
def initialize(values, context, options)
|
41
47
|
@values = values
|
48
|
+
@context = context
|
42
49
|
@options = options
|
43
50
|
@errors = initialize_errors
|
44
51
|
end
|
@@ -51,7 +58,6 @@ module Dry
|
|
51
58
|
def errors(new_options = EMPTY_HASH)
|
52
59
|
new_options.empty? ? @errors : @errors.with(schema_errors(new_options), new_options)
|
53
60
|
end
|
54
|
-
alias_method :messages, :errors
|
55
61
|
|
56
62
|
# Check if result is successful
|
57
63
|
#
|
@@ -82,7 +88,7 @@ module Dry
|
|
82
88
|
#
|
83
89
|
# @api private
|
84
90
|
def add_error(error)
|
85
|
-
errors.add(error)
|
91
|
+
@errors.add(error)
|
86
92
|
self
|
87
93
|
end
|
88
94
|
|
@@ -119,7 +125,11 @@ module Dry
|
|
119
125
|
#
|
120
126
|
# @api public
|
121
127
|
def inspect
|
122
|
-
|
128
|
+
if context.empty?
|
129
|
+
"#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
|
130
|
+
else
|
131
|
+
"#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} context=#{context.each.to_h.inspect}>"
|
132
|
+
end
|
123
133
|
end
|
124
134
|
|
125
135
|
# Freeze result and its error set
|
data/lib/dry/validation/rule.rb
CHANGED
@@ -9,7 +9,7 @@ module Dry
|
|
9
9
|
#
|
10
10
|
# @api private
|
11
11
|
class Rule
|
12
|
-
include Dry::Equalizer(:
|
12
|
+
include Dry::Equalizer(:keys, :block, inspect: false)
|
13
13
|
|
14
14
|
extend Dry::Initializer
|
15
15
|
|
@@ -33,6 +33,15 @@ module Dry
|
|
33
33
|
def call(contract, result, context)
|
34
34
|
Evaluator.new(contract, values: result, keys: keys, _context: context, &block)
|
35
35
|
end
|
36
|
+
|
37
|
+
# Return a nice string representation
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
#
|
41
|
+
# @api public
|
42
|
+
def inspect
|
43
|
+
%(#<#{self.class} keys=#{keys.inspect}>)
|
44
|
+
end
|
36
45
|
end
|
37
46
|
end
|
38
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -30,20 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
34
|
-
- - ">="
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: 0.2.1
|
33
|
+
version: '0.4'
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
38
|
- - "~>"
|
42
39
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0.
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 0.2.1
|
40
|
+
version: '0.4'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: dry-equalizer
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,34 +58,34 @@ dependencies:
|
|
64
58
|
requirements:
|
65
59
|
- - "~>"
|
66
60
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
61
|
+
version: '3.0'
|
68
62
|
type: :runtime
|
69
63
|
prerelease: false
|
70
64
|
version_requirements: !ruby/object:Gem::Requirement
|
71
65
|
requirements:
|
72
66
|
- - "~>"
|
73
67
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
68
|
+
version: '3.0'
|
75
69
|
- !ruby/object:Gem::Dependency
|
76
70
|
name: dry-schema
|
77
71
|
requirement: !ruby/object:Gem::Requirement
|
78
72
|
requirements:
|
79
73
|
- - ">="
|
80
74
|
- !ruby/object:Gem::Version
|
81
|
-
version: '0.
|
75
|
+
version: '0.6'
|
82
76
|
- - "~>"
|
83
77
|
- !ruby/object:Gem::Version
|
84
|
-
version: '0.
|
78
|
+
version: '0.6'
|
85
79
|
type: :runtime
|
86
80
|
prerelease: false
|
87
81
|
version_requirements: !ruby/object:Gem::Requirement
|
88
82
|
requirements:
|
89
83
|
- - ">="
|
90
84
|
- !ruby/object:Gem::Version
|
91
|
-
version: '0.
|
85
|
+
version: '0.6'
|
92
86
|
- - "~>"
|
93
87
|
- !ruby/object:Gem::Version
|
94
|
-
version: '0.
|
88
|
+
version: '0.6'
|
95
89
|
- !ruby/object:Gem::Dependency
|
96
90
|
name: bundler
|
97
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -151,6 +145,7 @@ files:
|
|
151
145
|
- lib/dry/validation/contract.rb
|
152
146
|
- lib/dry/validation/contract/class_interface.rb
|
153
147
|
- lib/dry/validation/evaluator.rb
|
148
|
+
- lib/dry/validation/extensions/hints.rb
|
154
149
|
- lib/dry/validation/extensions/monads.rb
|
155
150
|
- lib/dry/validation/message.rb
|
156
151
|
- lib/dry/validation/message_set.rb
|