rasti-form 1.1.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +7 -10
- data/README.md +1 -1
- data/lib/rasti/form.rb +10 -2
- data/lib/rasti/form/castable.rb +1 -1
- data/lib/rasti/form/errors.rb +20 -0
- data/lib/rasti/form/types/array.rb +19 -1
- data/lib/rasti/form/types/io.rb +23 -0
- data/lib/rasti/form/types/regexp.rb +9 -14
- data/lib/rasti/form/types/string.rb +19 -2
- data/lib/rasti/form/types/uuid.rb +1 -13
- data/lib/rasti/form/validable.rb +7 -0
- data/lib/rasti/form/version.rb +1 -1
- data/rasti-form.gemspec +2 -3
- data/spec/form_spec.rb +80 -2
- data/spec/types/array_spec.rb +38 -6
- data/spec/types/io_spec.rb +18 -0
- data/spec/types/regexp_spec.rb +6 -8
- data/spec/types/string_formatted_spec.rb +20 -0
- metadata +16 -21
- data/lib/rasti/form/formatable.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d866f9b7a43e27b5a12fbe5044672018066e99d45c2cce45bc7cb80baef095c1
|
4
|
+
data.tar.gz: 9c24281985c31043750f3998c6ddc857c30830abba2a6f6128d0cfe45c5d8432
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8eab55df69a186a42353248bb9218ae79352057642db8ea96af007c3a2717ed908d7b1219e803af11ed87e2d0166557e22ab58c849bdffa1638353f66bc3c7
|
7
|
+
data.tar.gz: 3e102c9dae971bb5482665db5471bbf1420ba35b2c81960bcd02583b119970dd5ba54df79450fe19ab50a3134b619809d1d234a21380725375460b86b59dcd37
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.5.7
|
data/.travis.yml
CHANGED
@@ -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
|
9
|
-
- 2.4
|
10
|
-
- 2.5
|
11
|
-
-
|
12
|
-
-
|
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
|
|
@@ -94,6 +93,7 @@ form.to # => 2016-10-28 00:00:00 -0300
|
|
94
93
|
- Form
|
95
94
|
- Hash
|
96
95
|
- Integer
|
96
|
+
- IO
|
97
97
|
- Regexp
|
98
98
|
- String
|
99
99
|
- Symbol
|
data/lib/rasti/form.rb
CHANGED
@@ -98,7 +98,7 @@ module Rasti
|
|
98
98
|
rescue CastError => error
|
99
99
|
errors[attr_name] << error.message
|
100
100
|
|
101
|
-
rescue ValidationError => error
|
101
|
+
rescue MultiCastError, ValidationError => error
|
102
102
|
error.errors.each do |inner_name, inner_errors|
|
103
103
|
inner_errors.each { |message| errors["#{attr_name}.#{inner_name}"] << message }
|
104
104
|
end
|
@@ -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)
|
@@ -155,6 +159,10 @@ module Rasti
|
|
155
159
|
end
|
156
160
|
end
|
157
161
|
|
162
|
+
def assert_time_range(attribute_from, attribute_to)
|
163
|
+
assert attribute_from, public_send(attribute_from) <= public_send(attribute_to), 'invalid time range'
|
164
|
+
end
|
165
|
+
|
158
166
|
def assert_included_in(attribute, set)
|
159
167
|
if assert_present attribute
|
160
168
|
assert attribute, set.include?(fetch(attribute)), "not included in #{set.map { |e| e.is_a?(::String) ? "'#{e}'" : e.inspect }.join(', ')}"
|
data/lib/rasti/form/castable.rb
CHANGED
data/lib/rasti/form/errors.rb
CHANGED
@@ -23,6 +23,26 @@ module Rasti
|
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
+
class MultiCastError < StandardError
|
27
|
+
|
28
|
+
attr_reader :type, :value, :errors
|
29
|
+
|
30
|
+
def initialize(type, value, errors)
|
31
|
+
@type = type
|
32
|
+
@value = value
|
33
|
+
@errors = errors
|
34
|
+
end
|
35
|
+
|
36
|
+
def message
|
37
|
+
"Invalid cast: #{display_value} -> #{type} - #{JSON.dump(errors)}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def display_value
|
41
|
+
value.is_a?(::String) ? "'#{value}'" : value.inspect
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
26
46
|
class ValidationError < StandardError
|
27
47
|
|
28
48
|
attr_reader :scope, :errors
|
@@ -27,7 +27,25 @@ module Rasti
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def transform(value)
|
30
|
-
|
30
|
+
result = []
|
31
|
+
errors = {}
|
32
|
+
|
33
|
+
value.each_with_index do |e,i|
|
34
|
+
index = i + 1
|
35
|
+
begin
|
36
|
+
result << type.cast(e)
|
37
|
+
rescue ValidationError => error
|
38
|
+
error.errors.each do |k,v|
|
39
|
+
errors["#{index}.#{k}"] = v
|
40
|
+
end
|
41
|
+
rescue => error
|
42
|
+
errors[index] = [error.message]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
raise MultiCastError.new(self, value, errors) unless errors.empty?
|
47
|
+
|
48
|
+
result
|
31
49
|
end
|
32
50
|
|
33
51
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Rasti
|
2
|
+
class Form
|
3
|
+
module Types
|
4
|
+
class IO
|
5
|
+
class << self
|
6
|
+
|
7
|
+
include Castable
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid?(value)
|
12
|
+
value.respond_to?(:read) && value.respond_to?(:write)
|
13
|
+
end
|
14
|
+
|
15
|
+
def transform(value)
|
16
|
+
value
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -2,26 +2,21 @@ module Rasti
|
|
2
2
|
class Form
|
3
3
|
module Types
|
4
4
|
class Regexp
|
5
|
+
class << self
|
5
6
|
|
6
|
-
|
7
|
+
include Castable
|
7
8
|
|
8
|
-
|
9
|
-
new format
|
10
|
-
end
|
9
|
+
private
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
alias_method :inspect, :to_s
|
11
|
+
def valid?(value)
|
12
|
+
value.is_a?(::Regexp) || value.is_a?(::String)
|
13
|
+
end
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def transform(value)
|
16
|
+
value.is_a?(::Regexp) ? value.source : ::Regexp.compile(value).source
|
17
|
+
end
|
20
18
|
|
21
|
-
def initialize(format)
|
22
|
-
@format = format.is_a?(String) ? ::Regexp.new(format) : format
|
23
19
|
end
|
24
|
-
|
25
20
|
end
|
26
21
|
end
|
27
22
|
end
|
@@ -5,11 +5,28 @@ module Rasti
|
|
5
5
|
class << self
|
6
6
|
|
7
7
|
include Castable
|
8
|
-
|
8
|
+
|
9
|
+
def [](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}]"
|
17
|
+
end
|
18
|
+
alias_method :inspect, :to_s
|
19
|
+
|
9
20
|
private
|
10
21
|
|
22
|
+
attr_reader :format
|
23
|
+
|
11
24
|
def valid?(value)
|
12
|
-
!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?
|
13
30
|
end
|
14
31
|
|
15
32
|
def transform(value)
|
@@ -1,19 +1,7 @@
|
|
1
1
|
module Rasti
|
2
2
|
class Form
|
3
3
|
module Types
|
4
|
-
|
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
|
data/lib/rasti/form/validable.rb
CHANGED
data/lib/rasti/form/version.rb
CHANGED
data/rasti-form.gemspec
CHANGED
@@ -20,9 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'multi_require', '~> 1.0'
|
22
22
|
|
23
|
-
spec.add_development_dependency '
|
24
|
-
spec.add_development_dependency '
|
25
|
-
spec.add_development_dependency 'minitest', '~> 5.0'
|
23
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
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'
|
28
27
|
spec.add_development_dependency 'simplecov', '~> 0.12'
|
data/spec/form_spec.rb
CHANGED
@@ -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
|
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,10 +242,31 @@ 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
|
195
250
|
|
251
|
+
it 'Time range' do
|
252
|
+
form = build_form do
|
253
|
+
attribute :from, Rasti::Form::Types::Time['%Y-%m-%d %H:%M:%S %Z']
|
254
|
+
attribute :to, Rasti::Form::Types::Time['%Y-%m-%d %H:%M:%S %Z']
|
255
|
+
|
256
|
+
def validate
|
257
|
+
assert_time_range :from, :to
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
from = Time.parse '2018-01-01 03:10:00'
|
262
|
+
to = Time.parse '2018-01-01 15:30:00'
|
263
|
+
|
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\"]}"
|
268
|
+
end
|
269
|
+
|
196
270
|
it 'Nested form' do
|
197
271
|
form = build_form do
|
198
272
|
attribute :range, Rasti::Form::Types::Form[min: Rasti::Form::Types::Integer, max: Rasti::Form::Types::Integer]
|
@@ -203,6 +277,8 @@ describe Rasti::Form do
|
|
203
277
|
end
|
204
278
|
end
|
205
279
|
|
280
|
+
proc { form.new range: {min: 1, max: 2} }.must_be_silent
|
281
|
+
|
206
282
|
error = proc { form.new }.must_raise Rasti::Form::ValidationError
|
207
283
|
error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["not present"],"range.max":["not present"]}'
|
208
284
|
end
|
@@ -219,7 +295,9 @@ describe Rasti::Form do
|
|
219
295
|
|
220
296
|
form = build_form do
|
221
297
|
attribute :range, Rasti::Form::Types::Form[range]
|
222
|
-
end
|
298
|
+
end
|
299
|
+
|
300
|
+
proc { form.new range: {min: 1, max: 2} }.must_be_silent
|
223
301
|
|
224
302
|
error = proc { form.new range: {min: 2, max: 1} }.must_raise Rasti::Form::ValidationError
|
225
303
|
error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["Min must be less than Max"]}'
|
data/spec/types/array_spec.rb
CHANGED
@@ -2,21 +2,53 @@ require 'minitest_helper'
|
|
2
2
|
|
3
3
|
describe Rasti::Form::Types::Array do
|
4
4
|
|
5
|
-
|
5
|
+
VALID_ARRAY = [1, '2', Time.now]
|
6
|
+
INVALID_ARRAY = [nil, 1, 'text', :symbol, {a: 1, b: 2}, Object.new]
|
6
7
|
|
7
|
-
it "#{
|
8
|
-
Rasti::Form::Types::Array[Rasti::Form::Types::Integer].cast(
|
8
|
+
it "#{VALID_ARRAY.inspect} -> #{VALID_ARRAY.map(&:to_i)}" do
|
9
|
+
Rasti::Form::Types::Array[Rasti::Form::Types::Integer].cast(VALID_ARRAY).must_equal VALID_ARRAY.map(&:to_i)
|
9
10
|
end
|
10
11
|
|
11
|
-
it "#{
|
12
|
-
Rasti::Form::Types::Array[Rasti::Form::Types::String].cast(
|
12
|
+
it "#{VALID_ARRAY.inspect} -> #{VALID_ARRAY.map(&:to_s)}" do
|
13
|
+
Rasti::Form::Types::Array[Rasti::Form::Types::String].cast(VALID_ARRAY).must_equal VALID_ARRAY.map(&:to_s)
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
|
+
INVALID_ARRAY.each do |value|
|
16
17
|
it "#{value.inspect} -> CastError" do
|
17
18
|
error = proc { Rasti::Form::Types::Array[Rasti::Form::Types::String].cast(value) }.must_raise Rasti::Form::CastError
|
18
19
|
error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Array[Rasti::Form::Types::String]"
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
23
|
+
describe 'Multi cast errors' do
|
24
|
+
|
25
|
+
it 'Array of integers' do
|
26
|
+
array = [1, 2 , 'a', 3, 'c', 4, nil]
|
27
|
+
error = proc { Rasti::Form::Types::Array[Rasti::Form::Types::Integer].cast(array) }.must_raise Rasti::Form::MultiCastError
|
28
|
+
error.errors.must_equal 3 => ["Invalid cast: 'a' -> Rasti::Form::Types::Integer"],
|
29
|
+
5 => ["Invalid cast: 'c' -> Rasti::Form::Types::Integer"],
|
30
|
+
7 => ["Invalid cast: nil -> Rasti::Form::Types::Integer"]
|
31
|
+
error.message.must_equal "Invalid cast: [1, 2, \"a\", 3, \"c\", 4, nil] -> Rasti::Form::Types::Array[Rasti::Form::Types::Integer] - {\"3\":[\"Invalid cast: 'a' -> Rasti::Form::Types::Integer\"],\"5\":[\"Invalid cast: 'c' -> Rasti::Form::Types::Integer\"],\"7\":[\"Invalid cast: nil -> Rasti::Form::Types::Integer\"]}"
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Array of forms' do
|
35
|
+
inner_form_class = Rasti::Form::Types::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
|
36
|
+
form_class = Rasti::Form[points: Rasti::Form::Types::Array[inner_form_class]]
|
37
|
+
|
38
|
+
error = proc do
|
39
|
+
form_class.new points: [
|
40
|
+
{x: 1, y: 2},
|
41
|
+
{x: 'a', y: 2},
|
42
|
+
{x: 1, y: 'b'},
|
43
|
+
{x: 3, y: 4}
|
44
|
+
]
|
45
|
+
end.must_raise Rasti::Form::ValidationError
|
46
|
+
|
47
|
+
error.errors.must_equal 'points.2.x' => ["Invalid cast: 'a' -> Rasti::Form::Types::Integer"],
|
48
|
+
'points.3.y' => ["Invalid cast: 'b' -> Rasti::Form::Types::Integer"]
|
49
|
+
error.message.must_equal "Validation error: #<Rasti::Form[]> {\"points.2.x\":[\"Invalid cast: 'a' -> Rasti::Form::Types::Integer\"],\"points.3.y\":[\"Invalid cast: 'b' -> Rasti::Form::Types::Integer\"]}"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
22
54
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Rasti::Form::Types::IO do
|
4
|
+
|
5
|
+
[StringIO.new, File.new(__FILE__)].each do |value|
|
6
|
+
it "#{value.inspect} -> #{value}" do
|
7
|
+
Rasti::Form::Types::IO.cast(value).must_equal value
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
[nil, 'text', 123, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
|
12
|
+
it "#{value.inspect} -> CastError" do
|
13
|
+
error = proc { Rasti::Form::Types::IO.cast(value) }.must_raise Rasti::Form::CastError
|
14
|
+
error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::IO"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/spec/types/regexp_spec.rb
CHANGED
@@ -2,18 +2,16 @@ require 'minitest_helper'
|
|
2
2
|
|
3
3
|
describe Rasti::Form::Types::Regexp do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
it "#{value.inspect} -> #{value.to_s}" do
|
9
|
-
Rasti::Form::Types::Regexp[email_regexp].cast(value).must_equal value
|
5
|
+
['[a-z]', /[a-z]/].each do |value|
|
6
|
+
it "#{value.inspect} -> #{Regexp.new(value).inspect}" do
|
7
|
+
Rasti::Form::Types::Regexp.cast(value).must_equal Regexp.new(value).source
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
13
|
-
[nil, '
|
11
|
+
[nil, '[a-z', :symbol, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
|
14
12
|
it "#{value.inspect} -> CastError" do
|
15
|
-
error = proc { Rasti::Form::Types::Regexp
|
16
|
-
error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Regexp
|
13
|
+
error = proc { Rasti::Form::Types::Regexp.cast(value) }.must_raise Rasti::Form::CastError
|
14
|
+
error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Regexp"
|
17
15
|
end
|
18
16
|
end
|
19
17
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Rasti::Form::Types::String, 'Formatted' do
|
4
|
+
|
5
|
+
email_regexp = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
|
6
|
+
|
7
|
+
['user@mail.com'.to_sym, 'user.name-123@mail-test.com.ar'].each do |value|
|
8
|
+
it "#{value.inspect} -> #{value.to_s}" do
|
9
|
+
Rasti::Form::Types::String[email_regexp].cast(value).must_equal value.to_s
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
[nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
|
14
|
+
it "#{value.inspect} -> CastError" do
|
15
|
+
error = proc { Rasti::Form::Types::String[email_regexp].cast(value) }.must_raise Rasti::Form::CastError
|
16
|
+
error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::String[#{as_string(email_regexp)}]"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
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:
|
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:
|
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: '
|
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: '
|
40
|
+
version: '12.0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: minitest
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,6 +45,9 @@ dependencies:
|
|
59
45
|
- - "~>"
|
60
46
|
- !ruby/object:Gem::Version
|
61
47
|
version: '5.0'
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '5.11'
|
62
51
|
type: :development
|
63
52
|
prerelease: false
|
64
53
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -66,6 +55,9 @@ dependencies:
|
|
66
55
|
- - "~>"
|
67
56
|
- !ruby/object:Gem::Version
|
68
57
|
version: '5.0'
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '5.11'
|
69
61
|
- !ruby/object:Gem::Dependency
|
70
62
|
name: minitest-colorin
|
71
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -156,7 +148,6 @@ files:
|
|
156
148
|
- lib/rasti/form.rb
|
157
149
|
- lib/rasti/form/castable.rb
|
158
150
|
- lib/rasti/form/errors.rb
|
159
|
-
- lib/rasti/form/formatable.rb
|
160
151
|
- lib/rasti/form/types/array.rb
|
161
152
|
- lib/rasti/form/types/boolean.rb
|
162
153
|
- lib/rasti/form/types/enum.rb
|
@@ -164,6 +155,7 @@ files:
|
|
164
155
|
- lib/rasti/form/types/form.rb
|
165
156
|
- lib/rasti/form/types/hash.rb
|
166
157
|
- lib/rasti/form/types/integer.rb
|
158
|
+
- lib/rasti/form/types/io.rb
|
167
159
|
- lib/rasti/form/types/regexp.rb
|
168
160
|
- lib/rasti/form/types/string.rb
|
169
161
|
- lib/rasti/form/types/symbol.rb
|
@@ -182,7 +174,9 @@ files:
|
|
182
174
|
- spec/types/form_spec.rb
|
183
175
|
- spec/types/hash_spec.rb
|
184
176
|
- spec/types/integer_spec.rb
|
177
|
+
- spec/types/io_spec.rb
|
185
178
|
- spec/types/regexp_spec.rb
|
179
|
+
- spec/types/string_formatted_spec.rb
|
186
180
|
- spec/types/string_spec.rb
|
187
181
|
- spec/types/symbol_spec.rb
|
188
182
|
- spec/types/time_spec.rb
|
@@ -206,8 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
200
|
- !ruby/object:Gem::Version
|
207
201
|
version: '0'
|
208
202
|
requirements: []
|
209
|
-
|
210
|
-
rubygems_version: 2.6.8
|
203
|
+
rubygems_version: 3.0.6
|
211
204
|
signing_key:
|
212
205
|
specification_version: 4
|
213
206
|
summary: Forms validations and type casting
|
@@ -222,7 +215,9 @@ test_files:
|
|
222
215
|
- spec/types/form_spec.rb
|
223
216
|
- spec/types/hash_spec.rb
|
224
217
|
- spec/types/integer_spec.rb
|
218
|
+
- spec/types/io_spec.rb
|
225
219
|
- spec/types/regexp_spec.rb
|
220
|
+
- spec/types/string_formatted_spec.rb
|
226
221
|
- spec/types/string_spec.rb
|
227
222
|
- spec/types/symbol_spec.rb
|
228
223
|
- spec/types/time_spec.rb
|
@@ -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
|