zero-params_processor 0.4.0 → 0.4.2

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: 8ec687b8215056d4e4197b1c0a2e11425e00460d84c6e204b93f756f4ece1b4e
4
- data.tar.gz: a494c7786f8390b2f254c6b9bffffe6d303447aa859d390c1a25beb18052dc20
3
+ metadata.gz: 6af3afb1c845aebdf43201ba56f36de36be89e14b0708ae574a108070f650088
4
+ data.tar.gz: 84420bb307c78be64a6951134fed5c5984ada904253268cedcff32f8be0818c0
5
5
  SHA512:
6
- metadata.gz: a0e1025443eee3ba5a337dd7107c7621c716679cd790f7944ac25f1d6f3ad8ce1979debd97d4cae212bbbc61aac6e42f6e7b2e4cc86844364e66565d93b126d6
7
- data.tar.gz: c50e9d5f36bccb010b1b23eea0557523df285a40fbcb9bd24accc2057fe67efa3308e924d480cba011979c4e57f36fccd8aef842076927222402728892f6e4b8
6
+ metadata.gz: 1e3e9190fab8af960cf6fd430875d3555df90c47b8c7779268f49a120f6806f6bd1d9643d4b43048f1dfa9b7b4681e1ee72b2be3b8d2f150ba497a024891468b
7
+ data.tar.gz: 602f1c9906ca11618cb2a5dfd544a33b127add743d6ffd05a8ed197de8da539869e7f446bd396c32796860c67bb149052c74c31d99a3e06b8b15de7dd3a450ab
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
  .rspec_status
12
12
 
