dry-validation 0.3.0 → 0.3.1
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 -1
- data/lib/dry/validation.rb +3 -0
- data/lib/dry/validation/error_compiler.rb +3 -1
- data/lib/dry/validation/messages/abstract.rb +4 -1
- data/lib/dry/validation/messages/i18n.rb +2 -2
- data/lib/dry/validation/messages/yaml.rb +2 -2
- data/lib/dry/validation/predicates.rb +7 -2
- data/lib/dry/validation/version.rb +1 -1
- data/spec/integration/custom_error_messages_spec.rb +34 -7
- data/spec/integration/localized_error_messages_spec.rb +3 -1
- data/spec/integration/messages/i18n_spec.rb +1 -1
- data/spec/shared/predicates.rb +4 -4
- data/spec/spec_helper.rb +7 -1
- data/spec/unit/error_compiler_spec.rb +7 -0
- data/spec/unit/predicates/size_spec.rb +17 -11
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0630ad4e7946a89c0562c0f064220eaa186e2157
|
4
|
+
data.tar.gz: 10200a87ef1ea0910e3c7acd490f36529df7e6b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e829d8b8a77b5ab749700b4a6340684503745008b39985a82bbdd604e00e4ab3e1c41ebbdfda37e1594de822cbc2457bfe88a58a625cfa2d3c51d0fd4ebf9c52
|
7
|
+
data.tar.gz: 59a24f1ee1d3036f9c6ed2232e2b809b2aa62529916b7e0c0a8490286569992cada68e9324e47b2776eecef144857ddd5cc44803f5da66fcf3572ca5ca253a36
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,16 @@
|
|
1
|
-
# v0.3.
|
1
|
+
# v0.3.1 2015-12-08
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Support for `Range` and `Array` as an argument in `size?` predicate (solnic)
|
6
|
+
|
7
|
+
### Fixed
|
8
|
+
|
9
|
+
* Error compiler returns an empty hash rather than a nil when there are no errors (solnic)
|
10
|
+
|
11
|
+
[Compare v0.3.0...v0.3.1](https://github.com/dryrb/dry-validation/compare/v0.3.0...v0.3.1)
|
12
|
+
|
13
|
+
# v0.3.0 2015-12-07
|
2
14
|
|
3
15
|
### Added
|
4
16
|
|
data/lib/dry/validation.rb
CHANGED
@@ -3,13 +3,15 @@ module Dry
|
|
3
3
|
class ErrorCompiler
|
4
4
|
attr_reader :messages, :options
|
5
5
|
|
6
|
+
DEFAULT_RESULT = {}.freeze
|
7
|
+
|
6
8
|
def initialize(messages, options = {})
|
7
9
|
@messages = messages
|
8
10
|
@options = options
|
9
11
|
end
|
10
12
|
|
11
13
|
def call(ast)
|
12
|
-
ast.map { |node| visit(node) }.reduce(:merge)
|
14
|
+
ast.map { |node| visit(node) }.reduce(:merge) || DEFAULT_RESULT
|
13
15
|
end
|
14
16
|
|
15
17
|
def with(options)
|
@@ -1,12 +1,15 @@
|
|
1
|
+
require 'pathname'
|
1
2
|
require 'thread_safe/cache'
|
2
3
|
|
3
4
|
module Dry
|
4
5
|
module Validation
|
5
6
|
module Messages
|
6
7
|
class Abstract
|
8
|
+
DEFAULT_PATH = Pathname(__dir__).join('../../../../config/errors.yml').realpath.freeze
|
9
|
+
|
7
10
|
extend Dry::Configurable
|
8
11
|
|
9
|
-
setting :
|
12
|
+
setting :paths, [DEFAULT_PATH]
|
10
13
|
setting :root, 'errors'.freeze
|
11
14
|
setting :lookup_options, [:root, :predicate, :rule, :val_type, :arg_type].freeze
|
12
15
|
|
@@ -6,7 +6,7 @@ module Dry
|
|
6
6
|
class Messages::I18n < Messages::Abstract
|
7
7
|
attr_reader :t
|
8
8
|
|
9
|
-
::I18n.load_path
|
9
|
+
::I18n.load_path.concat(config.paths)
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
@t = I18n.method(:t)
|
@@ -17,7 +17,7 @@ module Dry
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def key?(key, options)
|
20
|
-
I18n.exists?(key, options.fetch(:locale, I18n.default_locale))
|
20
|
+
::I18n.exists?(key, options.fetch(:locale, I18n.default_locale))
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -88,8 +88,13 @@ module Dry
|
|
88
88
|
!self[:lt?].(num, input)
|
89
89
|
end
|
90
90
|
|
91
|
-
predicate(:size?) do |
|
92
|
-
|
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
|
93
98
|
end
|
94
99
|
|
95
100
|
predicate(:min_size?) do |num, input|
|
@@ -1,7 +1,19 @@
|
|
1
|
-
|
1
|
+
require 'dry/validation/messages/i18n'
|
2
|
+
|
3
|
+
RSpec.describe Dry::Validation do
|
2
4
|
subject(:validation) { schema.new }
|
3
5
|
|
4
|
-
|
6
|
+
shared_context 'schema with customized messages' do
|
7
|
+
describe '#messages' do
|
8
|
+
it 'returns compiled error messages' do
|
9
|
+
expect(validation.(email: '').messages).to match_array([
|
10
|
+
[:email, [['Please provide your email', '']]]
|
11
|
+
])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'yaml' do
|
5
17
|
let(:schema) do
|
6
18
|
Class.new(Dry::Validation::Schema) do
|
7
19
|
configure do |config|
|
@@ -12,12 +24,27 @@ RSpec.describe Dry::Validation, 'with custom messages' do
|
|
12
24
|
end
|
13
25
|
end
|
14
26
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
27
|
+
include_context 'schema with customized messages'
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'i18n' do
|
31
|
+
context 'with custom messages set globally' do
|
32
|
+
before do
|
33
|
+
I18n.load_path << SPEC_ROOT.join('fixtures/locales/en.yml')
|
34
|
+
I18n.backend.load_translations
|
20
35
|
end
|
36
|
+
|
37
|
+
let(:schema) do
|
38
|
+
Class.new(Dry::Validation::Schema) do
|
39
|
+
configure do |config|
|
40
|
+
config.messages = :i18n
|
41
|
+
end
|
42
|
+
|
43
|
+
key(:email, &:filled?)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
include_context 'schema with customized messages'
|
21
48
|
end
|
22
49
|
end
|
23
50
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
require 'dry/validation/messages/i18n'
|
2
|
+
|
1
3
|
RSpec.describe Dry::Validation, 'with localized messages' do
|
2
4
|
subject(:validation) { schema.new }
|
3
5
|
|
4
6
|
before do
|
5
7
|
I18n.config.available_locales_set << :pl
|
6
|
-
I18n.load_path
|
8
|
+
I18n.load_path.concat(%w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") })
|
7
9
|
I18n.backend.load_translations
|
8
10
|
end
|
9
11
|
|
@@ -5,7 +5,7 @@ RSpec.describe Messages::I18n do
|
|
5
5
|
|
6
6
|
before do
|
7
7
|
I18n.config.available_locales_set << :pl
|
8
|
-
I18n.load_path
|
8
|
+
I18n.load_path.concat(%w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") })
|
9
9
|
I18n.backend.load_translations
|
10
10
|
end
|
11
11
|
|
data/spec/shared/predicates.rb
CHANGED
@@ -16,8 +16,8 @@ RSpec.shared_examples 'a passing predicate' do
|
|
16
16
|
let(:predicate) { Dry::Validation::Predicates[predicate_name] }
|
17
17
|
|
18
18
|
it do
|
19
|
-
arguments_list.each do |
|
20
|
-
expect(predicate.call(
|
19
|
+
arguments_list.each do |(left, right)|
|
20
|
+
expect(predicate.call(left, right)).to be(true)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -26,8 +26,8 @@ RSpec.shared_examples 'a failing predicate' do
|
|
26
26
|
let(:predicate) { Dry::Validation::Predicates[predicate_name] }
|
27
27
|
|
28
28
|
it do
|
29
|
-
arguments_list.each do |
|
30
|
-
expect(predicate.call(
|
29
|
+
arguments_list.each do |(left, right)|
|
30
|
+
expect(predicate.call(left, right)).to be(false)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'i18n'
|
4
3
|
require 'dry-validation'
|
5
4
|
|
6
5
|
begin
|
@@ -16,4 +15,11 @@ include Dry::Validation
|
|
16
15
|
|
17
16
|
RSpec.configure do |config|
|
18
17
|
config.disable_monkey_patching!
|
18
|
+
|
19
|
+
config.after do
|
20
|
+
if defined?(I18n)
|
21
|
+
I18n.load_path = Dry::Validation.messages_paths.dup
|
22
|
+
I18n.backend.reload!
|
23
|
+
end
|
24
|
+
end
|
19
25
|
end
|
@@ -7,43 +7,49 @@ RSpec.describe Dry::Validation::Predicates do
|
|
7
7
|
context 'when value size is equal to n' do
|
8
8
|
let(:arguments_list) do
|
9
9
|
[
|
10
|
-
[
|
10
|
+
[[8], 2],
|
11
11
|
[4, 'Jill'],
|
12
12
|
[2, { 1 => 'st', 2 => 'nd' }],
|
13
|
-
[8,
|
14
|
-
[
|
13
|
+
[8, 8],
|
14
|
+
[1..8, 5]
|
15
15
|
]
|
16
16
|
end
|
17
17
|
|
18
18
|
it_behaves_like 'a passing predicate'
|
19
19
|
end
|
20
20
|
|
21
|
-
context 'when value size is
|
21
|
+
context 'when value size is greater than n' do
|
22
22
|
let(:arguments_list) do
|
23
23
|
[
|
24
|
-
[
|
24
|
+
[[1, 2], 3],
|
25
25
|
[5, 'Jill'],
|
26
26
|
[3, { 1 => 'st', 2 => 'nd' }],
|
27
|
-
[
|
28
|
-
[
|
27
|
+
[1, 9],
|
28
|
+
[1..5, 6]
|
29
29
|
]
|
30
30
|
end
|
31
31
|
|
32
32
|
it_behaves_like 'a failing predicate'
|
33
33
|
end
|
34
34
|
|
35
|
-
context 'with value size is
|
35
|
+
context 'with value size is less than n' do
|
36
36
|
let(:arguments_list) do
|
37
37
|
[
|
38
|
-
[
|
38
|
+
[[1, 2], 1],
|
39
39
|
[3, 'Jill'],
|
40
40
|
[1, { 1 => 'st', 2 => 'nd' }],
|
41
|
-
[
|
42
|
-
[
|
41
|
+
[1, 7],
|
42
|
+
[1..5, 4]
|
43
43
|
]
|
44
44
|
end
|
45
45
|
|
46
46
|
it_behaves_like 'a failing predicate'
|
47
47
|
end
|
48
|
+
|
49
|
+
context 'with an unsupported size' do
|
50
|
+
it 'raises an error' do
|
51
|
+
expect { Predicates[:size?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
|
52
|
+
end
|
53
|
+
end
|
48
54
|
end
|
49
55
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Holland
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-12-
|
12
|
+
date: 2015-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-configurable
|
@@ -191,6 +191,7 @@ files:
|
|
191
191
|
- spec/integration/schema_spec.rb
|
192
192
|
- spec/shared/predicates.rb
|
193
193
|
- spec/spec_helper.rb
|
194
|
+
- spec/unit/error_compiler_spec.rb
|
194
195
|
- spec/unit/input_type_compiler_spec.rb
|
195
196
|
- spec/unit/predicate_spec.rb
|
196
197
|
- spec/unit/predicates/bool_spec.rb
|
@@ -264,6 +265,7 @@ test_files:
|
|
264
265
|
- spec/integration/schema_spec.rb
|
265
266
|
- spec/shared/predicates.rb
|
266
267
|
- spec/spec_helper.rb
|
268
|
+
- spec/unit/error_compiler_spec.rb
|
267
269
|
- spec/unit/input_type_compiler_spec.rb
|
268
270
|
- spec/unit/predicate_spec.rb
|
269
271
|
- spec/unit/predicates/bool_spec.rb
|