rasti-form 3.1.2 → 4.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.rb +35 -134
- data/lib/rasti/form/errors.rb +6 -52
- data/lib/rasti/form/validable.rb +5 -6
- data/rasti-form.gemspec +2 -12
- data/spec/coverage_helper.rb +1 -3
- data/spec/minitest_helper.rb +1 -7
- data/spec/validations_spec.rb +301 -0
- metadata +18 -47
- data/lib/rasti/form/castable.rb +0 -25
- 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 -40
- 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 -7
- data/lib/rasti/form/version.rb +0 -5
- data/spec/form_spec.rb +0 -411
- 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: 4.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-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_require
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rasti-model
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -146,41 +160,12 @@ files:
|
|
146
160
|
- Rakefile
|
147
161
|
- lib/rasti-form.rb
|
148
162
|
- lib/rasti/form.rb
|
149
|
-
- lib/rasti/form/castable.rb
|
150
163
|
- lib/rasti/form/errors.rb
|
151
|
-
- lib/rasti/form/types/array.rb
|
152
|
-
- lib/rasti/form/types/boolean.rb
|
153
|
-
- lib/rasti/form/types/enum.rb
|
154
|
-
- lib/rasti/form/types/float.rb
|
155
|
-
- lib/rasti/form/types/form.rb
|
156
|
-
- lib/rasti/form/types/hash.rb
|
157
|
-
- lib/rasti/form/types/integer.rb
|
158
|
-
- lib/rasti/form/types/io.rb
|
159
|
-
- lib/rasti/form/types/regexp.rb
|
160
|
-
- lib/rasti/form/types/string.rb
|
161
|
-
- lib/rasti/form/types/symbol.rb
|
162
|
-
- lib/rasti/form/types/time.rb
|
163
|
-
- lib/rasti/form/types/uuid.rb
|
164
164
|
- lib/rasti/form/validable.rb
|
165
|
-
- lib/rasti/form/version.rb
|
166
165
|
- rasti-form.gemspec
|
167
166
|
- spec/coverage_helper.rb
|
168
|
-
- spec/form_spec.rb
|
169
167
|
- spec/minitest_helper.rb
|
170
|
-
- spec/
|
171
|
-
- spec/types/boolean_spec.rb
|
172
|
-
- spec/types/enum_spec.rb
|
173
|
-
- spec/types/float_spec.rb
|
174
|
-
- spec/types/form_spec.rb
|
175
|
-
- spec/types/hash_spec.rb
|
176
|
-
- spec/types/integer_spec.rb
|
177
|
-
- spec/types/io_spec.rb
|
178
|
-
- spec/types/regexp_spec.rb
|
179
|
-
- spec/types/string_formatted_spec.rb
|
180
|
-
- spec/types/string_spec.rb
|
181
|
-
- spec/types/symbol_spec.rb
|
182
|
-
- spec/types/time_spec.rb
|
183
|
-
- spec/types/uuid_spec.rb
|
168
|
+
- spec/validations_spec.rb
|
184
169
|
homepage: https://github.com/gabynaiman/rasti-form
|
185
170
|
licenses:
|
186
171
|
- MIT
|
@@ -206,19 +191,5 @@ specification_version: 4
|
|
206
191
|
summary: Forms validations and type casting
|
207
192
|
test_files:
|
208
193
|
- spec/coverage_helper.rb
|
209
|
-
- spec/form_spec.rb
|
210
194
|
- spec/minitest_helper.rb
|
211
|
-
- spec/
|
212
|
-
- spec/types/boolean_spec.rb
|
213
|
-
- spec/types/enum_spec.rb
|
214
|
-
- spec/types/float_spec.rb
|
215
|
-
- spec/types/form_spec.rb
|
216
|
-
- spec/types/hash_spec.rb
|
217
|
-
- spec/types/integer_spec.rb
|
218
|
-
- spec/types/io_spec.rb
|
219
|
-
- spec/types/regexp_spec.rb
|
220
|
-
- spec/types/string_formatted_spec.rb
|
221
|
-
- spec/types/string_spec.rb
|
222
|
-
- spec/types/symbol_spec.rb
|
223
|
-
- spec/types/time_spec.rb
|
224
|
-
- 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,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
|
data/lib/rasti/form/types/io.rb
DELETED
@@ -1,23 +0,0 @@
|
|
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
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
class Form
|
3
|
-
module Types
|
4
|
-
class Regexp
|
5
|
-
class << self
|
6
|
-
|
7
|
-
include Castable
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def valid?(value)
|
12
|
-
value.is_a?(::Regexp) || value.is_a?(::String)
|
13
|
-
end
|
14
|
-
|
15
|
-
def transform(value)
|
16
|
-
value.is_a?(::Regexp) ? value.source : ::Regexp.compile(value).source
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|