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.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +8 -11
- data/README.md +0 -1
- data/lib/rasti/form.rb +34 -129
- data/lib/rasti/form/errors.rb +7 -47
- data/lib/rasti/form/validable.rb +9 -3
- data/rasti-form.gemspec +3 -14
- data/spec/coverage_helper.rb +1 -3
- data/spec/minitest_helper.rb +1 -7
- data/spec/validations_spec.rb +301 -0
- metadata +11 -56
- data/lib/rasti/form/castable.rb +0 -25
- data/lib/rasti/form/formatable.rb +0 -19
- data/lib/rasti/form/types/array.rb +0 -54
- data/lib/rasti/form/types/boolean.rb +0 -42
- data/lib/rasti/form/types/enum.rb +0 -48
- data/lib/rasti/form/types/float.rb +0 -33
- data/lib/rasti/form/types/form.rb +0 -40
- data/lib/rasti/form/types/hash.rb +0 -39
- data/lib/rasti/form/types/integer.rb +0 -21
- data/lib/rasti/form/types/io.rb +0 -23
- data/lib/rasti/form/types/regexp.rb +0 -23
- data/lib/rasti/form/types/string.rb +0 -44
- data/lib/rasti/form/types/symbol.rb +0 -23
- data/lib/rasti/form/types/time.rb +0 -40
- data/lib/rasti/form/types/uuid.rb +0 -19
- data/lib/rasti/form/version.rb +0 -5
- data/spec/form_spec.rb +0 -350
- data/spec/types/array_spec.rb +0 -54
- data/spec/types/boolean_spec.rb +0 -24
- data/spec/types/enum_spec.rb +0 -28
- data/spec/types/float_spec.rb +0 -18
- data/spec/types/form_spec.rb +0 -41
- data/spec/types/hash_spec.rb +0 -20
- data/spec/types/integer_spec.rb +0 -18
- data/spec/types/io_spec.rb +0 -18
- data/spec/types/regexp_spec.rb +0 -18
- data/spec/types/string_formatted_spec.rb +0 -20
- data/spec/types/string_spec.rb +0 -16
- data/spec/types/symbol_spec.rb +0 -16
- data/spec/types/time_spec.rb +0 -25
- data/spec/types/uuid_spec.rb +0 -18
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: 5.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: 2021-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_require
|
@@ -25,33 +25,33 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rasti-model
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
-
type: :
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '12.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '12.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -160,42 +160,12 @@ files:
|
|
160
160
|
- Rakefile
|
161
161
|
- lib/rasti-form.rb
|
162
162
|
- lib/rasti/form.rb
|
163
|
-
- lib/rasti/form/castable.rb
|
164
163
|
- lib/rasti/form/errors.rb
|
165
|
-
- lib/rasti/form/formatable.rb
|
166
|
-
- lib/rasti/form/types/array.rb
|
167
|
-
- lib/rasti/form/types/boolean.rb
|
168
|
-
- lib/rasti/form/types/enum.rb
|
169
|
-
- lib/rasti/form/types/float.rb
|
170
|
-
- lib/rasti/form/types/form.rb
|
171
|
-
- lib/rasti/form/types/hash.rb
|
172
|
-
- lib/rasti/form/types/integer.rb
|
173
|
-
- lib/rasti/form/types/io.rb
|
174
|
-
- lib/rasti/form/types/regexp.rb
|
175
|
-
- lib/rasti/form/types/string.rb
|
176
|
-
- lib/rasti/form/types/symbol.rb
|
177
|
-
- lib/rasti/form/types/time.rb
|
178
|
-
- lib/rasti/form/types/uuid.rb
|
179
164
|
- lib/rasti/form/validable.rb
|
180
|
-
- lib/rasti/form/version.rb
|
181
165
|
- rasti-form.gemspec
|
182
166
|
- spec/coverage_helper.rb
|
183
|
-
- spec/form_spec.rb
|
184
167
|
- spec/minitest_helper.rb
|
185
|
-
- spec/
|
186
|
-
- spec/types/boolean_spec.rb
|
187
|
-
- spec/types/enum_spec.rb
|
188
|
-
- spec/types/float_spec.rb
|
189
|
-
- spec/types/form_spec.rb
|
190
|
-
- spec/types/hash_spec.rb
|
191
|
-
- spec/types/integer_spec.rb
|
192
|
-
- spec/types/io_spec.rb
|
193
|
-
- spec/types/regexp_spec.rb
|
194
|
-
- spec/types/string_formatted_spec.rb
|
195
|
-
- spec/types/string_spec.rb
|
196
|
-
- spec/types/symbol_spec.rb
|
197
|
-
- spec/types/time_spec.rb
|
198
|
-
- spec/types/uuid_spec.rb
|
168
|
+
- spec/validations_spec.rb
|
199
169
|
homepage: https://github.com/gabynaiman/rasti-form
|
200
170
|
licenses:
|
201
171
|
- MIT
|
@@ -215,26 +185,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
215
185
|
- !ruby/object:Gem::Version
|
216
186
|
version: '0'
|
217
187
|
requirements: []
|
218
|
-
|
219
|
-
rubygems_version: 2.6.8
|
188
|
+
rubygems_version: 3.0.6
|
220
189
|
signing_key:
|
221
190
|
specification_version: 4
|
222
191
|
summary: Forms validations and type casting
|
223
192
|
test_files:
|
224
193
|
- spec/coverage_helper.rb
|
225
|
-
- spec/form_spec.rb
|
226
194
|
- spec/minitest_helper.rb
|
227
|
-
- spec/
|
228
|
-
- spec/types/boolean_spec.rb
|
229
|
-
- spec/types/enum_spec.rb
|
230
|
-
- spec/types/float_spec.rb
|
231
|
-
- spec/types/form_spec.rb
|
232
|
-
- spec/types/hash_spec.rb
|
233
|
-
- spec/types/integer_spec.rb
|
234
|
-
- spec/types/io_spec.rb
|
235
|
-
- spec/types/regexp_spec.rb
|
236
|
-
- spec/types/string_formatted_spec.rb
|
237
|
-
- spec/types/string_spec.rb
|
238
|
-
- spec/types/symbol_spec.rb
|
239
|
-
- spec/types/time_spec.rb
|
240
|
-
- spec/types/uuid_spec.rb
|
195
|
+
- spec/validations_spec.rb
|
data/lib/rasti/form/castable.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Castable
|
4
|
-
|
5
|
-
def cast(value)
|
6
|
-
if valid? value
|
7
|
-
transform! value
|
8
|
-
else
|
9
|
-
raise CastError.new self, value
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def transform!(value)
|
16
|
-
transform value
|
17
|
-
rescue MultiCastError, ValidationError => ex
|
18
|
-
raise ex
|
19
|
-
rescue
|
20
|
-
raise CastError.new self, value
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -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
|
@@ -1,54 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Array
|
5
|
-
|
6
|
-
include Castable
|
7
|
-
|
8
|
-
attr_reader :type
|
9
|
-
|
10
|
-
def self.[](type)
|
11
|
-
new type
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_s
|
15
|
-
"#{self.class}[#{type}]"
|
16
|
-
end
|
17
|
-
alias_method :inspect, :to_s
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def initialize(type)
|
22
|
-
@type = type
|
23
|
-
end
|
24
|
-
|
25
|
-
def valid?(value)
|
26
|
-
value.is_a? ::Array
|
27
|
-
end
|
28
|
-
|
29
|
-
def transform(value)
|
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
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Boolean
|
5
|
-
class << self
|
6
|
-
|
7
|
-
include Castable
|
8
|
-
|
9
|
-
TRUE_FORMAT = /^t(rue)?$/i
|
10
|
-
FALSE_FORMAT = /^f(alse)?$/i
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def valid?(value)
|
15
|
-
boolean?(value) || valid_string?(value)
|
16
|
-
end
|
17
|
-
|
18
|
-
def transform(value)
|
19
|
-
boolean?(value) ? value : true_string?(value)
|
20
|
-
end
|
21
|
-
|
22
|
-
def boolean?(value)
|
23
|
-
value == true || value == false
|
24
|
-
end
|
25
|
-
|
26
|
-
def valid_string?(value)
|
27
|
-
value.is_a?(::String) && (true_string?(value) || false_string?(value))
|
28
|
-
end
|
29
|
-
|
30
|
-
def true_string?(value)
|
31
|
-
!!value.match(TRUE_FORMAT)
|
32
|
-
end
|
33
|
-
|
34
|
-
def false_string?(value)
|
35
|
-
!!value.match(FALSE_FORMAT)
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Enum
|
5
|
-
|
6
|
-
include Castable
|
7
|
-
|
8
|
-
attr_reader :values
|
9
|
-
|
10
|
-
def self.[](*values)
|
11
|
-
new values
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_s
|
15
|
-
"#{self.class}[#{values.map(&:inspect).join(', ')}]"
|
16
|
-
end
|
17
|
-
alias_method :inspect, :to_s
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def initialize(values)
|
22
|
-
@values = values.map(&:to_s)
|
23
|
-
define_getters
|
24
|
-
end
|
25
|
-
|
26
|
-
def valid?(value)
|
27
|
-
values.include? String.cast(value)
|
28
|
-
rescue
|
29
|
-
false
|
30
|
-
end
|
31
|
-
|
32
|
-
def transform(value)
|
33
|
-
String.cast value
|
34
|
-
end
|
35
|
-
|
36
|
-
def define_getters
|
37
|
-
values.each do |value|
|
38
|
-
getter_name = value.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
|
39
|
-
define_singleton_method getter_name do
|
40
|
-
value
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Float
|
5
|
-
class << self
|
6
|
-
|
7
|
-
include Castable
|
8
|
-
|
9
|
-
FORMAT = /^(\d+\.)?\d+$/
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def valid?(value)
|
14
|
-
!value.nil? && (valid_string?(value) || transformable?(value))
|
15
|
-
end
|
16
|
-
|
17
|
-
def transform(value)
|
18
|
-
value.to_f
|
19
|
-
end
|
20
|
-
|
21
|
-
def valid_string?(value)
|
22
|
-
value.is_a?(::String) && value.match(FORMAT)
|
23
|
-
end
|
24
|
-
|
25
|
-
def transformable?(value)
|
26
|
-
!value.is_a?(::String) && value.respond_to?(:to_f)
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Form
|
5
|
-
|
6
|
-
include Castable
|
7
|
-
|
8
|
-
attr_reader :form_class
|
9
|
-
|
10
|
-
def self.[](*args)
|
11
|
-
new *args
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_s
|
15
|
-
"#{self.class}[#{form_class}]"
|
16
|
-
end
|
17
|
-
alias_method :inspect, :to_s
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def initialize(form)
|
22
|
-
@form_class = case
|
23
|
-
when form.is_a?(::Hash) then Rasti::Form[form]
|
24
|
-
when form.is_a?(Class) && form.ancestors.include?(Rasti::Form) then form
|
25
|
-
else raise ArgumentError, "Invalid form specification: #{form.inspect}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def valid?(value)
|
30
|
-
value.is_a? ::Hash
|
31
|
-
end
|
32
|
-
|
33
|
-
def transform(value)
|
34
|
-
form_class.new value
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Hash
|
5
|
-
|
6
|
-
include Castable
|
7
|
-
|
8
|
-
attr_reader :key_type, :value_type
|
9
|
-
|
10
|
-
def self.[](key_type, value_type)
|
11
|
-
new key_type, value_type
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_s
|
15
|
-
"#{self.class}[#{key_type}, #{value_type}]"
|
16
|
-
end
|
17
|
-
alias_method :inspect, :to_s
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def initialize(key_type, value_type)
|
22
|
-
@key_type = key_type
|
23
|
-
@value_type = value_type
|
24
|
-
end
|
25
|
-
|
26
|
-
def valid?(value)
|
27
|
-
value.is_a? ::Hash
|
28
|
-
end
|
29
|
-
|
30
|
-
def transform(value)
|
31
|
-
value.each_with_object({}) do |(k,v),h|
|
32
|
-
h[key_type.cast k] = value_type.cast v
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Integer < Float
|
5
|
-
class << self
|
6
|
-
|
7
|
-
private
|
8
|
-
|
9
|
-
def transform(value)
|
10
|
-
value.to_i
|
11
|
-
end
|
12
|
-
|
13
|
-
def transformable?(value)
|
14
|
-
!value.is_a?(::String) && value.respond_to?(:to_i)
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|