dry-validation 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ba7bd2c7d9b73c611e4ef91e34a4aedf18bc38b
4
- data.tar.gz: ad1046184040352140c06823150e09f5af13f6e5
3
+ metadata.gz: 8275fd162e586ba1fcbb31efe723708f553e31f6
4
+ data.tar.gz: d95d66f951d31033be04e04258b3ac0dca700c8a
5
5
  SHA512:
6
- metadata.gz: bdc8ccd55d16dbf914b03c346582ed67fb22fd415bc397d468b4febd34bf15c3eafe0764c9ec0259d3ec7ea3404d935b46a67e347e27122d95949a8402cb909e
7
- data.tar.gz: 0e6e8d169dd74bf80dda570838b89f8aa3488fb4e985b04d28d6da8ad458a0ff83fd0a798d8815dbdeb16d17c5128fd69521946bf1d7d6ff0aa0e3c7d398bd9c
6
+ metadata.gz: 147d0f89a321c42bfa3799f37c3bd5700faefd22189a1c8bd49af812de16f4de80ab0bb9a98b253c688c66582f433bccacaccbcda5f0820fb3d6fb13c9c26ae5
7
+ data.tar.gz: 7e32822ddbe928051bceab4e7bf4e0ad9378d63e5e95972c4c6f98056dd5a7a66a81e3e4b4a8c85c8391de782d95e1fdfd6932ffeeb96776743bf0f9cf1ee63e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # v0.9.5 2016-08-15
2
+
3
+ ### Fixed
4
+
5
+ * Infering multiple predicates with options works as expected ie `value(:str?, min_size?: 3, max_size?: 6)` (solnic)
6
+ * Default `locale` configured in `I18n` is now respected by the messages compiler (agustin + cavi21)
7
+
8
+ [Compare v0.9.4...v0.9.5](https://github.com/dryrb/dry-validation/compare/v0.9.4...v0.9.5)
9
+
1
10
  # v0.9.4 2016-08-11
2
11
 
3
12
  ### Fixed
@@ -11,7 +11,7 @@ module Dry
11
11
  @messages = messages
12
12
  @options = options
13
13
  @full = @options.fetch(:full, false)
14
- @locale = @options.fetch(:locale, :en)
14
+ @locale = @options.fetch(:locale, messages.default_locale)
15
15
  @default_lookup_options = { message_type: message_type, locale: locale }
16
16
  end
17
17
 
@@ -100,6 +100,10 @@ module Dry
100
100
  def cache
101
101
  @cache ||= self.class.cache[self]
102
102
  end
103
+
104
+ def default_locale
105
+ :en
106
+ end
103
107
  end
104
108
  end
105
109
  end
@@ -18,13 +18,18 @@ module Dry
18
18
  end
19
19
 
20
20
  def key?(key, options)
21
- ::I18n.exists?(key, options.fetch(:locale, I18n.default_locale))
21
+ ::I18n.exists?(key, options.fetch(:locale, default_locale)) ||
22
+ ::I18n.exists?(key, I18n.default_locale)
22
23
  end
23
24
 
24
25
  def merge(path)
25
26
  ::I18n.load_path << path
26
27
  self
27
28
  end
29
+
30
+ def default_locale
31
+ I18n.locale || I18n.default_locale || super
32
+ end
28
33
  end
29
34
  end
30
35
  end
@@ -34,11 +34,11 @@ module Dry
34
34
  end
35
35
 
36
36
  def get(key, options = {})
37
- data[key % { locale: options.fetch(:locale, :en) }]
37
+ data[key % { locale: options.fetch(:locale, default_locale) }]
38
38
  end
39
39
 
40
40
  def key?(key, options = {})
41
- data.key?(key % { locale: options.fetch(:locale, :en) })
41
+ data.key?(key % { locale: options.fetch(:locale, default_locale) })
42
42
  end
43
43
 
44
44
  def merge(overrides)
@@ -158,8 +158,8 @@ module Dry
158
158
  alias_method :>, :then
159
159
 
160
160
  def infer_predicates(predicates, macro = nil)
161
- predicates.map do |predicate|
162
- name, *args = ::Kernel.Array(predicate).first
161
+ predicates.flat_map(&::Kernel.method(:Array)).map do |predicate|
162
+ name, *args = ::Kernel.Array(predicate)
163
163
 
164
164
  if macro && INVALID_PREDICATES[macro].include?(name)
165
165
  ::Kernel.raise InvalidSchemaError, "you can't use #{name} predicate with #{macro} macro"
@@ -172,8 +172,8 @@ module Dry
172
172
  end
173
173
 
174
174
  def infer_predicates(predicates, infer_on = self)
175
- predicates.map { |predicate|
176
- name, *args = ::Kernel.Array(predicate).first
175
+ predicates.flat_map(&::Kernel.method(:Array)).map { |predicate|
176
+ name, *args = ::Kernel.Array(predicate)
177
177
 
178
178
  if name.is_a?(Schema)
179
179
  infer_on.schema(name)
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Validation
3
- VERSION = '0.9.4'.freeze
3
+ VERSION = '0.9.5'.freeze
4
4
  end
5
5
  end
@@ -5,6 +5,10 @@ pl:
5
5
  arg:
6
6
  default: "wielkość musi być równa %{size}"
7
7
  range: "wielkość musi być między %{size_left} a %{size_right}"
8
+ value:
9
+ string:
10
+ arg:
11
+ default: "wielkość musi być równa %{size}"
8
12
  rules:
9
13
  email:
10
14
  filled?: "Proszę podać adres email"
@@ -7,6 +7,7 @@ RSpec.describe Messages::I18n do
7
7
  I18n.config.available_locales_set << :pl
8
8
  I18n.load_path.concat(%w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") })
9
9
  I18n.backend.load_translations
10
+ I18n.locale = :pl
10
11
  end
11
12
 
12
13
  describe '#[]' do
@@ -18,58 +19,76 @@ RSpec.describe Messages::I18n do
18
19
  it 'returns a message for a predicate' do
19
20
  message = messages[:filled?, rule: :name]
20
21
 
21
- expect(message).to eql("must be filled")
22
+ expect(message).to eql("nie może być pusty")
22
23
  end
23
24
 
24
25
  it 'returns a message for a specific rule' do
25
26
  message = messages[:filled?, rule: :email]
26
27
 
27
- expect(message).to eql("Please provide your email")
28
+ expect(message).to eql("Proszę podać adres email")
28
29
  end
29
30
 
30
31
  it 'returns a message for a specific val type' do
31
32
  message = messages[:size?, rule: :pages, val_type: String]
32
33
 
33
- expect(message).to eql("length must be %{size}")
34
+ expect(message).to eql("wielkość musi być równa %{size}")
34
35
  end
35
36
 
36
37
  it 'returns a message for a specific rule and its default arg type' do
37
38
  message = messages[:size?, rule: :pages]
38
39
 
39
- expect(message).to eql("size must be %{size}")
40
+ expect(message).to eql("wielkość musi być równa %{size}")
40
41
  end
41
42
 
42
43
  it 'returns a message for a specific rule and its arg type' do
43
44
  message = messages[:size?, rule: :pages, arg_type: Range]
44
45
 
45
- expect(message).to eql("size must be within %{size_left} - %{size_right}")
46
+ expect(message).to eql("wielkość musi być między %{size_left} a %{size_right}")
46
47
  end
47
48
  end
48
49
 
49
50
  context 'with a different locale' do
50
51
  it 'returns a message for a predicate' do
51
- message = messages[:filled?, rule: :name, locale: :pl]
52
+ message = messages[:filled?, rule: :name, locale: :en]
52
53
 
53
- expect(message).to eql("nie może być pusty")
54
+ expect(message).to eql("must be filled")
54
55
  end
55
56
 
56
57
  it 'returns a message for a specific rule' do
57
- message = messages[:filled?, rule: :email, locale: :pl]
58
+ message = messages[:filled?, rule: :email, locale: :en]
58
59
 
59
- expect(message).to eql("Proszę podać adres email")
60
+ expect(message).to eql("Please provide your email")
60
61
  end
61
62
 
62
63
  it 'returns a message for a specific rule and its default arg type' do
63
- message = messages[:size?, rule: :pages, locale: :pl]
64
+ message = messages[:size?, rule: :pages, locale: :en]
64
65
 
65
- expect(message).to eql("wielkość musi być równa %{size}")
66
+ expect(message).to eql("size must be %{size}")
66
67
  end
67
68
 
68
69
  it 'returns a message for a specific rule and its arg type' do
69
- message = messages[:size?, rule: :pages, arg_type: Range, locale: :pl]
70
+ message = messages[:size?, rule: :pages, arg_type: Range, locale: :en]
70
71
 
71
- expect(message).to eql("wielkość musi być między %{size_left} a %{size_right}")
72
+ expect(message).to eql("size must be within %{size_left} - %{size_right}")
73
+ end
74
+ end
75
+
76
+ context 'fallbacking to I18n.default_locale with fallback backend config' do
77
+ before do
78
+ require "i18n/backend/fallbacks"
79
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
80
+ end
81
+
82
+ it 'returns a message for a predicate in the default_locale' do
83
+ message = messages[:even?, rule: :some_number]
84
+
85
+ expect(I18n.locale).to eql(:pl)
86
+ expect(message).to eql("must be even")
72
87
  end
73
88
  end
74
89
  end
90
+
91
+ after(:all) do
92
+ I18n.locale = I18n.default_locale
93
+ end
75
94
  end
@@ -36,6 +36,20 @@ RSpec.describe 'Macros #value' do
36
36
  end
37
37
  end
38
38
 
39
+ context 'with a second predicate with args' do
40
+ subject(:schema) do
41
+ Dry::Validation.Schema do
42
+ required(:name).value(:str?, min_size?: 3, max_size?: 6)
43
+ end
44
+ end
45
+
46
+ it 'generates str? & min_size? & max_size?' do
47
+ expect(schema.(name: 'fo').messages).to eql(
48
+ name: ['size cannot be less than 3', 'size cannot be greater than 6']
49
+ )
50
+ end
51
+ end
52
+
39
53
  context 'with a range arg' do
40
54
  subject(:schema) do
41
55
  Dry::Validation.Schema do
@@ -1,5 +1,5 @@
1
1
  RSpec.describe ErrorCompiler, '#call' do
2
- subject(:error_compiler) { ErrorCompiler.new({}) }
2
+ subject(:error_compiler) { ErrorCompiler.new( Messages.default ) }
3
3
 
4
4
  it 'returns an empty hash when there are no errors' do
5
5
  expect(error_compiler.([])).to be_empty
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.9.4
4
+ version: 0.9.5
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: 2016-08-11 00:00:00.000000000 Z
12
+ date: 2016-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby