validate-params 0.2.2 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b32fa7c57a158e0ef8e52c18256b07043bcad0db71c74a34aaa133e2251bce83
4
- data.tar.gz: c82e56a0e5f5d5727d3d80f9724a5701dfcdf9bc72cff8b7b33fbcd0d1e1a213
3
+ metadata.gz: e4a258ed30473649f0653321c8ba39c756cf6e88748d56334d3f5e3655b848f6
4
+ data.tar.gz: 372fef2f81ba16caae4b8bb0761e9ccc1db69855607fda37fa3ff324d6efe9f1
5
5
  SHA512:
6
- metadata.gz: ab2532167e3f8c333a40d199b66b80a5a1d46e66cea87714ed6cd04e7001855ab47beb9b4583be740a7ec9802a48272c4a5835f6e4cf41ec03cec50cfba14ef1
7
- data.tar.gz: e5b3faaf1b1d60313c2cca7ae067ee94b3fbbe06ef49b3b53dd3777677714a0b7cb07a22da18f4f8e639929a16485a4b4fa895a77139b2dc2c0d4bf6a03524c0
6
+ metadata.gz: 0cc0c6d36439278009bd32ca26e832d5daaa728f8f8b68147e323db08cdfed7050ddc614a55b8a9f7d7c8134e958c0df3e3303e28113aaa585db988cf463a18c
7
+ data.tar.gz: 966e2f1354c35e26d69396552f178769c734fe58d0dcfdd7f88e74e4e81b49ccd97c97c628cc3bd499a160f9e1bd6207f2fb6225d6ea3f254d6076a017025090
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ TargetRubyVersion: 2.7
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2023-05-12
4
+
5
+ - Add required attribute support
6
+
3
7
  ## [0.1.0] - 2023-05-10
4
8
 
