validate-params 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +103 -0
- data/README.md +46 -0
- data/Rakefile +16 -0
- data/lib/validate_params/params_validator.rb +130 -0
- data/lib/validate_params/version.rb +5 -0
- data/lib/validate_params.rb +8 -0
- data/sig/validate_params.rbs +4 -0
- data/validate_params.gemspec +33 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 06ea6e17956a26da41257b6ba81a599764e66bc6ea328f25b6d5537a3cd268ff
|
4
|
+
data.tar.gz: f2df924be3a6215c5894766db060b78b0b46cc459fe6ff903274cf5887824927
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5669fd9d7b7407403c577162809fd6ffba54f2304d3b4902fa27d8ab33376da08b066b44db9c1139c53c25965372a7e3953928ed1457d237bec3a7a6e473a176
|
7
|
+
data.tar.gz: f76f246d4221734df076e605afb3914c6a6e0f497a1c1f3b1f036e6c7c25c6401ecf26678f472050479ca510d06c3f7019ebbd96b461ba16fbff35b20ba23d88
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
validate_params (0.1.0)
|
5
|
+
activesupport (~> 7.0.4)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (7.0.4.3)
|
11
|
+
actionview (= 7.0.4.3)
|
12
|
+
activesupport (= 7.0.4.3)
|
13
|
+
rack (~> 2.0, >= 2.2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
+
actionview (7.0.4.3)
|
18
|
+
activesupport (= 7.0.4.3)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
+
activesupport (7.0.4.3)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (>= 1.6, < 2)
|
26
|
+
minitest (>= 5.1)
|
27
|
+
tzinfo (~> 2.0)
|
28
|
+
ast (2.4.2)
|
29
|
+
builder (3.2.4)
|
30
|
+
concurrent-ruby (1.2.2)
|
31
|
+
crass (1.0.6)
|
32
|
+
diff-lcs (1.5.0)
|
33
|
+
erubi (1.12.0)
|
34
|
+
i18n (1.13.0)
|
35
|
+
concurrent-ruby (~> 1.0)
|
36
|
+
json (2.6.3)
|
37
|
+
loofah (2.20.0)
|
38
|
+
crass (~> 1.0.2)
|
39
|
+
nokogiri (>= 1.5.9)
|
40
|
+
minitest (5.18.0)
|
41
|
+
nokogiri (1.14.3-arm64-darwin)
|
42
|
+
racc (~> 1.4)
|
43
|
+
parallel (1.23.0)
|
44
|
+
parser (3.2.2.1)
|
45
|
+
ast (~> 2.4.1)
|
46
|
+
racc (1.6.2)
|
47
|
+
rack (2.2.7)
|
48
|
+
rack-test (2.1.0)
|
49
|
+
rack (>= 1.3)
|
50
|
+
rails-dom-testing (2.0.3)
|
51
|
+
activesupport (>= 4.2.0)
|
52
|
+
nokogiri (>= 1.6)
|
53
|
+
rails-html-sanitizer (1.5.0)
|
54
|
+
loofah (~> 2.19, >= 2.19.1)
|
55
|
+
rainbow (3.1.1)
|
56
|
+
rake (13.0.6)
|
57
|
+
regexp_parser (2.8.0)
|
58
|
+
rexml (3.2.5)
|
59
|
+
rspec (3.12.0)
|
60
|
+
rspec-core (~> 3.12.0)
|
61
|
+
rspec-expectations (~> 3.12.0)
|
62
|
+
rspec-mocks (~> 3.12.0)
|
63
|
+
rspec-core (3.12.2)
|
64
|
+
rspec-support (~> 3.12.0)
|
65
|
+
rspec-expectations (3.12.3)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.12.0)
|
68
|
+
rspec-mocks (3.12.5)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.12.0)
|
71
|
+
rspec-support (3.12.0)
|
72
|
+
rubocop (1.50.2)
|
73
|
+
json (~> 2.3)
|
74
|
+
parallel (~> 1.10)
|
75
|
+
parser (>= 3.2.0.0)
|
76
|
+
rainbow (>= 2.2.2, < 4.0)
|
77
|
+
regexp_parser (>= 1.8, < 3.0)
|
78
|
+
rexml (>= 3.2.5, < 4.0)
|
79
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
80
|
+
ruby-progressbar (~> 1.7)
|
81
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
82
|
+
rubocop-ast (1.28.1)
|
83
|
+
parser (>= 3.2.1.0)
|
84
|
+
ruby-progressbar (1.13.0)
|
85
|
+
tzinfo (2.0.6)
|
86
|
+
concurrent-ruby (~> 1.0)
|
87
|
+
unicode-display_width (2.4.2)
|
88
|
+
|
89
|
+
PLATFORMS
|
90
|
+
ruby
|
91
|
+
arm64-darwin-21
|
92
|
+
arm64-darwin-22
|
93
|
+
|
94
|
+
DEPENDENCIES
|
95
|
+
actionpack (~> 7.0.4)
|
96
|
+
bundler (~> 2.0)
|
97
|
+
rake (~> 13.0)
|
98
|
+
rspec (~> 3.0)
|
99
|
+
rubocop (~> 1.21)
|
100
|
+
validate_params!
|
101
|
+
|
102
|
+
BUNDLED WITH
|
103
|
+
2.3.22
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# ValidateParams
|
2
|
+
|
3
|
+
[](https://github.com/peopleforce/validate_params/actions/workflows/rspec.yml)
|
4
|
+
|
5
|
+
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/validate_params`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
TODO: Delete this and the text above, and describe your gem
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add validate_params
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install validate_params
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class TestController < ActionController::Base
|
23
|
+
include ValidateParams::ParamsValidator
|
24
|
+
|
25
|
+
validate_params :index do |p|
|
26
|
+
p.param :id_param, Integer
|
27
|
+
p.param :date_param, Date
|
28
|
+
p.param :datetime_param, DateTime
|
29
|
+
end
|
30
|
+
|
31
|
+
def index
|
32
|
+
...
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validate_params.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module ValidateParams
|
4
|
+
module ParamsValidator
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
before_action :set_params_defaults
|
9
|
+
before_action :perform_validate_params
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
attr_accessor :params_validations, :method
|
14
|
+
|
15
|
+
def param(field, type, required = false, default: nil)
|
16
|
+
@params_validations ||= []
|
17
|
+
@params_validations << { field: field, type: type, required: required, default: default }
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_params(request_action, &block)
|
21
|
+
@request_action = request_action
|
22
|
+
|
23
|
+
yield(self) if block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def params_validations
|
28
|
+
self.class.instance_variable_get(:@params_validations) || []
|
29
|
+
end
|
30
|
+
|
31
|
+
def request_action
|
32
|
+
self.class.instance_variable_get(:@request_action) || nil
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def build_error_message(field, field_type, field_value)
|
38
|
+
I18n.t("api.public.invalid_parameter", field: field, field_type: field_type, field_value: field_value)
|
39
|
+
end
|
40
|
+
|
41
|
+
def error_param_name(field)
|
42
|
+
case field
|
43
|
+
when Array
|
44
|
+
"#{field[0]}[#{field[1]}]"
|
45
|
+
when Hash
|
46
|
+
field.map { |k, v| "#{k}[#{v}]" }.first
|
47
|
+
else
|
48
|
+
field
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def set_params_defaults
|
53
|
+
params_validations.each do |params_validation|
|
54
|
+
next if params_validation[:default].blank?
|
55
|
+
|
56
|
+
params[params_validation[:field]] ||= params_validation[:default]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def perform_validate_params
|
61
|
+
return unless request_action.present? && request_action == action_name.to_sym
|
62
|
+
|
63
|
+
errors = []
|
64
|
+
|
65
|
+
for params_validation in params_validations
|
66
|
+
parameter_value = if params_validation[:field].is_a? Hash
|
67
|
+
request.params.dig(params_validation[:field].keys.first,
|
68
|
+
params_validation[:field][params_validation[:field].keys.first])
|
69
|
+
else
|
70
|
+
request.params[params_validation[:field]]
|
71
|
+
end
|
72
|
+
|
73
|
+
next if parameter_value.blank? && !params_validation[:required]
|
74
|
+
|
75
|
+
case params_validation[:type].to_s
|
76
|
+
when "Date"
|
77
|
+
if invalid_date?(parameter_value)
|
78
|
+
errors << build_error_message(
|
79
|
+
error_param_name(params_validation[:field]),
|
80
|
+
params_validation[:type],
|
81
|
+
parameter_value
|
82
|
+
)
|
83
|
+
end
|
84
|
+
when "DateTime"
|
85
|
+
if invalid_datetime?(parameter_value)
|
86
|
+
errors << build_error_message(
|
87
|
+
error_param_name(params_validation[:field]),
|
88
|
+
params_validation[:type],
|
89
|
+
parameter_value
|
90
|
+
)
|
91
|
+
end
|
92
|
+
when "Integer"
|
93
|
+
if invalid_integer?(parameter_value)
|
94
|
+
errors << build_error_message(
|
95
|
+
error_param_name(params_validation[:field]),
|
96
|
+
params_validation[:type],
|
97
|
+
parameter_value
|
98
|
+
)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
return if errors.empty?
|
104
|
+
|
105
|
+
render json: { success: false, errors: errors }, status: :unprocessable_entity
|
106
|
+
end
|
107
|
+
|
108
|
+
def invalid_date?(value)
|
109
|
+
return true unless /\d{4}-\d{2}-\d{2}/.match?(value)
|
110
|
+
|
111
|
+
parsed_date = begin
|
112
|
+
Date.strptime(value, "%Y-%m-%d")
|
113
|
+
rescue StandardError
|
114
|
+
nil
|
115
|
+
end
|
116
|
+
parsed_date.blank? || parsed_date.year > 9999
|
117
|
+
end
|
118
|
+
|
119
|
+
def invalid_datetime?(value)
|
120
|
+
Time.at(Integer(value))
|
121
|
+
false
|
122
|
+
rescue ArgumentError, TypeError
|
123
|
+
true
|
124
|
+
end
|
125
|
+
|
126
|
+
def invalid_integer?(value)
|
127
|
+
value !~ /\A[-+]?[0-9]+\z/
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/validate_params/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.license = "MIT"
|
7
|
+
spec.name = "validate-params"
|
8
|
+
spec.version = ValidateParams::VERSION
|
9
|
+
spec.authors = ["dcherevatenko"]
|
10
|
+
spec.email = ["denis.cherevatenko@peopleforce.io "]
|
11
|
+
|
12
|
+
spec.summary = "Gem to validate params in controllers"
|
13
|
+
spec.description = "Provides a clean DSL to validate params for controller actions."
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(__dir__) do
|
19
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "actionpack", "~> 7.0.4"
|
31
|
+
|
32
|
+
spec.add_dependency "activesupport", "~> 7.0.4"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dcherevatenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: actionpack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 7.0.4
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 7.0.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 7.0.4
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 7.0.4
|
83
|
+
description: Provides a clean DSL to validate params for controller actions.
|
84
|
+
email:
|
85
|
+
- 'denis.cherevatenko@peopleforce.io '
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".rspec"
|
91
|
+
- ".rubocop.yml"
|
92
|
+
- CHANGELOG.md
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/validate_params.rb
|
98
|
+
- lib/validate_params/params_validator.rb
|
99
|
+
- lib/validate_params/version.rb
|
100
|
+
- sig/validate_params.rbs
|
101
|
+
- validate_params.gemspec
|
102
|
+
homepage:
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: 2.7.0
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubygems_version: 3.2.3
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Gem to validate params in controllers
|
125
|
+
test_files: []
|