rasti-form 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- 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/version.rb +1 -1
- data/lib/rasti/form.rb +1 -1
- data/rasti-form.gemspec +1 -1
- data/spec/types/array_spec.rb +38 -6
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26c1c1f5dc1233ecbda35f3e19c0a6b8e286405c
|
4
|
+
data.tar.gz: 465729370df0070a7c536b6fc39c11d92a3b27fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae1a7c147d82af7c2a3095a682e80899746fcc3b00aa44a0c4fb88842895f01daf577056be8b1eaa06351c0e85c12a03f4d0abe82db0ec910c97f789c98deb58
|
7
|
+
data.tar.gz: 40e2ffc9f4287a1c726c15f469575c5ed14bef3ae8a5f47eef0a49abd97b4ea127e78544cb831c7cf834bac2f1aedb30f717aba5aa1c75374847d388eb715a98
|
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
|
data/lib/rasti/form/version.rb
CHANGED
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
|
data/rasti-form.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
|
23
23
|
spec.add_development_dependency 'bundler', '~> 1.12'
|
24
24
|
spec.add_development_dependency 'rake', '~> 11.0'
|
25
|
-
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
|
26
26
|
spec.add_development_dependency 'minitest-colorin', '~> 0.1'
|
27
27
|
spec.add_development_dependency 'minitest-line', '~> 0.6'
|
28
28
|
spec.add_development_dependency 'simplecov', '~> 0.12'
|
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
|
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: 2.0.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: 2018-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_require
|
@@ -59,6 +59,9 @@ dependencies:
|
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '5.0'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '5.11'
|
62
65
|
type: :development
|
63
66
|
prerelease: false
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -66,6 +69,9 @@ dependencies:
|
|
66
69
|
- - "~>"
|
67
70
|
- !ruby/object:Gem::Version
|
68
71
|
version: '5.0'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '5.11'
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
76
|
name: minitest-colorin
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|