sinatra-dry_param 0.0.1 → 0.0.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: 8b962e8ac60d8efb56b2038012e96ca363814ff635397f01efc4873f392bf563
4
- data.tar.gz: d4bd1dcbd7dac7f2e65b84cf072c52d7ba03d52844770088c7e25dc2383af8f8
3
+ metadata.gz: b6463c4978cf3eaed12b4d8ee9eedc2100b6fefb5f99b0c20e95cc9185f4eb3a
4
+ data.tar.gz: 2580d83d97408439d5a041729394adda20084fc55f7ddb6828b4d4016dddfee1
5
5
  SHA512:
6
- metadata.gz: a75b6b7610cc7e20bcb0af13184d0a366760ef1776a98c6c1f533145ed328663b8a9ed2b038c68302b1b4b70e1e4788f37d4ca8d8ef8b8479dceacf74559154b
7
- data.tar.gz: 7bb89004a2bd622696b2dde679c5d6657e6f818491b802ecc842e7ec8c45c78727343e3d99d77b1c3c55b01223cd7203889e6a624d03ec5667b28ccb93eae145
6
+ metadata.gz: 10a5a61f6f752898d49db7370f728962348b62ae2f527d3071c22abf4e8ff9aef0156debc21c9e9f9af6c4384d5d200c413ffa3f67f0312b65fbe2a1cb83979a
7
+ data.tar.gz: 2109f819e752c331500f2689ae170e460dcb6bcfbadf138f87d5bd15840fafe7718a6ebe6efb4d568f70181cb03c33681458c8993188c540d8f4779ba9a10397
data/.gitignore CHANGED
@@ -9,3 +9,7 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ .byebug_history
14
+
15
+ Gemfile.lock
@@ -1,7 +1,10 @@
1
1
  ---
2
- sudo: false
3
2
  language: ruby
4
3
  cache: bundler
5
4
  rvm:
