rasti-form 3.0.0 → 3.1.0

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
- SHA1:
3
- metadata.gz: 9dd0424409c0c7232782c40cde8dbf7fa84752d3
4
- data.tar.gz: a7c8be9e98dcc9e6d52482469486ba643a0ffa33
2
+ SHA256:
3
+ metadata.gz: d866f9b7a43e27b5a12fbe5044672018066e99d45c2cce45bc7cb80baef095c1
4
+ data.tar.gz: 9c24281985c31043750f3998c6ddc857c30830abba2a6f6128d0cfe45c5d8432
5
5
  SHA512:
6
- metadata.gz: a2fb881aec7bca876dd58a3c2bf3e6dcca8ac3f13777437992f4e359d07856c9a7075e4b4c51882f41cb30ffacb8ebf85616632898efbfa02ccd7abea6a7e81c
7
- data.tar.gz: c77fed8829801cae565a567f3303b8c4271e5a9b3b79c7ac2f04475e48a9bc6d884e80e2253903a0374a4848d635cf0576a1b191044c850f6cf9aea70a373416
6
+ metadata.gz: ff8eab55df69a186a42353248bb9218ae79352057642db8ea96af007c3a2717ed908d7b1219e803af11ed87e2d0166557e22ab58c849bdffa1638353f66bc3c7
7
+ data.tar.gz: 3e102c9dae971bb5482665db5471bbf1420ba35b2c81960bcd02583b119970dd5ba54df79450fe19ab50a3134b619809d1d234a21380725375460b86b59dcd37
@@ -1 +1 @@
1
- ruby-2.4.0
1
+ ruby-2.5.7
@@ -1,15 +1,15 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 1.9.3
5
4
  - 2.0
6
5
  - 2.1
7
6
  - 2.2
8
- - 2.3.0
9
- - 2.4.0
10
- - 2.5.0
11
- - jruby-1.7.25
12
- - jruby-9.1.7.0
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
11
+ - 2.7
12
+ - jruby-9.2.9.0
13
13
  - ruby-head
14
14
  - jruby-head
15
15
 
@@ -17,7 +17,4 @@ matrix:
17
17
  fast_finish: true
18
18
  allow_failures:
19
19
  - rvm: ruby-head
