dry-validation 0.7.1 → 0.7.2

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: 02681fc0853b4f66dc7951e078dd5ef37559cbe6
4
- data.tar.gz: 214d78242cec3b96d1ad065b403596c6876491c8
3
+ metadata.gz: 3c372ced33f2c8865c3ecac3fe917610dc414d89
4
+ data.tar.gz: 50e61628de4b84caf57bcc874fbf74a4ba3925b8
5
5
  SHA512:
6
- metadata.gz: 1d2b0bd2955cec709349b1060913eb6bfee39e55d3b9939ddff63def27a5aaa6360a1599f1afb8442dfe0b2a4326a61adcd2af6b2e4e91c2c928d5f419a7cd9f
7
- data.tar.gz: f17b2305e70319088bf4028354640eb18c37b1d88ab2ad2d29cbf2b504704a7cec594ee0115ba334b96ccfcc02b36e9db466498d6934ece8ff743884582885d8
6
+ metadata.gz: 2a79910c2cd2e54c8076ca4b218976d972d06f99db6063005d57d7c9e47f96433bd380a53c4504a76c41cc4a810706fb383e3c298c2122416578276626b1fa2b
7
+ data.tar.gz: d731cd50af1a2b58ac03baccb1706fb2cb66acbb98ae4931f363d19fb13eebada0b766d8030a3404efe5a30d43264805a69321518467777c3a8f639b2f0e5596
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # v0.7.2 2016-03-28
2
+
3
+ ### Added
4
+
5
+ * Support for nested schemas inside high-level rules (solnic)
6
+ * `Schema#to_proc` so that you can do `data.each(&schema)` (solnic)
7
+
8
+ [Compare v0.7.1...v0.7.2](https://github.com/dryrb/dry-validation/compare/v0.7.1...v0.7.2)
9
+
1
10
  # v0.7.1 2016-03-21
2
11
 
3
12
  ### Added
@@ -191,6 +191,14 @@ module Dry
191
191
  end
192
192
  end
193
193
 
194
+ def curry(*args)
195
+ to_proc.curry.(*args)
196
+ end
197
+
198
+ def to_proc
199
+ -> input { self.call(input) }
200
+ end
201
+
194
202
  private
195
203
 
196
204
  def apply(input)
@@ -6,6 +6,16 @@ module Dry
6
6
  Check
7
7
  end
8
8
 
9
+ def schema(other = nil, &block)
10
+ schema = Schema.create_class(self, other, &block)
11
+
12
+ if other
13
+ schema.config.input_processor = other.class.config.input_processor
14
+ end
15
+
16
+ hash?.and(create_rule([:check, [name, schema.to_ast], [path]]))
17
+ end
18
+
9
19
  private
10
20
 
11
21
  def method_missing(meth, *meth_args)
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Validation
3
- VERSION = '0.7.1'.freeze
3
+ VERSION = '0.7.2'.freeze
4
4
  end
5
5
  end
@@ -79,4 +79,41 @@ RSpec.describe Schema, 'using high-level rules' do
79
79
  )
80
80
  end
81
81
  end
82
+
83
+ describe 'with nested schemas' do
84
+ subject(:schema) do
85
+ Dry::Validation.Schema do
86
+ key(:command).required(:str?, inclusion?: %w(First Second))
87
+
88
+ key(:args).required(:hash?)
89
+
90
+ rule(first_args: [:command, :args]) do |command, args|
91
+ command.eql?('First')
92
+ .then(args.schema { key(:first).required(:bool?) })
93
+ end
94
+
95
+ rule(second_args: [:command, :args]) do |command, args|
96
+ command.eql?('Second')
97
+ .then(args.schema { key(:second).required(:bool?) })
98
+ end
99
+ end
100
+ end
101
+
102
+ it 'generates check rule matching on value' do
103
+ expect(schema.(command: 'First', args: { first: true })).to be_success
104
+ expect(schema.(command: 'Second', args: { second: true })).to be_success
105
+
106
+ expect(schema.(command: 'oops', args: { such: 'validation' }).messages).to eql(
107
+ command: ['must be one of: First, Second']
108
+ )
109
+
110
+ expect(schema.(command: 'First', args: { second: true }).messages).to eql(
111
+ args: { first: ['is missing'] }
112
+ )
113
+
114
+ expect(schema.(command: 'Second', args: { first: true }).messages).to eql(
115
+ args: { second: ['is missing'] }
116
+ )
117
+ end
118
+ end
82
119
  end
@@ -119,4 +119,42 @@ RSpec.describe Dry::Validation::Schema::Form, 'defining a schema' do
119
119
  )
120
120
  end
121
121
  end
122
+
123
+ describe 'with nested schema in a high-level rule' do
124
+ subject(:schema) do
125
+ Dry::Validation.Form do
126
+ key(:address).maybe
127
+
128
+ key(:delivery).required(:bool?)
129
+
130
+ rule(address: [:delivery, :address]) do |delivery, address|
131
+ delivery.true?.then(address.schema(AddressSchema))
132
+ end
133
+ end
134
+ end
135
+
136
+ before do
137
+ AddressSchema = Dry::Validation.Form do
138
+ key(:city).required
139
+ key(:zipcode).required(:int?)
140
+ end
141
+ end
142
+
143
+ after do
144
+ Object.send(:remove_const, :AddressSchema)
145
+ end
146
+
147
+ it 'applies nested form schema' do
148
+ result = schema.(delivery: '1', address: { city: 'NYC', zipcode: '123' })
149
+ expect(result).to be_success
150
+
151
+ result = schema.(delivery: '1', address: { city: 'NYC', zipcode: 'foo' })
152
+ expect(result.messages).to eql(
153
+ address: { zipcode: ['must be an integer'] }
154
+ )
155
+
156
+ result = schema.(delivery: '0', address: nil)
157
+ expect(result).to be_success
158
+ end
159
+ end
122
160
  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.7.1
4
+ version: 0.7.2
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-03-21 00:00:00.000000000 Z
12
+ date: 2016-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-configurable
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  requirements: []
258
258
  rubyforge_project:
259
- rubygems_version: 2.4.5.1
259
+ rubygems_version: 2.5.1
260
260
  signing_key:
261
261
  specification_version: 4
262
262
  summary: A simple validation library
@@ -301,4 +301,3 @@ test_files:
301
301
  - spec/unit/schema/rule_spec.rb
302
302
  - spec/unit/schema/value_spec.rb
303
303
  - spec/unit/schema_spec.rb
304
- has_rdoc: