typed-parameter 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27016542e3178c6385e4fdfe27f3520f6f37f5afc49572e04f701a0b261eba92
4
- data.tar.gz: 991f39290e89d92ede9ae33df068712f3e90fc545e4282ccd7cf5e0bf82fdc4b
3
+ metadata.gz: 85aaf512215b5dd4abc568b69190dde93d0894befac1a1f1b6c9097cd468a9f1
4
+ data.tar.gz: 51d58c512d2f057b6ce7071876a8b01ad6360fd9d9d70aaa323ce56e96ecdaca
5
5
  SHA512:
6
- metadata.gz: 557509df857bfc89d385cabce59a11296fc0ce54751d9060f3da267fc72bd77b5ff4a98e65c488127d83fe0cbe01b2aa18b723325da36819657f5f0615ea4867
7
- data.tar.gz: 676c8fc68a53c6923407bf104cf54a1825d27db20b40377f4f0c2d0800fe5910843c6e775c6cea5b862c30d92b50faf0656e4631dbe1164981a3f957d2c9bfa0
6
+ metadata.gz: 26b48a740e5f53583003ef50f9c2a3c016cb19a8146b74e90660865fa224f8de9e01bbe150e83c379091f069021281f394d36e7ec204e40f4b586c15126c2034
7
+ data.tar.gz: 144a0dab8ec0676cdb4fb3d0447769df7d8c0cc007d38830f2499fd16cba1cf78a194b64644b5e223a3e3a82188ccc30f0e7b5cdb5fa3f2d60b0f2d47bc1a426
data/README.md CHANGED
@@ -163,10 +163,85 @@ end
163
163
  ~~~
164
164
  Example 4. CustomType
165
165
  ------
166
- ...
166
+ If You want to use your custom type like Email, just create two file.
167
+ - your type file. like "Email"
168
+ - yout type constant. like "EmailConstrant"
169
+
170
+ ~~~ruby
171
+ class Email
172
+ def initialize(value)
173
+ @email = value
174
+ end
175
+ end
176
+
177
+ class EmailConstrant
178
+ class << self
179
+ EMAIL_REGREX = //
180
+
181
+ # using convert value to your type
182
+ def value(value)
183
+ raise ArgumentError unless EMAIL_REGREX.match? value
184
+
185
+ Email.new(value)
186
+ end
187
+ end
188
+ end
189
+
190
+ class EmailSwaggerType
191
+ class << self
192
+ def value
193
+ { type: :string }
194
+ end
195
+ end
196
+ end
197
+
198
+ # Register your type to typed-parameter when application initialize.
199
+ TypedParameter::ParameterTypes.register Email
200
+ TypedParameter::Constants.register :Email, EmailConstrant
201
+ TypedParameter::Swagger::Types.register :Email, EmailSwaggerType
202
+ ~~~
167
203
 
168
204
  Example 5. Swaggerize
169
205
  ------