6
- - 2.6.2
7
- before_install: gem install bundler -v 1.17.2
5
+ - 2.6.3
6
+ - 2.5.5
7
+ - 2.4.6
8
+ - jruby-9.2.7.0
9
+ before_install:
10
+ - gem install bundler -v 1.17.3
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Sinatra::DryParam
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sinatra/dry_param`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This sinatra extension provides easy interfaces for validating params. It serves both schema validation and strong-params feature.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Validation is processed by [dry-schema](https://github.com/dry-rb/dry-schema), provides robust type and schema validation.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,139 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ Register and use the extension in your sinatra app, for classic style sinatra app:
26
+
27
+
28
+ ```ruby
29
+ require 'sinatra'
30
+ require 'sinatra/dry_param'
31
+
32
+ params do
33
+ required(:name).filled(:string)
34
+ end
35
+
36
+ get '/hello' do
37
+ data = validate_params
38
+ h "Hello #{data[:name]}!"
39
+ end
40
+ ```
41
+
42
+ Or for modular style sinatra app:
43
+
44
+ ```ruby
45
+ require 'sinatra/base'
46
+ require 'sinatra/dry_param'
47
+
48
+ class App < Sinatra::Base
49
+ register Sinatra::DryParam
50
+
51
+ params do
52
+ required(:name).filled(:string)
53
+ end
54
+
55
+ get '/hello' do
56
+ data = validate_params
57
+ h "Hello #{data[:name]}!"
58
+ end
59
+ end
60
+ ```
61
+
62
+ ### Define params schemas
63
+
64
+ One or more schemas can be defined using method `::params`, distinguised by names:
65
+
66
+ ```ruby
67
+ class App < Sinatra::Base
68
+ register Sinatra::DryParam
69
+
70
+ params do
71
+ # dry-schema definitions
72
+ end
73
+
74
+ params :paging do
75
+ # different name as :paging
76
+ end
77
+
78
+ params do
79
+ # Override the previous schema in whole class scope.
80
+ # Should only override parent class' schemas.
81
+ end
82
+
83
+ get '/' do
84
+ validate_params # Use the default schema to validate params
85
+ validate_params :dry, params # Same with above line, the default schema name is :dry
86
+ validate_params :paging, params[:paging] # Use the :paging schema to validate one param
87
+ end
88
+ ```
89
+
90
+ ### Schema definition
91
+
92
+ The block passed in `::params` is the block passed to `Dry::Schema.Params` of dry-schema.
93
+
94
+ For syntax of schema definition, please refer to dry-schema [documentation](https://dry-rb.org/gems/dry-schema/).
95
+
96
+ ### Reusing param schemas
97
+
98
+ Defined params can be retrieved as app settings, using the schema name with `"_params"` suffix.
99
+
100
+ ```ruby
101
+ settings.dry_params
102
+ settings.paging_params
103
+ ```
104
+
105
+ And we can reuse schemas as how dry-schema is [reused](https://dry-rb.org/gems/dry-schema/reusing-schemas/)
106
+
107
+ ```ruby
108
+ params :address do
109
+ required(:city).filled(:string)
110
+ required(:street).filled(:string)
111
+ required(:zipcode).filled(:string)
112
+ end
113
+
114
+ params do
115
+ required(:email).filled(:string)
116
+ required(:name).filled(:string)
117
+ required(:address).hash(settings.address_params)
118
+ end
119
+
120
+ post '/users' do
121
+ input = validate_params
122
+ User.create_with_address(input)
123
+ end
124
+ ```
125
+
126
+ ### Render response
127
+
128
+ By default, if `validate_params` failed, the action is halted with status 400 and render errors.
129
+
130
+ ```ruby
131
+ params do
132
+ required(:page).filled(:integer, gt?: 0)
133
+ required(:per_page).filled(:integer, gt?: 0)
134
+ end
135
+
136
+ get '/' do
137
+ validate_params
138
+ end
139
+
140
+ #client
141
+ get '/', page: 0
142
+ #> Response body: { "page": ['must be greater than 0'], "per_page": ['is missing'] }
143
+ ```
144
+
145
+ You can customize the behavior with this enable setting `raise_dry_param_exceptions` and catch the exception:
146
+
147
+ ```ruby
148
+ class App < Sinatra::Base
149
+ register Sinatra::DryParam
150
+
151
+ enable :raise_dry_param_exceptions
152
+
153
+ error Sinatra::DryParam::InvalidParamsError do
154
+ "Your params is not valid: " + env['sinatra.error'].results.to_h.to_s
155
+ end
156
+ end
157
+ ```
26
158
 
27
159
  ## Development
28
160
 
@@ -32,7 +164,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
164
 
33
165
  ## Contributing
34
166
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sinatra-dry_param. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
167
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tiev/sinatra-dry_param. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
168
 
37
169
  ## License
38
170
 
@@ -40,4 +172,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
172
 
41
173
  ## Code of Conduct
42
174
 
43
- Everyone interacting in the Sinatra::DryParam project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sinatra-dry_param/blob/master/CODE_OF_CONDUCT.md).
175
+ Everyone interacting in the Sinatra::DryParam project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tiev/sinatra-dry_param/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,6 +1,13 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/gem_tasks'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
7
+
8
+ require 'rspec/core'
9
+ require 'rspec/core/rake_task'
3
10
 
4
11
  RSpec::Core::RakeTask.new(:spec)
5
12
 
6
- task :default => :spec
13
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra/dry_param'
@@ -5,19 +5,38 @@ require 'sinatra/dry_param/version'
5
5
 
6
6
  module Sinatra
7
7
  module DryParam
8
- class Error < StandardError; end
8
+ class InvalidParamsError < StandardError
9
+ attr_reader :results
10
+
11
+ def initialize(msg, results)
12
+ @results = results
13
+ super(msg)
14
+ end
15
+ end
9
16
 
10
17
  module Helpers
11
- def validate_params
12
- schema = Dry::Schema.Params
13
- schema.call(params)
18
+ def validate_params(name = :dry, prs = params)
19
+ result = settings.send("#{name}_params").call(prs)
20
+ if result.success?
21
+ result.to_h
22
+ else
23
+ raise InvalidParamsError.new(result.errors.to_h.to_s, result.errors) if settings.raise_dry_param_exceptions?
24
+
25
+ halt 400, result.errors.to_h.to_json
26
+ end
14
27
  end
15
28
  end
16
29
 
30
+ def params(name = :dry, schema: nil, &block)
31
+ schema ||= Dry::Schema.Params(&block)
32
+ set "#{name}_params", schema
33
+ end
34
+
17
35
  def self.registered(app)
18
36
  require 'dry/schema'
19
-
20
37
  app.helpers DryParam::Helpers
38
+
39
+ app.disable :raise_dry_param_exceptions
21
40
  end
22
41
  end
23
42
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module DryParam
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
  end
7
7
  end
@@ -23,12 +23,13 @@ Gem::Specification.new do |spec|
23
23
  spec.bindir = 'exe'
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ['lib']
26
+ spec.required_ruby_version = '>= 2.4'
26
27
 
28
+ spec.add_runtime_dependency 'dry-schema', '>= 0.3.0'
27
29
  spec.add_runtime_dependency 'sinatra', '>= 2.0'
28
- spec.add_runtime_dependency 'dry-schema', '>= 0.5.1'
29
30
 
30
- spec.add_development_dependency 'bundler', '~> 1.17'
31
- spec.add_development_dependency 'rake', '~> 10.0'
32
- spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'bundler'
33
32
  spec.add_development_dependency 'rack-test'
33
+ spec.add_development_dependency 'rake'
34
+ spec.add_development_dependency 'rspec'
34
35
  end
metadata CHANGED
@@ -1,87 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-dry_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viet (Drake) Tran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-24 00:00:00.000000000 Z
11
+ date: 2019-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sinatra
14
+ name: dry-schema
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: 0.3.0
20
20
  type: :runtime
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: '2.0'
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: dry-schema
28
+ name: sinatra
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.1
33
+ version: '2.0'
34
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: 0.5.1
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.17'
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: '1.17'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: rack-test
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '3.0'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '3.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rack-test
84
+ name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -106,12 +106,12 @@ files:
106
106
  - ".travis.yml"
107
107
  - CODE_OF_CONDUCT.md
108
108
  - Gemfile
109
- - Gemfile.lock
110
109
  - LICENSE.txt
111
110
  - README.md
112
111
  - Rakefile
113
112
  - bin/console
114
113
  - bin/setup
114
+ - lib/sinatra-dry_param.rb
115
115
  - lib/sinatra/dry_param.rb
116
116
  - lib/sinatra/dry_param/version.rb
117
117
  - sinatra-dry_param.gemspec
@@ -127,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
127
  requirements:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
- version: '0'
130
+ version: '2.4'
131
131
  required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
133
  - - ">="
@@ -1,81 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sinatra-dry_param (0.0.1)
5
- dry-schema (>= 0.5.1)
6
- sinatra (>= 2.0)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- concurrent-ruby (1.1.5)
12
- diff-lcs (1.3)
13
- dry-configurable (0.8.2)
14
- concurrent-ruby (~> 1.0)
15
- dry-core (~> 0.4, >= 0.4.7)
16
- dry-container (0.7.0)
17
- concurrent-ruby (~> 1.0)
18
- dry-configurable (~> 0.1, >= 0.1.3)
19
- dry-core (0.4.7)
20
- concurrent-ruby (~> 1.0)
21
- dry-equalizer (0.2.2)
22
- dry-inflector (0.1.2)
23
- dry-initializer (2.5.0)
24
- dry-logic (0.6.1)
25
- concurrent-ruby (~> 1.0)
26
- dry-core (~> 0.2)
27
- dry-equalizer (~> 0.2)
28
- dry-schema (0.5.1)
29
- concurrent-ruby (~> 1.0)
30
- dry-configurable (~> 0.8, >= 0.8.0)
31
- dry-core (~> 0.2, >= 0.2.1)
32
- dry-equalizer (~> 0.2)
33
- dry-initializer (~> 2.4)
34
- dry-logic (~> 0.6, >= 0.6.0)
35
- dry-types (~> 0.15, >= 0.15.0)
36
- dry-types (0.15.0)
37
- concurrent-ruby (~> 1.0)
38
- dry-container (~> 0.3)
39
- dry-core (~> 0.4, >= 0.4.4)
40
- dry-equalizer (~> 0.2, >= 0.2.2)
41
- dry-inflector (~> 0.1, >= 0.1.2)
42
- dry-logic (~> 0.5, >= 0.5)
43
- mustermann (1.0.3)
44
- rack (2.0.7)
45
- rack-protection (2.0.5)
46
- rack
47
- rack-test (1.1.0)
48
- rack (>= 1.0, < 3)
49
- rake (10.5.0)
50
- rspec (3.8.0)
51
- rspec-core (~> 3.8.0)
52
- rspec-expectations (~> 3.8.0)
53
- rspec-mocks (~> 3.8.0)
54
- rspec-core (3.8.0)
55
- rspec-support (~> 3.8.0)
56
- rspec-expectations (3.8.2)
57
- diff-lcs (>= 1.2.0, < 2.0)
58
- rspec-support (~> 3.8.0)
59
- rspec-mocks (3.8.0)
60
- diff-lcs (>= 1.2.0, < 2.0)
61
- rspec-support (~> 3.8.0)
62
- rspec-support (3.8.0)
63
- sinatra (2.0.5)
64
- mustermann (~> 1.0)
65
- rack (~> 2.0)
66
- rack-protection (= 2.0.5)
67
- tilt (~> 2.0)
68
- tilt (2.0.9)
69
-
70
- PLATFORMS
71
- ruby
72
-
73
- DEPENDENCIES
74
- bundler (~> 1.17)
75
- rack-test
76
- rake (~> 10.0)
77
- rspec (~> 3.0)
78
- sinatra-dry_param!
79
-
80
- BUNDLED WITH
81
- 1.17.2