typed-parameter 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/typed_parameter/base.rb +45 -22
- data/lib/typed_parameter/boolean.rb +2 -6
- data/lib/typed_parameter/constraints/date_constraint.rb +1 -1
- data/lib/typed_parameter/constraints/date_time_constraint.rb +1 -1
- data/lib/typed_parameter/enum_constraint.rb +13 -3
- data/lib/typed_parameter/errors.rb +2 -0
- data/lib/typed_parameter/swagger/component_generator.rb +4 -2
- data/lib/typed_parameter/swagger/type_generator.rb +3 -3
- data/lib/typed_parameter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff83851eec45e830cd251fa2a886722d6968373ce94f3a84eef45b47b3c67351
|
4
|
+
data.tar.gz: 47f08d63b04dad171ff357ca3ce31433d586bd5da1adc95560395783e687a80b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3ceb0d67668c8f4b02a3269c450c9800b9c63821b1904d14992b50d06475bc911b0bb6b5b3a5fe7d26db97aa03e164fe877ec533058c7426eea520027e90d6f
|
7
|
+
data.tar.gz: c505148ce9101eebf71c145f000abd8e45aa6501c4832cc39e03d43b0a734890d70cdf53a13226c8653ba4bee847d566672d4f139075d003b046a944b6fa54a4
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# typed_parameter
|
2
2
|
Create typed parameter class using strong paramter in rails.
|
3
3
|
You can cleanup parameter code for permit in your controller and convert it to the type you want.
|
4
4
|
## Installation
|
@@ -145,7 +145,7 @@ example_params = {
|
|
145
145
|
}
|
146
146
|
|
147
147
|
def parent_and_childs_params
|
148
|
-
|
148
|
+
ParentParameter.permit(example_params)
|
149
149
|
end
|
150
150
|
|
151
151
|
# Result
|
data/lib/typed_parameter/base.rb
CHANGED
@@ -2,25 +2,46 @@ module TypedParameter
|
|
2
2
|
class Base
|
3
3
|
class << self
|
4
4
|
def field(name, type, **kargs)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
if block_given?
|
6
|
+
raise Error::InvalidBlockParameter unless [type].flatten[0].in?([Hash, Object])
|
7
|
+
|
8
|
+
block_param_name = "#{self.to_s}_Partial_#{name.to_s.camelize}"
|
9
|
+
class_eval("class ::#{block_param_name} < ::TypedParameter::Base; end")
|
10
|
+
block_param = block_param_name.constantize
|
11
|
+
yield(block_param)
|
12
|
+
|
13
|
+
if type.is_a?(Array)
|
14
|
+
target = [block_param]
|
15
|
+
else
|
16
|
+
target = block_param
|
17
|
+
end
|
18
|
+
else
|
19
|
+
target = type
|
20
|
+
end
|
9
21
|
|
10
|
-
def permit(params)
|
11
|
-
raise ArgumentError unless params.class <= ActionController::Parameters
|
12
22
|
|
13
|
-
|
23
|
+
initialize_constraints(name, target, kargs)
|
24
|
+
initialize_swagger_properties(name, target, kargs)
|
14
25
|
end
|
15
26
|
|
16
|
-
def
|
17
|
-
|
27
|
+
def permit(params)
|
28
|
+
raise Error::ParameterError unless params.class <= ActionController::Parameters
|
29
|
+
|
30
|
+
use_constraints(params).permit!
|
18
31
|
end
|
19
32
|
|
20
33
|
def swagger_properties
|
21
34
|
__swagger_properties.freeze
|
22
35
|
end
|
23
36
|
|
37
|
+
def swagger_requirements
|
38
|
+
__swagger_requirements.freeze
|
39
|
+
end
|
40
|
+
|
41
|
+
def constraints
|
42
|
+
__constraints.freeze
|
43
|
+
end
|
44
|
+
|
24
45
|
alias key name
|
25
46
|
|
26
47
|
def ref
|
@@ -31,43 +52,45 @@ module TypedParameter
|
|
31
52
|
|
32
53
|
def initialize_swagger_properties(name, type, kargs)
|
33
54
|
swagger_type = TypedParameter::Swagger::TypeGenerator.generate(type)
|
34
|
-
swagger_options = swagger_type.merge(kargs)
|
55
|
+
swagger_options = swagger_type.merge(kargs.slice(:enum, :description))
|
35
56
|
__swagger_properties[name] = swagger_options
|
36
|
-
|
37
|
-
|
38
|
-
def initialize_permitted_fields(name, type)
|
39
|
-
__fields << PermitFieldGenerator.generate(name, type)
|
57
|
+
__swagger_requirements << name if kargs[:required]
|
40
58
|
end
|
41
59
|
|
42
60
|
def initialize_constraints(name, type, kargs)
|
43
61
|
options = kargs.slice(:enum, :required)
|
44
|
-
|
62
|
+
options[:enum] = options[:enum].map(&:to_s) if options[:enum]
|
45
63
|
__constraints << [name, type, options]
|
46
64
|
end
|
47
65
|
|
48
66
|
def use_constraints(params)
|
49
|
-
|
67
|
+
typed_params = ActionController::Parameters.new
|
50
68
|
|
51
69
|
__constraints.each do |name, type, options|
|
52
70
|
value = params[name]
|
53
|
-
raise RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] &&
|
71
|
+
raise Error::RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && empty_value?(value)
|
72
|
+
|
54
73
|
next unless value.present?
|
55
74
|
|
56
|
-
|
57
|
-
|
75
|
+
typed_params[name] = TypeConstraint.value(type, value)
|
76
|
+
typed_params[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
|
58
77
|
end
|
59
78
|
|
60
|
-
|
79
|
+
typed_params
|
61
80
|
end
|
62
81
|
|
63
|
-
def
|
64
|
-
|
82
|
+
def empty_value?(value)
|
83
|
+
value.nil? || value == '' || (value.is_a?(Array) && value.empty?)
|
65
84
|
end
|
66
85
|
|
67
86
|
def __constraints
|
68
87
|
@__constraints ||= []
|
69
88
|
end
|
70
89
|
|
90
|
+
def __swagger_requirements
|
91
|
+
@__swagger_requirements ||= []
|
92
|
+
end
|
93
|
+
|
71
94
|
def __swagger_properties
|
72
95
|
@__swagger_properties ||= {}
|
73
96
|
end
|
@@ -4,16 +4,12 @@ class Boolean; end
|
|
4
4
|
|
5
5
|
class TrueClass
|
6
6
|
def is_a?(type)
|
7
|
-
|
8
|
-
|
9
|
-
super(type)
|
7
|
+
type == Boolean || super(type)
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
13
11
|
class FalseClass
|
14
12
|
def is_a?(type)
|
15
|
-
|
16
|
-
|
17
|
-
super(type)
|
13
|
+
type == Boolean || super(type)
|
18
14
|
end
|
19
15
|
end
|
@@ -1,10 +1,20 @@
|
|
1
1
|
module TypedParameter
|
2
2
|
module EnumConstraint
|
3
3
|
def self.value(value, enums)
|
4
|
-
|
5
|
-
|
4
|
+
return array_value(value, enums) if value.is_a? Array
|
5
|
+
return value if value.to_s.in? enums
|
6
6
|
|
7
|
-
value
|
7
|
+
raise EnumFieldError, "#{value} must be in #{enums.join(', ')}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.array_value(values, enums)
|
11
|
+
values.each do |value|
|
12
|
+
next if value.to_s.in? enums
|
13
|
+
|
14
|
+
raise EnumFieldError, "#{value} must be in #{enums.join(', ')}"
|
15
|
+
end
|
16
|
+
|
17
|
+
values
|
8
18
|
end
|
9
19
|
end
|
10
20
|
end
|
@@ -6,11 +6,13 @@ module TypedParameter
|
|
6
6
|
key = parameter_class.key
|
7
7
|
properties = Swagger::PropertyGenerator.generate(parameter_class.swagger_properties)
|
8
8
|
|
9
|
-
|
9
|
+
swagger = { type: :object, properties: properties }
|
10
|
+
swagger[:required] = parameter_class.swagger_requirements if parameter_class.swagger_requirements.present?
|
11
|
+
{ "#{key}": swagger }
|
10
12
|
end
|
11
13
|
|
12
14
|
def generate_all!
|
13
|
-
parameter_classes = TypedParameter::Base.descendants.reject { |c| c.
|
15
|
+
parameter_classes = TypedParameter::Base.descendants.reject { |c| c.swagger_properties.empty? }
|
14
16
|
|
15
17
|
parameter_classes.reduce({}) { |prev, curr| prev.merge(generate(curr)) }
|
16
18
|
end
|
@@ -5,9 +5,9 @@ module TypedParameter
|
|
5
5
|
def generate(type)
|
6
6
|
return array_generate(type) if type.is_a? Array
|
7
7
|
return hash_generate(type) if type.is_a? Hash
|
8
|
+
return { ref: type } if type < TypedParameter::Base
|
8
9
|
|
9
|
-
|
10
|
-
swagger_type&.value || { ref: type }
|
10
|
+
Swagger::Types[type.name.to_sym]&.value
|
11
11
|
end
|
12
12
|
|
13
13
|
def hash_generate(hash)
|
@@ -17,7 +17,7 @@ module TypedParameter
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def array_generate(type)
|
20
|
-
{ type: :array, items: generate(type[0]) }
|
20
|
+
{ type: :array, items: generate(type[0]) }
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed-parameter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Minwoo Jo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|