rasti-form 3.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +8 -11
  4. data/README.md +0 -1
  5. data/lib/rasti/form.rb +34 -129
  6. data/lib/rasti/form/errors.rb +7 -47
  7. data/lib/rasti/form/validable.rb +9 -3
  8. data/rasti-form.gemspec +3 -14
  9. data/spec/coverage_helper.rb +1 -3
  10. data/spec/minitest_helper.rb +1 -7
  11. data/spec/validations_spec.rb +301 -0
  12. metadata +11 -56
  13. data/lib/rasti/form/castable.rb +0 -25
  14. data/lib/rasti/form/formatable.rb +0 -19
  15. data/lib/rasti/form/types/array.rb +0 -54
  16. data/lib/rasti/form/types/boolean.rb +0 -42
  17. data/lib/rasti/form/types/enum.rb +0 -48
  18. data/lib/rasti/form/types/float.rb +0 -33
  19. data/lib/rasti/form/types/form.rb +0 -40
  20. data/lib/rasti/form/types/hash.rb +0 -39
  21. data/lib/rasti/form/types/integer.rb +0 -21
  22. data/lib/rasti/form/types/io.rb +0 -23
  23. data/lib/rasti/form/types/regexp.rb +0 -23
  24. data/lib/rasti/form/types/string.rb +0 -44
  25. data/lib/rasti/form/types/symbol.rb +0 -23
  26. data/lib/rasti/form/types/time.rb +0 -40
  27. data/lib/rasti/form/types/uuid.rb +0 -19
  28. data/lib/rasti/form/version.rb +0 -5
  29. data/spec/form_spec.rb +0 -350
  30. data/spec/types/array_spec.rb +0 -54
  31. data/spec/types/boolean_spec.rb +0 -24
  32. data/spec/types/enum_spec.rb +0 -28
  33. data/spec/types/float_spec.rb +0 -18
  34. data/spec/types/form_spec.rb +0 -41
  35. data/spec/types/hash_spec.rb +0 -20
  36. data/spec/types/integer_spec.rb +0 -18
  37. data/spec/types/io_spec.rb +0 -18
  38. data/spec/types/regexp_spec.rb +0 -18
  39. data/spec/types/string_formatted_spec.rb +0 -20
  40. data/spec/types/string_spec.rb +0 -16
  41. data/spec/types/symbol_spec.rb +0 -16
  42. data/spec/types/time_spec.rb +0 -25
  43. data/spec/types/uuid_spec.rb +0 -18
