validate-params 0.9.0 → 0.10.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: abce4d094f5a56366394cab4c9c01e3bbaca5a9ed5cb3524754d941a7a90d6e6
4
- data.tar.gz: 17aa70265348cae7d3f661fa07419da90c4ba1b4b08ccf881baaa5bd556bedf4
3
+ metadata.gz: 4e686013f02bbb7e5fd50eeabb70a7b3abdd3fdcc8458b3e2230d1a288435988
4
+ data.tar.gz: e2434d12a8b52721b930b426133189db03a84a7dbcb86ed57c1ca98c4b4b06b3
5
5
  SHA512:
6
- metadata.gz: 4cd4813db2797307b1e47956f02fffe2358cb7aaa130b5248ac0a8635e32c63a582a190d619af1d5bb5d7e4cf80f590862e97a96edab99c39dbbe86533de833d
7
- data.tar.gz: 01dbe50eee3aed0eb9ee06dac9035a5e7c81f9f658cdf84e14fc888913a8ed2842049294f7c8c95ee175fb9c0240c66e5193fc87d8008d2c2cb1fb1691f41f6c
6
+ metadata.gz: 809c060682bd2e8b9e49781589f5fe45bd91abfdfdba7d41b1285b12fef5e8d27fb8476acdd8e1ffee73ec1a16c3f5017acb8348faa0e5b9c8735d499496c094
7
+ data.tar.gz: 34ee15be73ac94a817b144f246b43e89623c4a81261035448e432bce589b51971d3b8022ac6732a8e27197f0f21efb918b85535866a4109d63582a0a7d12da9a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.10.0] - 2023-08-28
4
+
5
+ - Added support for IO type to validate file uploads
6
+
3
7
  ## [0.9.0] - 2023-08-02
4
8
 
5
9
  - Add :min and :max options to validate param values for Integer types
data/Gemfile CHANGED
@@ -6,4 +6,4 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
- gem "rubocop", "~> 1.55"
9
+ gem "rubocop", "~> 1.56"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.9.0)
4
+ validate-params (0.10.0)
5
5
  activesupport (>= 6.1.0)
6
6
  i18n (>= 1.6)
7
7
 
@@ -28,6 +28,7 @@ GEM
28
28
  tzinfo (~> 2.0)
29
29
  zeitwerk (~> 2.3)
30
30
  ast (2.4.2)
31
+ base64 (0.1.1)
31
32
  builder (3.2.4)
32
33
  concurrent-ruby (1.2.2)
33
34
  crass (1.0.6)
@@ -49,7 +50,7 @@ GEM
49
50
  parser (3.2.2.3)
50
51
  ast (~> 2.4.1)
51
52
  racc
52
- racc (1.6.2)
53
+ racc (1.7.1)
53
54
  rack (2.2.7)
54
55
  rack-test (2.1.0)
55
56
  rack (>= 1.3)
@@ -75,7 +76,8 @@ GEM
75
76
  diff-lcs (>= 1.2.0, < 2.0)
76
77
  rspec-support (~> 3.12.0)
77
78
  rspec-support (3.12.0)
78
- rubocop (1.55.1)
79
+ rubocop (1.56.0)
80
+ base64 (~> 0.1.1)
79
81
  json (~> 2.3)
80
82
  language_server-protocol (>= 3.17.0)
81
83
  parallel (~> 1.10)
@@ -102,7 +104,7 @@ DEPENDENCIES
102
104
  bundler (~> 2.0)
103
105
  rake (~> 13.0)
104
106
  rspec (~> 3.0)
105
- rubocop (~> 1.55)
107
+ rubocop (~> 1.56)
106
108
  validate-params!
107
109
 
108
110
  BUNDLED WITH
data/README.md CHANGED
@@ -2,7 +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
- 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.
5
+ ValidateParams is a lightweight, robust Ruby on Rails gem that introduces a simple yet powerful DSL (Domain Specific Language) to validate and type cast the 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.
6
6
 
7
7
  ## Installation
8
8
 
@@ -25,9 +25,11 @@ class TestController < ActionController::Base
25
25
  validate_params :index do |p|
26
26
  p.param :name, String, default: "John Doe"
27
27
  p.param :occurred_on, Date, required: true, default: proc { Date.today }
28
+ p.param :per_page, Integer, default: 50, min: 1, max: 50
28
29
  p.param :quantity, Integer, required: true, in: [1, 2, 3]
29
30
  p.param :user_ids, Array, of: Integer, default: [1, 2, 3]
30
31
  p.param :states, Array, of: String, default: ["active", "inactive"], reject_blank: true