13
13
  .idea/*
14
+ Gemfile.lock
@@ -3,9 +3,6 @@ language: ruby
3
3
  rvm:
4
4
  - 2.5.1
5
5
  before_install: gem install bundler
6
- env:
7
- global:
8
- - CC_TEST_REPORTER_ID=5f2477af6b38bd4db35abe49930cbae98e867cb3163b2da1a465176b2f0a361c
9
6
  before_script:
10
7
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
11
8
  - chmod +x ./cc-test-reporter
@@ -1,8 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open_api/router'
4
+
1
5
  require 'params_processor/version'
2
6
  require 'params_processor/validate'
3
7
  require 'params_processor/type_convert'
4
8
 
5
9
  module ParamsProcessor
10
+ cattr_accessor :docs
11
+
6
12
  private
7
13
 
8
14
  def process_params_by(*actions)
@@ -12,6 +18,7 @@ module ParamsProcessor
12
18
  _validate_param!(param_doc) if actions.index(:validate!)
13
19
  _convert_param(param_doc) if actions.index(:convert)
14
20
  _set_instance_var(param_doc) if actions.index(:set_instance_var)
21
+ _group_params(param_doc) if param_doc.group
15
22
  param_doc
16
23
  end
17
24
  _set_permitted(pdocs) if actions.index(:set_permitted)
@@ -24,7 +31,7 @@ module ParamsProcessor
24
31
 
25
32
  def _validate_param!(param_doc)
26
33
  input = param_doc.in == 'header' ? request.headers[param_doc.name.to_s] : params[param_doc.name.to_sym]
27
- error_class = "#{controller_name.camelize}Error".constantize rescue nil
34
+ error_class = "Error::#{controller_name.camelize}".constantize rescue nil
28
35
  Validate.(input, based_on: param_doc, raise: error_class)
29
36
  end
30
37
 
@@ -39,14 +46,20 @@ module ParamsProcessor
39
46
  end
40
47
 
41
48
  def _set_instance_var(param_doc)
42
- key = param_doc.real_name
43
- return if (value = params[key]).nil?
49
+ return if (value = params[(key = param_doc.real_name)]).nil?
44
50
 
45
51
  instance_variable_set("@#{key}", value)
46
52
  _permit_hash_and_array(value) if param_doc.permit?
47
53
  _auto_find_by_id(key, value) if @route_path.match?(/\{#{key}\}/) # e.g. "/examples/{id}"
48
54
  end
49
55
 
56
+ def _group_params(param_doc)
57
+ return if (value = params[(key = param_doc.real_name.to_sym)]).nil?
58
+
59
+ instance_variable_get("@#{param_doc.group}")&.merge!(key => value) ||
60
+ instance_variable_set("@#{param_doc.group}", key => value)
61
+ end
62
+
50
63
  # If params[:data] == [{..}, {..}], each `{..}` in the array
51
64
  # is an instance of ActionController::Parameters.
52
65
  # So, if :data is allowed to permit, it's values should also permit.
@@ -57,7 +70,7 @@ module ParamsProcessor
57
70
  def _auto_find_by_id(key, value)
58
71
  whos_id = (key.to_sym == :id ? controller_name : key.to_s.sub('_id', '')).singularize
59
72
  model = whos_id.camelize.constantize rescue return
60
- model_instance = model.find_by(id: value) || self.class.error_cls.not_found! # TODO HACK
73
+ model_instance = model.find_by(id: value) || Error::Api.not_found! # TODO HACK
61
74
  instance_variable_set("@#{whos_id}", model_instance)
62
75
  end
63
76
 
@@ -65,26 +78,25 @@ module ParamsProcessor
65
78
  exist_not_permit = params_docs.map(&:not_permit?).any?(&:present?)
66
79
  keys = params_docs.map { |p| p.doced_permit? ? p.real_name : nil }.compact
67
80
  keys = exist_not_permit ? params_docs.map(&:real_name) - keys : keys
68
- @permitted = params.permit(*keys)
69
- # @permitted = params.slice(*keys).to_unsafe_h
81
+ # @permitted = params.permit(*keys) TODO
82
+ @permitted = params.slice(*keys).to_unsafe_h.symbolize_keys
70
83
  end
71
84
 
72
85
  def permitted; @permitted end
73
86
 
74
87
  # TODO: performance
75
88
  def params_doc
76
- current_api = OpenApi.routes_index[self.class.controller_path]
77
- return [ ] unless current_api
89
+ return [ ] unless (current_api = OpenApi.routes_index[self.class.controller_path])
78
90
 
79
- DocConverter.docs ||= DocConverter.new(OpenApi.docs)
80
- @route_path = OpenApi::Generator.find_path_httpverb_by(self.class.controller_path, action_name).first
81
- path_doc = DocConverter.docs[current_api][:paths][@route_path]
91
+ self.docs ||= DocConverter.new(OpenApi.docs)
92
+ @route_path = OpenApi::Router.find_path_httpverb_by(self.class.controller_path, action_name).first
93
+ path_doc = docs[current_api][:paths][@route_path]
82
94
  # nil check is for skipping this before_action when the action is not doced.
83
95
  path_doc&.[](request.method.downcase)&.[](:parameters) || [ ]
84
96
  end
85
97
 
86
98
 
87
99
  class ValidationFailed < StandardError
88
- def info; { code: 400, msg: "#{Config.prefix}".concat(message), http_status: :bad_request }; end
100
+ def info; { code: 400, msg: "#{Config.prefix}" + message, http_status: :bad_request }; end
89
101
  end
90
102
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/all'
2
4
 
3
5
  module ParamsProcessor
@@ -1,9 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/hash_with_indifferent_access'
2
4
 
3
5
  module ParamsProcessor
4
6
  class DocConverter < HashWithIndifferentAccess
5
- cattr_accessor :docs
6
-
7
7
  def initialize(inhert_hash = { })
8
8
  super(inhert_hash)
9
9
  convert
@@ -34,7 +34,8 @@ module ParamsProcessor
34
34
 
35
35
  # 将 form-data 提到 parameters,方便统一访问接口
36
36
  # 在 param ref 处理之前上提,使后续可以一并将 properties 中的 schma 进行处理
37
- form = action_doc[:requestBody]&.[](:content)&.[]('multipart/form-data')
37
+ form = action_doc[:requestBody]&.[](:content)
38
+ form = form['multipart/form-data'] || form['application/json'] if form # TODO
38
39
  if form.present?
39
40
  required = form[:schema][:required] || [ ]
40
41
  permit = form[:schema][:permit] ? true : false
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/hash_with_indifferent_access'
2
4
 
3
5
  module ParamsProcessor
@@ -49,12 +51,13 @@ module ParamsProcessor
49
51
  one_of: %i[ schema oneOf ],
50
52
  any_of: %i[ schema anyOf ],
51
53
  not_be: %i[ schema not ],
52
- is: %i[ schema is ],
54
+ is: %i[ schema is_a ],
53
55
  dft: %i[ schema default ],
54
56
  as: %i[ schema as ],
55
57
  items: %i[ schema items ],
56
58
  props: %i[ schema properties ],
57
59
  blankable: %i[ schema blankable ],
60
+ group: %i[ schema group ],
58
61
  permit?: %i[ schema permit ],
59
62
  not_permit?: %i[ schema not_permit ],
60
63
  }.each do |method, path|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParamsProcessor
2
4
  class TypeConvert
3
5
  class << self
@@ -34,16 +36,19 @@ module ParamsProcessor
34
36
  when 'date' then parse_time(Date)
35
37
  when 'date-time' then parse_time(DateTime)
36
38
  when 'base64' then @input # Base64.strict_decode64(@input)
39
+ when 'binary' then @input
37
40
  else @input.to_s
38
41
  end
39
42
  end
40
43
 
41
44
  def array
42
- @input
45
+ return @input unless @input.is_a?(String)
46
+ @input = MultiJson.load(@input)
43
47
  end
44
48
 
45
49
  def object
46
- @input
50
+ return @input unless @input.is_a?(String)
51
+ @input = MultiJson.load(@input)
47
52
  end
48
53
 
49
54
  # combined TODO
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'multi_json'
2
4
  require 'params_processor/config'
3
5
  require 'params_processor/doc_converter'
@@ -21,7 +23,7 @@ module ParamsProcessor
21
23
  @doc = param_doc
22
24
  check (if_is_passed do
23
25
  check if_is_present
24
- break if @input.nil?# || (@doc.blankable != false && @input.blank? && @input != false)
26
+ next if @input.nil?# || (@doc.blankable != false && @input.blank? && @input != false)
25
27
  check_combined_types if @doc.combined?
26
28
  check type if @doc.type
27
29
  check size if @doc.size
@@ -48,12 +50,13 @@ module ParamsProcessor
48
50
  case @doc.type
49
51
  when 'integer' then @str_input.match?(/^-?\d*$/)
50
52
  when 'boolean' then @str_input.in? %w[ true 1 false 0 ]
51
- when 'array' then @input.is_a? Array
52
- when 'object' then @input.is_a?(ActionController::Parameters) || @input.is_a?(Hash)
53
+ when 'array' then @input.is_a?(Array) || (@input.is_a?(String) && @input['['])
54
+ when 'object' then @input.is_a?(ActionController::Parameters) || @input.is_a?(Hash) ||
55
+ (@input.is_a?(String) && @input['{'] && @input['}'])
53
56
  when 'number' then _number_type
54
57
  when 'string' then _string_type
55
58
  else true # TODO
56
- end or [:wrong_type, @doc.format.to_s]
59
+ end or [:wrong_type, (@doc.format || @doc.type).to_s]
57
60
  end
58
61
 
59
62
  def _number_type
@@ -154,6 +157,7 @@ module ParamsProcessor
154
157
 
155
158
  _doc, _input = @doc, @input
156
159
  items_doc = ParamDoc.new name: @doc.name, schema: @doc.items
160
+ @input = MultiJson.load(@input) if @input.is_a?(String)
157
161
  @input.each do |input|
158
162
  Validate.(input, based_on: items_doc, raise: @error_class)
159
163
  end
@@ -164,11 +168,12 @@ module ParamsProcessor
164
168
  return if @doc.props.blank?
165
169
 
166
170
  _doc = @doc
171
+ hash = @input.is_a?(String) ? MultiJson.load(@input) : @input
167
172
  required = (@doc[:schema][:required] || [ ]).map(&:to_s)
168
173
  @doc.props.each do |name, schema|
169
174
  prop_doc = ParamDoc.new name: name, required: required.include?(name), schema: schema
170
175
  _input = @input
171
- Validate.(@input[name] || @input[name.to_sym], based_on: prop_doc, raise: @error_class)
176
+ Validate.(hash[name] || hash[name.to_sym], based_on: prop_doc, raise: @error_class)
172
177
  @input = _input
173
178
  end
174
179
  @doc = _doc
@@ -176,13 +181,14 @@ module ParamsProcessor
176
181
 
177
182
  def check msg
178
183
  return unless msg.is_a? Array
184
+ @error_class.params_error! if @error_class.respond_to? :params_error
179
185
  @error_class.send("#{@doc.name}_#{msg.first}!") if @error_class.respond_to? "#{@doc.name}_#{msg.first}!"
180
186
  @error_class.send("#{msg.first}!") if @error_class.respond_to? msg.first
181
187
  raise ValidationFailed, Config.production_msg if Config.production_msg.present?
182
188
 
183
189
  test_msg = Config.send(msg.first) if Config.test
184
190
  msg = "#{Config.send(msg.first)}#{' ' + msg.last if msg.last.present?}"
185
- msg = " `#{@doc.name.to_sym}` " << msg
191
+ msg = " `#{@doc.name.to_sym}` " + msg
186
192
  raise ValidationFailed, test_msg || msg
187
193
  end
188
194
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParamsProcessor
2
- VERSION = '0.4.0'
4
+ VERSION = '0.4.2'
3
5
  end
@@ -22,12 +22,13 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.16.a'
26
- spec.add_development_dependency 'rake', '~> 10.0'
27
- spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
28
  spec.add_development_dependency 'simplecov'
29
29
 
30
- spec.add_runtime_dependency 'zero-rails_openapi', '>= 1.5.2'
30
+ spec.add_dependency 'zero-rails_openapi', '>= 1.5.2'
31
+
31
32
  spec.add_runtime_dependency 'rails', '>= 3'
32
33
  spec.add_runtime_dependency 'activesupport', '>= 3'
33
34
  spec.add_runtime_dependency 'multi_json'
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zero-params_processor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-18 00:00:00.000000000 Z
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.16.a
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.16.a
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
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: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '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: '3.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -135,7 +135,6 @@ files:
135
135
  - ".rubocop.yml"
136
136
  - ".travis.yml"
137
137
  - Gemfile
138
- - Gemfile.lock
139
138
  - LICENSE.txt
140
139
  - README.md
141
140
  - Rakefile
@@ -169,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
168
  version: '0'
170
169
  requirements: []
171
170
  rubyforge_project:
172
- rubygems_version: 2.7.6
171
+ rubygems_version: 2.7.3
173
172
  signing_key:
174
173
  specification_version: 4
175
174
  summary: Process parameters base on OpenApi3 JSON documentation
@@ -1,157 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- zero-params_processor (0.4.0)
5
- activesupport (>= 3)
6
- multi_json
7
- rails (>= 3)
8
- zero-rails_openapi (>= 1.5.2)
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- actioncable (5.2.1)
14
- actionpack (= 5.2.1)
15
- nio4r (~> 2.0)
16
- websocket-driver (>= 0.6.1)
17
- actionmailer (5.2.1)
18
- actionpack (= 5.2.1)
19
- actionview (= 5.2.1)
20
- activejob (= 5.2.1)
21
- mail (~> 2.5, >= 2.5.4)
22
- rails-dom-testing (~> 2.0)
23
- actionpack (5.2.1)
24
- actionview (= 5.2.1)
25
- activesupport (= 5.2.1)
26
- rack (~> 2.0)
27
- rack-test (>= 0.6.3)
28
- rails-dom-testing (~> 2.0)
29
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
- actionview (5.2.1)
31
- activesupport (= 5.2.1)
32
- builder (~> 3.1)
33
- erubi (~> 1.4)
34
- rails-dom-testing (~> 2.0)
35
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
36
- activejob (5.2.1)
37
- activesupport (= 5.2.1)
38
- globalid (>= 0.3.6)
39
- activemodel (5.2.1)
40
- activesupport (= 5.2.1)
41
- activerecord (5.2.1)
42
- activemodel (= 5.2.1)
43
- activesupport (= 5.2.1)
44
- arel (>= 9.0)
45
- activestorage (5.2.1)
46
- actionpack (= 5.2.1)
47
- activerecord (= 5.2.1)
48
- marcel (~> 0.3.1)
49
- activesupport (5.2.1)
50
- concurrent-ruby (~> 1.0, >= 1.0.2)
51
- i18n (>= 0.7, < 2)
52
- minitest (~> 5.1)
53
- tzinfo (~> 1.1)
54
- arel (9.0.0)
55
- builder (3.2.3)
56
- concurrent-ruby (1.0.5)
57
- crass (1.0.4)
58
- diff-lcs (1.3)
59
- docile (1.1.5)
60
- erubi (1.7.1)
61
- globalid (0.4.1)
62
- activesupport (>= 4.2.0)
63
- i18n (1.1.0)
64
- concurrent-ruby (~> 1.0)
65
- json (2.1.0)
66
- loofah (2.2.2)
67
- crass (~> 1.0.2)
68
- nokogiri (>= 1.5.9)
69
- mail (2.7.0)
70
- mini_mime (>= 0.1.1)
71
- marcel (0.3.2)
72
- mimemagic (~> 0.3.2)
73
- method_source (0.9.0)
74
- mimemagic (0.3.2)
75
- mini_mime (1.0.1)
76
- mini_portile2 (2.3.0)
77
- minitest (5.11.3)
78
- multi_json (1.13.1)
79
- nio4r (2.3.1)
80
- nokogiri (1.8.4)
81
- mini_portile2 (~> 2.3.0)
82
- rack (2.0.5)
83
- rack-test (1.1.0)
84
- rack (>= 1.0, < 3)
85
- rails (5.2.1)
86
- actioncable (= 5.2.1)
87
- actionmailer (= 5.2.1)
88
- actionpack (= 5.2.1)
89
- actionview (= 5.2.1)
90
- activejob (= 5.2.1)
91
- activemodel (= 5.2.1)
92
- activerecord (= 5.2.1)
93
- activestorage (= 5.2.1)
94
- activesupport (= 5.2.1)
95
- bundler (>= 1.3.0)
96
- railties (= 5.2.1)
97
- sprockets-rails (>= 2.0.0)
98
- rails-dom-testing (2.0.3)
99
- activesupport (>= 4.2.0)
100
- nokogiri (>= 1.6)
101
- rails-html-sanitizer (1.0.4)
102
- loofah (~> 2.2, >= 2.2.2)
103
- railties (5.2.1)
104
- actionpack (= 5.2.1)
105
- activesupport (= 5.2.1)
106
- method_source
107
- rake (>= 0.8.7)
108
- thor (>= 0.19.0, < 2.0)
109
- rake (10.5.0)
110
- rspec (3.6.0)
111
- rspec-core (~> 3.6.0)
112
- rspec-expectations (~> 3.6.0)
113
- rspec-mocks (~> 3.6.0)
114
- rspec-core (3.6.0)
115
- rspec-support (~> 3.6.0)
116
- rspec-expectations (3.6.0)
117
- diff-lcs (>= 1.2.0, < 2.0)
118
- rspec-support (~> 3.6.0)
119
- rspec-mocks (3.6.0)
120
- diff-lcs (>= 1.2.0, < 2.0)
121
- rspec-support (~> 3.6.0)
122
- rspec-support (3.6.0)
123
- simplecov (0.15.1)
124
- docile (~> 1.1.0)
125
- json (>= 1.8, < 3)
126
- simplecov-html (~> 0.10.0)
127
- simplecov-html (0.10.2)
128
- sprockets (3.7.2)
129
- concurrent-ruby (~> 1.0)
130
- rack (> 1, < 3)
131
- sprockets-rails (3.2.1)
132
- actionpack (>= 4.0)
133
- activesupport (>= 4.0)
134
- sprockets (>= 3.0.0)
135
- thor (0.20.0)
136
- thread_safe (0.3.6)
137
- tzinfo (1.2.5)
138
- thread_safe (~> 0.1)
139
- websocket-driver (0.7.0)
140
- websocket-extensions (>= 0.1.0)
141
- websocket-extensions (0.1.3)
142
- zero-rails_openapi (1.5.2)
143
- activesupport (>= 3)
144
- rails (>= 3)
145
-
146
- PLATFORMS
147
- ruby
148
-
149
- DEPENDENCIES
150
- bundler (~> 1.16.a)
151
- rake (~> 10.0)
152
- rspec (~> 3.0)
153
- simplecov
154
- zero-params_processor!
155
-
156
- BUNDLED WITH
157
- 1.16.3