206
+ With Rswag(https://github.com/rswag/rswag), you can swaggerize your parameter types.
207
+
208
+ ~~~ruby
209
+ class SwaggerParameter < TypedParameter::Base
210
+ field :string, String, enum: ["one", "two"], description: "String Field"
211
+ field :integer, Integer, description: "Integer Field"
212
+ field :ref_field, SwaggerRefParameter
213
+ end
214
+
215
+ class SwaggerRefParameter < TypedParameter::Base
216
+ field :string, String, description: "Ref String Field"
217
+ end
218
+
219
+ SwaggerParameter.swagger_properties
220
+ # =>
221
+ # {
222
+ # string: { type: :string, enum: ["one", "two" ], description: "String Field" },
223
+ # integer: { type: :integer, description: "Integer Field" },
224
+ # ref_field: { "$ref": "#/components/schemas/SwaggerRefParamter" }
225
+ # }
226
+ #
227
+
228
+ # Add Components in your swagger
229
+ # in swagger_helper
230
+ components = TypedParameter::Swagger::ComponentGenerator.generate_all!
231
+
232
+ config.swagger_docs = {
233
+ 'v1/swagger.yaml' => {
234
+ openapi: '3.0.1',
235
+ # ...,
236
+ components: {
237
+ schemas: {
238
+ **components
239
+ }
240
+ }
241
+ #...
242
+ }
243
+ }
244
+ ~~~
170
245
  ...
171
246
 
172
247
 
@@ -13,6 +13,5 @@ require 'typed_parameter/swagger/type_generator'
13
13
  require 'typed_parameter/swagger/property_generator'
14
14
  require 'typed_parameter/swagger/component_generator'
15
15
 
16
- module TypedParameter
17
- class Error < StandardError; end
18
- end
16
+ require 'typed_parameter/errors'
17
+ module TypedParameter; end
@@ -2,7 +2,7 @@ module TypedParameter
2
2
  class Base
3
3
  class << self
4
4
  def field(name, type, **kargs)
5
- initialize_permitted_fields(name, type)
5
+ initialize_permitted_fields(name, type) # Need to remove! we don't need strong_parameter's filter
6
6
  initialize_swagger_properties(name, type, kargs)
7
7
  initialize_constraints(name, type, kargs)
8
8
  end
@@ -10,7 +10,7 @@ module TypedParameter
10
10
  def permit(params)
11
11
  raise ArgumentError unless params.class <= ActionController::Parameters
12
12
 
13
- use_constraints params.permit(fields)
13
+ use_constraints(params).permit! # will change to use_constraints(params).permit!
14
14
  end
15
15
 
16
16
  def fields
@@ -46,16 +46,18 @@ module TypedParameter
46
46
  end
47
47
 
48
48
  def use_constraints(params)
49
+ paramters = ActionController::Parameters.new
50
+
49
51
  __constraints.each do |name, type, options|
50
52
  value = params[name]
51
- raise ArgumentError, "(#{self.name}) #{name} is required" if options[:required] && !value.present?
53
+ raise RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && !value.present?
52
54
  next unless value.present?
53
55
 
54
- params[name] = TypeConstraint.value(type, value)
55
- params[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
56
+ paramters[name] = TypeConstraint.value(type, value)
57
+ paramters[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
56
58
  end
57
59
 
58
- params
60
+ paramters
59
61
  end
60
62
 
61
63
  def __fields
@@ -3,7 +3,7 @@ require 'typed_parameter/constraints/integer_constraint'
3
3
  require 'typed_parameter/constraints/float_constraint'
4
4
  require 'typed_parameter/constraints/string_constraint'
5
5
  require 'typed_parameter/constraints/date_constraint'
6
- require 'typed_parameter/constraints/datetime_constraint'
6
+ require 'typed_parameter/constraints/date_time_constraint'
7
7
  require 'typed_parameter/constraints/boolean_constraint'
8
8
 
9
9
  module TypedParameter
@@ -14,7 +14,7 @@ module TypedParameter
14
14
  @constraints[key] = constraint
15
15
  end
16
16
 
17
- def get(name)
17
+ def [](name)
18
18
  @constraints[name].freeze
19
19
  end
20
20
  end
@@ -2,7 +2,7 @@ module TypedParameter
2
2
  module EnumConstraint
3
3
  def self.value(value, enums)
4
4
  enums = enums.map(&:to_s) if value.is_a? String
5
- raise ArgumentError, "#{value} must be in #{enums.join(', ')}" unless value.in? enums
5
+ raise EnumFieldError, "#{value} must be in #{enums.join(', ')}" unless value.in? enums
6
6
 
7
7
  value
8
8
  end
@@ -0,0 +1,7 @@
1
+ module TypedParameter
2
+ class Error < StandardError; end
3
+
4
+ class RequiredFieldError < Error; end
5
+
6
+ class EnumFieldError < Error; end
7
+ end
@@ -5,7 +5,7 @@ module TypedParameter
5
5
  type_class = [type].flatten.first
6
6
 
7
7
  props = if type.is_a? Array
8
- type_class.in? TypedParameter::ParameterTypes.types ? [] : generate_properties(type_class)
8
+ type_class.in?(TypedParameter::ParameterTypes.types) ? [] : generate_properties(type_class)
9
9
  else
10
10
  generate_properties(type_class)
11
11
  end
@@ -8,6 +8,12 @@ module TypedParameter
8
8
 
9
9
  { "#{key}": { type: :object, properties: properties } }
10
10
  end
11
+
12
+ def generate_all!
13
+ parameter_classes = TypedParameter::Base.descendants.reject { |c| c.fields.empty? }
14
+
15
+ parameter_classes.reduce({}) { |prev, curr| prev.merge(generate(curr)) }
16
+ end
11
17
  end
12
18
  end
13
19
  end
@@ -6,7 +6,7 @@ module TypedParameter
6
6
  return array_generate(type) if type.is_a? Array
7
7
  return hash_generate(type) if type.is_a? Hash
8
8
 
9
- swagger_type = "TypedParameter::Swagger::Types::#{type.name}Type".safe_constantize
9
+ swagger_type = Swagger::Types[type.name.to_sym]
10
10
  swagger_type&.value || { ref: type }
11
11
  end
12
12
 
@@ -5,3 +5,27 @@ require 'typed_parameter/swagger/types/string_type'
5
5
  require 'typed_parameter/swagger/types/date_type'
6
6
  require 'typed_parameter/swagger/types/date_time_type'
7
7
  require 'typed_parameter/swagger/types/boolean_type'
8
+
9
+ module TypedParameter
10
+ module Swagger
11
+ module Types
12
+ class << self
13
+ def register(key, type)
14
+ @swagger_types ||= {}
15
+ @swagger_types[key] = type
16
+ end
17
+
18
+ def [](key)
19
+ @swagger_types[key].freeze
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ TypedParameter::Swagger::Types.register :Integer, TypedParameter::Swagger::Types::IntegerType
27
+ TypedParameter::Swagger::Types.register :String, TypedParameter::Swagger::Types::StringType
28
+ TypedParameter::Swagger::Types.register :Float, TypedParameter::Swagger::Types::FloatType
29
+ TypedParameter::Swagger::Types.register :Date, TypedParameter::Swagger::Types::DateType
30
+ TypedParameter::Swagger::Types.register :DateTime, TypedParameter::Swagger::Types::DateTimeType
31
+ TypedParameter::Swagger::Types.register :Boolean, TypedParameter::Swagger::Types::BooleanType
@@ -7,7 +7,7 @@ module TypedParameter
7
7
  if type_class < TypedParameter::Base
8
8
  type.is_a?(Array) ? parameter_array(type_class, value) : type.permit(value)
9
9
  else
10
- constraint = TypedParameter::Constraints.get type_class.name.to_sym
10
+ constraint = TypedParameter::Constraints[type_class.name.to_sym]
11
11
  constraint ||= TypedParameter::Constraints::StringConstraint # Default
12
12
 
13
13
  type.is_a?(Array) ? constraint_array(constraint, value) : constraint.value(value)
@@ -1,3 +1,3 @@
1
1
  module TypedParameter
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  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.3.0
4
+ version: 0.4.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: 2021-03-07 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,6 +66,7 @@ files:
66
66
  - lib/typed_parameter/constraints/integer_constraint.rb
67
67
  - lib/typed_parameter/constraints/string_constraint.rb
68
68
  - lib/typed_parameter/enum_constraint.rb
69
+ - lib/typed_parameter/errors.rb
69
70
  - lib/typed_parameter/parameter_types.rb
70
71
  - lib/typed_parameter/permit_field_generator.rb
71
72
  - lib/typed_parameter/swagger/component_generator.rb