32
+ p.param :file, IO, min: 1.byte, max: 1.megabyte
31
33
  p.param :date_of_birth, Hash do |pp|
32
34
  pp.param :gt, Date, min: Date.new(2020, 1, 1), max: Date.new(2021, 1, 1)
33
35
  pp.param :lt, Date
@@ -44,10 +46,25 @@ class TestController < ActionController::Base
44
46
  end
45
47
  ```
46
48
 
49
+ ### Types
50
+
51
+ Here are the following supported types along with operations supported.
52
+
53
+ - String (required, default)
54
+ - Integer (required, default, min, max, in)
55
+ - Date (required, default, min, max)
56
+ - DateTime (required, default, min, max)
57
+ - IO (required, min, max)
58
+ - Array of: (String|Integer) (default, reject_blank)
59
+ - Hash - Nested block of types
60
+
61
+
47
62
  ## Response
48
63
 
49
64
  If the parameters are valid, the controller action will be executed as normal. If the parameters are invalid, a **400 Bad Request** response will be returned with a JSON body containing the errors, or an empty HTML response.
50
65
 
66
+ ### JSON (Default)
67
+
51
68
  ```json
52
69
  {
53
70
  "success": false,
@@ -55,14 +72,22 @@ If the parameters are valid, the controller action will be executed as normal. I
55
72
  {
56
73
  "message": "hired_on must be a valid Date"
57
74
  },
75
+ {
76
+ "message": "per_page cannot be more than maximum",
77
+ "max": 50
78
+ },
79
+ {
80
+ "message": "states is an invalid value",
81
+ "valid_values": ["active", "inactive"]
82
+ },
58
83
 
59
84
  ]
60
85
  }
61
86
  ```
62
87
 
63
- ## Format
88
+ ### HTML
64
89
 
65
- By default responses are returned in JSON format. To return responses as an empty HTML response, change the :format options in the validate_params methods to :html.
90
+ By default responses are returned in JSON format. To return responses as an empty HTML response with a **400 Bad Request** status, change the :format option in the validate_params methods to **:html**.
66
91
 
67
92
  Example:
68
93
 
@@ -76,6 +76,18 @@ module ValidateParams
76
76
  validate_inclusion if @options[:in].present?
77
77
  end
78
78
 
79
+ def io
80
+ unless Types::IO.valid?(@value)
81
+ @errors << { message: error_message }
82
+ return
83
+ end
84
+
85
+ formatted_value = Types::IO.cast(@value)
86
+
87
+ validate_min(formatted_value) if @options[:min].present?
88
+ validate_max(formatted_value) if @options[:max].present?
89
+ end
90
+
79
91
  def validate_inclusion
80
92
  return if @options[:in].include?(@value)
81
93
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidateParams
4
+ class Types
5
+ class IO
6
+ def self.valid?(value)
7
+ value.class.method_defined?(:size)
8
+ end
9
+
10
+ def self.cast(raw_value, **)
11
+ raw_value.size
12
+ end
13
+ end
14
+ end
15
+ end
@@ -5,6 +5,7 @@ require "validate_params/types/date_time"
5
5
  require "validate_params/types/integer"
6
6
  require "validate_params/types/array"
7
7
  require "validate_params/types/string"
8
+ require "validate_params/types/i_o"
8
9
  require_relative "param_builder"
9
10
  require_relative "param_validator"
10
11
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.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-08-07 00:00:00.000000000 Z
11
+ date: 2023-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -116,17 +116,18 @@ files:
116
116
  - lib/validate_params/types/array.rb
117
117
  - lib/validate_params/types/date.rb
118
118
  - lib/validate_params/types/date_time.rb
119
+ - lib/validate_params/types/i_o.rb
119
120
  - lib/validate_params/types/integer.rb
120
121
  - lib/validate_params/types/string.rb
121
122
  - lib/validate_params/validatable.rb
122
123
  - lib/validate_params/version.rb
123
124
  - sig/validate_params.rbs
124
125
  - validate_params.gemspec
125
- homepage:
126
+ homepage:
126
127
  licenses:
127
128
  - MIT
128
129
  metadata: {}
129
- post_install_message:
130
+ post_install_message:
130
131
  rdoc_options: []
131
132
  require_paths:
132
133
  - lib
@@ -141,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
142
  - !ruby/object:Gem::Version
142
143
  version: '0'
143
144
  requirements: []
144
- rubygems_version: 3.4.10
145
- signing_key:
145
+ rubygems_version: 3.2.3
146
+ signing_key:
146
147
  specification_version: 4
147
148
  summary: Gem to validate params in controllers
148
149
  test_files: []