5
9
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.2.0)
4
+ validate-params (0.3.0)
5
5
  activesupport (>= 6.1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  [![Rspec](https://github.com/peopleforce/validate_params/actions/workflows/rspec.yml/badge.svg)](https://github.com/peopleforce/validate_params/actions/workflows/rspec.yml)
4
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
5
+ ValidateParams is a lightweight, robust Ruby on Rails gem that introduces a simple yet powerful DSL (Domain Specific Language) to validate parameters for your controller actions. It is designed to make your code cleaner, more maintainable, and ensures that your application handles invalid or unexpected parameters gracefully.
8
6
 
9
7
  ## Installation
10
8
 
@@ -18,14 +16,23 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
16
 
19
17
  ## Usage
20
18
 
19
+ Definition of the validator with symbols as keys:
20
+
21
21
  ```ruby
22
22
  class TestController < ActionController::Base
23
23
  include ValidateParams::ParamsValidator
24
24
 
25
25
  validate_params :index do |p|
26
- p.param :id_param, Integer
27
- p.param :date_param, Date
28
- p.param :datetime_param, DateTime
26
+ p.param :occurred_on, Date, required: true
27
+ p.param :quantity, Integer
28
+ p.param :date_of_birth, Hash do |pp|
29
+ pp.param :gt, Date
30
+ pp.param :lt, Date
31
+ end
32
+ p.param :created_at, Hash do |pp|
33
+ pp.param :lt, DateTime
34
+ pp.param :gt, DateTime
35
+ end
29
36
  end
30
37
 
31
38
  def index
@@ -33,8 +40,6 @@ class TestController < ActionController::Base
33
40
  end
34
41
  end
35
42
  ```
36
-
37
-
38
43
  ## Development
39
44
 
40
45
  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.
@@ -44,3 +49,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
44
49
  ## Contributing
45
50
 
46
51
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validate_params.
52
+
53
+ ## Credits
54
+
55
+ Built by the team at [PeopleForce](https://peopleforce.io), the HRM for small to medium sized tech businesses.
@@ -0,0 +1,5 @@
1
+ en:
2
+ validate_params:
3
+ params_validator:
4
+ required: "The parameter %{field} is required"
5
+ invalid: "Invalid %{type} value '%{value}' for parameter %{field}"
@@ -12,9 +12,14 @@ module ValidateParams
12
12
  class_methods do
13
13
  attr_accessor :params_validations, :method
14
14
 
15
- def param(field, type, required = false, default: nil)
15
+ def param(field, type, required: false, default: nil, &block)
16
16
  @params_validations ||= []
17
- @params_validations << { field: field, type: type, required: required, default: default }
17
+ @params_validations <<
18
+ if block
19
+ yield(ParamBuilder.new(field))
20
+ else
21
+ ParamBuilder.new.param(field, type, required: required, default: default)
22
+ end
18
23
  end
19
24
 
20
25
  def validate_params_for(request_action, &block)
@@ -34,8 +39,12 @@ module ValidateParams
34
39
 
35
40
  private
36
41
 
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)
42
+ def build_error_message(field, type, value)
43
+ I18n.t("validate_params.params_validator.invalid", field: field, type: type, value: value)
44
+ end
45
+
46
+ def build_required_message(field)
47
+ I18n.t("validate_params.params_validator.required", field: field)
39
48
  end
40
49
 
41
50
  def error_param_name(field)
@@ -53,7 +62,15 @@ module ValidateParams
53
62
  params_validations.each do |params_validation|
54
63
  next if params_validation[:default].blank?
55
64
 
56
- params[params_validation[:field]] ||= params_validation[:default]
65
+ if params_validation[:field].is_a?(Hash)
66
+ params_validation[:field].each_key do |key|
67
+ next if params.dig(key, params_validation[:field][key])
68
+
69
+ params.merge!(key => { params_validation[:field][key] => params_validation[:default] })
70
+ end
71
+ else
72
+ params[params_validation[:field]] ||= params_validation[:default]
73
+ end
57
74
  end
58
75
  end
59
76
 
@@ -64,14 +81,19 @@ module ValidateParams
64
81
 
65
82
  for params_validation in params_validations
66
83
  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])
84
+ params.dig(params_validation[:field].keys.first,
85
+ params_validation[:field][params_validation[:field].keys.first])
69
86
  else
70
- request.params[params_validation[:field]]
87
+ params[params_validation[:field]]
71
88
  end
72
89
 
73
90
  next if parameter_value.blank? && !params_validation[:required]
74
91
 
92
+ if parameter_value.blank? && params_validation[:required]
93
+ errors << build_required_message(error_param_name(params_validation[:field]))
94
+ next
95
+ end
96
+
75
97
  case params_validation[:type].to_s
76
98
  when "Date"
77
99
  if invalid_date?(parameter_value)
@@ -126,5 +148,19 @@ module ValidateParams
126
148
  def invalid_integer?(value)
127
149
  value !~ /\A[-+]?[0-9]+\z/
128
150
  end
151
+
152
+ class ParamBuilder
153
+ def initialize(parent_field = nil)
154
+ @parent_field = parent_field
155
+ end
156
+
157
+ def param(field, type, required: false, default: nil)
158
+ if @parent_field
159
+ { field: { @parent_field => field }, type: type, required: required, default: default }
160
+ else
161
+ { field: field, type: type, required: required, default: default }
162
+ end
163
+ end
164
+ end
129
165
  end
130
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.2.2"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -24,10 +24,10 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
+ spec.add_development_dependency "actionpack", ">= 6.1.0"
27
28
  spec.add_development_dependency "bundler", "~> 2.0"
28
29
  spec.add_development_dependency "rake", "~> 10.0"
29
30
  spec.add_development_dependency "rspec", "~> 3.0"
30
- spec.add_development_dependency "actionpack", ">= 6.1.0"
31
31
 
32
32
  spec.add_dependency "activesupport", ">= 6.1.0"
33
33
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-11 00:00:00.000000000 Z
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +66,6 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  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: 6.1.0
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: 6.1.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,16 +94,17 @@ files:
94
94
  - Gemfile.lock
95
95
  - README.md
96
96
  - Rakefile
97
+ - config/locales/en.yml
97
98
  - lib/validate_params.rb
98
99
  - lib/validate_params/params_validator.rb
99
100
  - lib/validate_params/version.rb
100
101
  - sig/validate_params.rbs
101
102
  - validate_params.gemspec
102
- homepage:
103
+ homepage:
103
104
  licenses:
104
105
  - MIT
105
106
  metadata: {}
106
- post_install_message:
107
+ post_install_message:
107
108
  rdoc_options: []
108
109
  require_paths:
109
110
  - lib
@@ -118,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  - !ruby/object:Gem::Version
119
120
  version: '0'
120
121
  requirements: []
121
- rubygems_version: 3.2.3
122
- signing_key:
122
+ rubygems_version: 3.0.9
123
+ signing_key:
123
124
  specification_version: 4
124
125
  summary: Gem to validate params in controllers
125
126
  test_files: []