20
- - rvm: jruby-head
21
-
22
- before_install:
23
- - gem install bundler
20
+ - rvm: jruby-head
data/README.md CHANGED
@@ -4,7 +4,6 @@
4
4
  [![Build Status](https://travis-ci.org/gabynaiman/rasti-form.svg?branch=master)](https://travis-ci.org/gabynaiman/rasti-form)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/rasti-form/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/rasti-form?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/gabynaiman/rasti-form.svg)](https://codeclimate.com/github/gabynaiman/rasti-form)
7
- [![Dependency Status](https://gemnasium.com/gabynaiman/rasti-form.svg)](https://gemnasium.com/gabynaiman/rasti-form)
8
7
 
9
8
  Forms validations and type casting
10
9
 
@@ -145,7 +145,11 @@ module Rasti
145
145
  end
146
146
 
147
147
  def assert_present(attribute)
148
- assert attribute, !fetch(attribute).nil?, 'not present'
148
+ assert attribute, !fetch(attribute).nil?, 'not present' unless errors.key? attribute
149
+ end
150
+
151
+ def assert_not_present(attribute)
152
+ assert attribute, fetch(attribute).nil?, 'is present'
149
153
  end
150
154
 
151
155
  def assert_not_empty(attribute)
@@ -2,21 +2,31 @@ module Rasti
2
2
  class Form
3
3
  module Types
4
4
  class String
5
-
6
- include Formatable
7
-
8
5
  class << self
9
6
 
10
7
  include Castable
11
8
 
12
9
  def [](format)
13
- new format
10
+ Class.new(self) do
11
+ @format = format.is_a?(::String) ? ::Regexp.new(format) : format
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ name || "#{superclass.name}[#{format.inspect}]"
14
17
  end
18
+ alias_method :inspect, :to_s
15
19
 
16
20
  private
17
21
 
22
+ attr_reader :format
23
+
18
24
  def valid?(value)
19
- !value.nil? && value.respond_to?(:to_s)
25
+ !value.nil? && value.respond_to?(:to_s) && valid_format?(value)
26
+ end
27
+
28
+ def valid_format?(value)
29
+ format.nil? || !value.to_s.match(format).nil?
20
30
  end
21
31
 
22
32
  def transform(value)
@@ -24,20 +34,6 @@ module Rasti
24
34
  end
25
35
 
26
36
  end
27
-
28
- def to_s
29
- "#{self.class}[#{format.inspect}]"
30
- end
31
- alias_method :inspect, :to_s
32
-
33
- private
34
-
35
- attr_reader :format
36
-
37
- def initialize(format)
38
- @format = format.is_a?(String) ? ::Regexp.new(format) : format
39
- end
40
-
41
37
  end
42
38
  end
43
39
  end
@@ -1,19 +1,7 @@
1
1
  module Rasti
2
2
  class Form
3
3
  module Types
4
- class UUID
5
- class << self
6
-
7
- include Formatable
8
-
9
- private
10
-
11
- def format
12
- /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+$/
13
- end
14
-
15
- end
16
- end
4
+ UUID = String[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+$/]
17
5
  end
18
6
  end
19
7
  end
@@ -23,6 +23,13 @@ module Rasti
23
23
  false
24
24
  end
25
25
 
26
+ def assert_not_error(key)
27
+ yield
28
+ true
29
+ rescue => error
30
+ assert key, false, error.message
31
+ end
32
+
26
33
  end
27
34
  end
28
35
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  class Form
3
- VERSION = '3.0.0'
3
+ VERSION = '3.1.0'
4
4
  end
5
5
  end
@@ -20,8 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'multi_require', '~> 1.0'
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.12'
24
- spec.add_development_dependency 'rake', '~> 11.0'
23
+ spec.add_development_dependency 'rake', '~> 12.0'
25
24
  spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
26
25
  spec.add_development_dependency 'minitest-colorin', '~> 0.1'
27
26
  spec.add_development_dependency 'minitest-line', '~> 0.6'
@@ -8,7 +8,7 @@ describe Rasti::Form do
8
8
 
9
9
  def build_form(&block)
10
10
  Class.new(Rasti::Form) do
11
- class_eval &block
11
+ class_eval(&block)
12
12
  end
13
13
  end
14
14
 
@@ -141,6 +141,23 @@ describe Rasti::Form do
141
141
 
142
142
  describe 'Validations' do
143
143
 
144
+ it 'Not error' do
145
+ form = build_form do
146
+ attribute :text, Rasti::Form::Types::String
147
+
148
+ def validate
149
+ assert_not_error :text do
150
+ raise 'Invalid text' if text.nil?
151
+ end
152
+ end
153
+ end
154
+
155
+ proc { form.new text: 'text' }.must_be_silent
156
+
157
+ error = proc { form.new }.must_raise Rasti::Form::ValidationError
158
+ error.message.must_equal 'Validation error: #<Rasti::Form[]> {"text":["Invalid text"]}'
159
+ end
160
+
144
161
  it 'Required' do
145
162
  form = build_form do
146
163
  attribute :text, Rasti::Form::Types::String
@@ -150,10 +167,42 @@ describe Rasti::Form do
150
167
  end
151
168
  end
152
169
 
170
+ proc { form.new text: 'text' }.must_be_silent
171
+
153
172
  error = proc { form.new }.must_raise Rasti::Form::ValidationError
154
173
  error.message.must_equal 'Validation error: #<Rasti::Form[]> {"text":["not present"]}'
155
174
  end
156
175
 
176
+ it 'Required when cast failed' do
177
+ form = build_form do
178
+ attribute :number, Rasti::Form::Types::Integer
179
+
180
+ def validate
181
+ assert_present :number
182
+ end
183
+ end
184
+
185
+ proc { form.new number: 1 }.must_be_silent
186
+
187
+ error = proc { form.new number: 'text' }.must_raise Rasti::Form::ValidationError
188
+ error.message.must_equal "Validation error: #<Rasti::Form[]> {\"number\":[\"Invalid cast: 'text' -> Rasti::Form::Types::Integer\"]}"
189
+ end
190
+
191
+ it 'Not required' do
192
+ form = build_form do
193
+ attribute :text, Rasti::Form::Types::String
194
+
195
+ def validate
196
+ assert_not_present :text
197
+ end
198
+ end
199
+
200
+ proc { form.new }.must_be_silent
201
+
202
+ error = proc { form.new text: 'text' }.must_raise Rasti::Form::ValidationError
203
+ error.message.must_equal 'Validation error: #<Rasti::Form[text: "text"]> {"text":["is present"]}'
204
+ end
205
+
157
206
  it 'Not empty string' do
158
207
  form = build_form do
159
208
  attribute :text, Rasti::Form::Types::String
@@ -163,6 +212,8 @@ describe Rasti::Form do
163
212
  end
164
213
  end
165
214
 
215
+ proc { form.new text: 'text' }.must_be_silent
216
+
166
217
  error = proc { form.new text: ' ' }.must_raise Rasti::Form::ValidationError
167
218
  error.message.must_equal 'Validation error: #<Rasti::Form[text: " "]> {"text":["is empty"]}'
168
219
  end
@@ -176,6 +227,8 @@ describe Rasti::Form do
176
227
  end
177
228
  end
178
229
 
230
+ proc { form.new array: ['text'] }.must_be_silent
231
+
179
232
  error = proc { form.new array: [] }.must_raise Rasti::Form::ValidationError
180
233
  error.message.must_equal 'Validation error: #<Rasti::Form[array: []]> {"array":["is empty"]}'
181
234
  end
@@ -189,6 +242,8 @@ describe Rasti::Form do
189
242
  end
190
243
  end
191
244
 
245
+ proc { form.new text: 'value_1' }.must_be_silent
246
+
192
247
  error = proc { form.new text: 'xyz' }.must_raise Rasti::Form::ValidationError
193
248
  error.message.must_equal 'Validation error: #<Rasti::Form[text: "xyz"]> {"text":["not included in \'value_1\', \'value_2\'"]}'
194
249
  end
@@ -203,11 +258,13 @@ describe Rasti::Form do
203
258
  end
204
259
  end
205
260
 
206
- from = '2018-01-01 15:30:00 +0000'
207
- to = '2018-01-01 03:10:00 +0000'
261
+ from = Time.parse '2018-01-01 03:10:00'
262
+ to = Time.parse '2018-01-01 15:30:00'
208
263
 
209
- error = proc { form.new from: from, to: to }.must_raise Rasti::Form::ValidationError
210
- error.message.must_equal "Validation error: #<Rasti::Form[from: #{from}, to: #{to}]> {\"from\":[\"invalid time range\"]}"
264
+ proc { form.new from: from, to: to }.must_be_silent
265
+
266
+ error = proc { form.new from: to.to_s, to: from.to_s }.must_raise Rasti::Form::ValidationError
267
+ error.message.must_equal "Validation error: #<Rasti::Form[from: #{to}, to: #{from}]> {\"from\":[\"invalid time range\"]}"
211
268
  end
212
269
 
213
270
  it 'Nested form' do
@@ -220,6 +277,8 @@ describe Rasti::Form do
220
277
  end
221
278
  end
222
279
 
280
+ proc { form.new range: {min: 1, max: 2} }.must_be_silent
281
+
223
282
  error = proc { form.new }.must_raise Rasti::Form::ValidationError
224
283
  error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["not present"],"range.max":["not present"]}'
225
284
  end
@@ -236,7 +295,9 @@ describe Rasti::Form do
236
295
 
237
296
  form = build_form do
238
297
  attribute :range, Rasti::Form::Types::Form[range]
239
- end
298
+ end
299
+
300
+ proc { form.new range: {min: 1, max: 2} }.must_be_silent
240
301
 
241
302
  error = proc { form.new range: {min: 2, max: 1} }.must_raise Rasti::Form::ValidationError
242
303
  error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["Min must be less than Max"]}'
@@ -4,7 +4,7 @@ describe Rasti::Form::Types::Regexp do
4
4
 
5
5
  ['[a-z]', /[a-z]/].each do |value|
6
6
  it "#{value.inspect} -> #{Regexp.new(value).inspect}" do
7
- Rasti::Form::Types::Regexp.cast(value).must_equal Regexp.new(value)
7
+ Rasti::Form::Types::Regexp.cast(value).must_equal Regexp.new(value).source
8
8
  end
9
9
  end
10
10
 
@@ -6,7 +6,7 @@ describe Rasti::Form::Types::String, 'Formatted' do
6
6
 
7
7
  ['user@mail.com'.to_sym, 'user.name-123@mail-test.com.ar'].each do |value|
8
8
  it "#{value.inspect} -> #{value.to_s}" do
9
- Rasti::Form::Types::String[email_regexp].cast(value).must_equal value
9
+ Rasti::Form::Types::String[email_regexp].cast(value).must_equal value.to_s
10
10
  end
11
11
  end
12
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-form
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-29 00:00:00.000000000 Z
11
+ date: 2020-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require
@@ -24,34 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.12'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.12'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '11.0'
33
+ version: '12.0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '11.0'
40
+ version: '12.0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: minitest
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -162,7 +148,6 @@ files:
162
148
  - lib/rasti/form.rb
163
149
  - lib/rasti/form/castable.rb
164
150
  - lib/rasti/form/errors.rb
165
- - lib/rasti/form/formatable.rb
166
151
  - lib/rasti/form/types/array.rb
167
152
  - lib/rasti/form/types/boolean.rb
168
153
  - lib/rasti/form/types/enum.rb
@@ -215,8 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
200
  - !ruby/object:Gem::Version
216
201
  version: '0'
217
202
  requirements: []
218
- rubyforge_project:
219
- rubygems_version: 2.6.8
203
+ rubygems_version: 3.0.6
220
204
  signing_key:
221
205
  specification_version: 4
222
206
  summary: Forms validations and type casting
@@ -1,19 +0,0 @@
1
- module Rasti
2
- class Form
3
- module Formatable
4
-
5
- include Castable
6
-
7
- private
8
-
9
- def valid?(value)
10
- (value.is_a?(::String) || value.is_a?(Symbol)) && value.to_s.match(format)
11
- end
12
-
13
- def transform(value)
14
- value
15
- end
16
-
17
- end
18
- end
19
- end