@@ -1,54 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Array do
4
-
5
- VALID_ARRAY = [1, '2', Time.now]
6
- INVALID_ARRAY = [nil, 1, 'text', :symbol, {a: 1, b: 2}, Object.new]
7
-
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)
10
- end
11
-
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)
14
- end
15
-
16
- INVALID_ARRAY.each do |value|
17
- it "#{value.inspect} -> CastError" do
18
- error = proc { Rasti::Form::Types::Array[Rasti::Form::Types::String].cast(value) }.must_raise Rasti::Form::CastError
19
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Array[Rasti::Form::Types::String]"
20
- end
21
- end
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
-
54
- end
@@ -1,24 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Boolean do
4
-
5
- [true, 'true', 'True', 'TRUE', 'T'].each do |value|
6
- it "#{value.inspect} -> true" do
7
- Rasti::Form::Types::Boolean.cast(value).must_equal true
8
- end
9
- end
10
-
11
- [false, 'false', 'False', 'FALSE', 'F'].each do |value|
12
- it "#{value.inspect} -> false" do
13
- Rasti::Form::Types::Boolean.cast(value).must_equal false
14
- end
15
- end
16
-
17
- [nil, 'text', 123, :false, :true, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
18
- it "#{value.inspect} -> CastError" do
19
- error = proc { Rasti::Form::Types::Boolean.cast(value) }.must_raise Rasti::Form::CastError
20
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Boolean"
21
- end
22
- end
23
-
24
- end
@@ -1,28 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Enum do
4
-
5
- enum = Rasti::Form::Types::Enum[:a,:b,:c]
6
-
7
- [:a, 'b', "c"].each do |value|
8
- it "#{value.inspect} -> #{enum}" do
9
- Rasti::Form::Types::Enum[:a,:b,:c].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].each do |value|
14
- it "#{value.inspect} -> CastError" do
15
- error = proc { Rasti::Form::Types::Enum[:a,:b,:c].cast(value) }.must_raise Rasti::Form::CastError
16
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Enum[\"a\", \"b\", \"c\"]"
17
- end
18
- end
19
-
20
- it 'Constants' do
21
- enum = Rasti::Form::Types::Enum[:first_value, 'SecondValue', 'THIRD_VALUE']
22
-
23
- enum.first_value.must_equal 'first_value'
24
- enum.second_value.must_equal 'SecondValue'
25
- enum.third_value.must_equal 'THIRD_VALUE'
26
- end
27
-
28
- end
@@ -1,18 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Float do
4
-
5
- [100, '200', Time.now,2.0,"12.5"].each do |value|
6
- it "#{value.inspect} -> #{value.to_i}" do
7
- Rasti::Form::Types::Float.cast(value).must_equal value.to_f
8
- end
9
- end
10
-
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, "1.", ".2","."].each do |value|
12
- it "#{value.inspect} -> CastError" do
13
- error = proc { Rasti::Form::Types::Float.cast(value) }.must_raise Rasti::Form::CastError
14
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Float"
15
- end
16
- end
17
-
18
- end
@@ -1,41 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Form do
4
-
5
- form = Rasti::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
6
-
7
- hash = {x: '1', y: '2'}
8
-
9
- it 'Class' do
10
- result = Rasti::Form::Types::Form[form].cast hash
11
- result.must_be_instance_of form
12
- result.x.must_equal 1
13
- result.y.must_equal 2
14
- end
15
-
16
- it 'Inline' do
17
- type = Rasti::Form::Types::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
18
- result = type.cast hash
19
- result.must_be_instance_of type.form_class
20
- result.x.must_equal 1
21
- result.y.must_equal 2
22
- end
23
-
24
- it 'Invalid form class' do
25
- error = proc { Rasti::Form::Types::Form[1] }.must_raise ArgumentError
26
- error.message.must_equal 'Invalid form specification: 1'
27
- end
28
-
29
- [nil, 'text', :symbol, 1, [1,2], Object.new].each do |value|
30
- it "#{value.inspect} -> CastError" do
31
- error = proc { Rasti::Form::Types::Form[form].cast(value) }.must_raise Rasti::Form::CastError
32
- error.message.must_equal "Invalid cast: #{as_string(value)} -> #{Rasti::Form::Types::Form[form]}"
33
- end
34
- end
35
-
36
- it '{x: "text"} -> ValidationError' do
37
- error = proc { Rasti::Form::Types::Form[form].cast x: 'test' }.must_raise Rasti::Form::ValidationError
38
- error.message.must_equal "Validation error: #<Rasti::Form[]> {\"x\":[\"Invalid cast: 'test' -> Rasti::Form::Types::Integer\"]}"
39
- end
40
-
41
- end
@@ -1,20 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Hash do
4
-
5
- it "{'a' => '123'} -> {a: 123}" do
6
- Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer].cast('a' => '123').must_equal a: 123
7
- end
8
-
9
- it "{'1' => :abc} -> {1 => 'abc'}" do
10
- Rasti::Form::Types::Hash[Rasti::Form::Types::Integer, Rasti::Form::Types::String].cast('1' => :abc).must_equal 1 => 'abc'
11
- end
12
-
13
- [nil, 1, 'text', :symbol, {a: true}, Object.new].each do |value|
14
- it "#{value.inspect} -> CastError" do
15
- error = proc { Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer].cast(value) }.must_raise Rasti::Form::CastError
16
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer]"
17
- end
18
- end
19
-
20
- end
@@ -1,18 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Integer do
4
-
5
- [100, '200', Time.now, 2.1, "12.5"].each do |value|
6
- it "#{value.inspect} -> #{value.to_i}" do
7
- Rasti::Form::Types::Integer.cast(value).must_equal value.to_i
8
- end
9
- end
10
-
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
- it "#{value.inspect} -> CastError" do
13
- error = proc { Rasti::Form::Types::Integer.cast(value) }.must_raise Rasti::Form::CastError
14
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Integer"
15
- end
16
- end
17
-
18
- end
@@ -1,18 +0,0 @@
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
@@ -1,18 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Regexp do
4
-
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)
8
- end
9
- end
10
-
11
- [nil, '[a-z', :symbol, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
12
- it "#{value.inspect} -> CastError" do
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"
15
- end
16
- end
17
-
18
- end
@@ -1,20 +0,0 @@
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
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
@@ -1,16 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::String do
4
-
5
- ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
- it "#{value.inspect} -> \"#{value.to_s}\"" do
7
- Rasti::Form::Types::String.cast(value).must_equal value.to_s
8
- end
9
- end
10
-
11
- it 'nil -> CastError' do
12
- error = proc { Rasti::Form::Types::String.cast(nil) }.must_raise Rasti::Form::CastError
13
- error.message.must_equal 'Invalid cast: nil -> Rasti::Form::Types::String'
14
- end
15
-
16
- end
@@ -1,16 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Symbol do
4
-
5
- ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
- it "#{value.inspect} -> \"#{value.to_s.to_sym}\"" do
7
- Rasti::Form::Types::Symbol.cast(value).must_equal value.to_s.to_sym
8
- end
9
- end
10
-
11
- it 'nil -> CastError' do
12
- error = proc { Rasti::Form::Types::Symbol.cast(nil) }.must_raise Rasti::Form::CastError
13
- error.message.must_equal 'Invalid cast: nil -> Rasti::Form::Types::Symbol'
14
- end
15
-
16
- end
@@ -1,25 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::Time do
4
-
5
- time = Time.new 2016, 8, 18
6
-
7
- it "#{time.inspect} -> #{time.inspect}" do
8
- Rasti::Form::Types::Time['%F %T %z'].cast(time).must_equal time
9
- end
10
-
11
- ['%d/%m/%y', '%Y-%m-%d'].each do |format|
12
- time_string = time.strftime(format)
13
- it "#{time_string.inspect} -> #{time.inspect}" do
14
- Rasti::Form::Types::Time[format].cast(time_string).must_equal time
15
- end
16
- end
17
-
18
- [time.strftime('%d/%m/%y'), 'text', nil, 1, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
19
- it "#{value.inspect} -> CastError" do
20
- error = proc { Rasti::Form::Types::Time['%F'].cast(value) }.must_raise Rasti::Form::CastError
21
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Time['%F']"
22
- end
23
- end
24
-
25
- end
@@ -1,18 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Rasti::Form::Types::UUID do
4
-
5
- ['f09b7716-81a9-11e6-a549-bb8f165bcf02', '12345678-1234-1234-1234-123456789123'].each do |value|
6
- it "#{value.inspect} -> #{value.to_s}" do
7
- Rasti::Form::Types::UUID.cast(value).must_equal value.to_s
8
- end
9
- end
10
-
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5, 'f09b7716-81a9-11e6-a549-bb16502', 'f09b7716-11e6-a549-bb8f16502', '-84a9-11e6-a549-bb8f16502', 'f09b7716-81a9-11e6-a549-bh16502'].each do |value|
12
- it "#{value.inspect} -> CastError" do
13
- error = proc { Rasti::Form::Types::UUID.cast(value) }.must_raise Rasti::Form::CastError
14
- error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::UUID"
15
- end
16
- end
17
-
18
- end