rails_validation_api 1.2.2 → 1.2.4

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: b4f6763bbc5879669935aedb9a4c25437da3a78c480ee2d0fd89a8c20ba52eec
4
- data.tar.gz: d262f3175cb60f276c0f28b286fa2fcf3ccf294275943da75c6eb9e8b5dc8810
3
+ metadata.gz: 83ba96c72236af10ef1179343132ef56d9f9b83e00daaa79cb40b0380dfbcbb7
4
+ data.tar.gz: 0673b7734ebda2392018999e750b171daecb63c65904ba98011625ed86c9e3d1
5
5
  SHA512:
6
- metadata.gz: 0baf32fb6aca2cde5a15c874f5f46d69b3d7e3c0ab00f9f7cb51583359a70c454c2dc259bc981fd22dafe832c08758358d0659b6b4f881c3e8a58ca8a9ebe290
7
- data.tar.gz: 737cf138b2137716889652e0155f9dd9905527a4bb3f439b9b0395201db551d01ed9e115eeb3d855d08d7cc30b76c81922b3c11e65f2a4030bc28864bf8bcf29
6
+ metadata.gz: b1f6669cc6aa2ddf529280dd74558f13e77147db34af9b23e80a792187248dfb8c293a34723c01d18880b9e5ada817656c3da10e046d4b2663196e35b56651d5
7
+ data.tar.gz: 18ca8b1b28f3a2675db6dc344b4b64ea0630faeefa18649382b2f6cd7cf801e56c5883d1cdf0da026f2b73570a3f55a126be697e025c46667f274a0be8b7e6bd
@@ -1,7 +1,6 @@
1
1
  puts <<~TEXT
2
2
  -------------------------------------------------------------------------------------------------------------------------------------------------
3
- | Add "include AutoLoadRailsValidationApi" to your controller ex: API::BaseController < ApplicationController to use the RailsValidationApi |
4
- | Add "config.autoload_paths += %w(\#{config.root}/app/validators/)" in config/application.rb to autoload validators. |
3
+ | Install: bundle exec rails generate rails_validation_api:install |
5
4
  |-----------------------------------------------------------------------------------------------------------------------------------------------| |
6
5
  | To use the RailsValidationApi, you need to add the following line to your Gemfile: |
7
6
  | Usage: bundle exec rails_validation_api <command> [name] |
@@ -0,0 +1,13 @@
1
+ require "rails/generators"
2
+
3
+ module RailsValidationApi
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def copy_initializer
9
+ template "rails_validation_api.rb", "config/initializers/rails_validation_api.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require "auto_load_rails_validation_api"
2
+
3
+ Rails.application.config.autoload_paths += %W(#{Rails.root}/app/validators)
4
+
5
+ ActiveSupport.on_load(:action_controller) do
6
+ include AutoLoadRailsValidationApi
7
+ end
@@ -2,9 +2,12 @@ require "date"
2
2
  require "time"
3
3
  require "bigdecimal"
4
4
  require "active_support/all"
5
- require "debug"
5
+ require "rails_param"
6
+
6
7
  module RailsValidationApi
7
8
  class Validator
9
+ include RailsParam
10
+
8
11
  attr_accessor :params
9
12
  attr_reader :errors
10
13
 
@@ -17,14 +20,15 @@ module RailsValidationApi
17
20
  def validate
18
21
  return false if @rules.nil? || @rules.empty?
19
22
 
20
- @rules.each do |rule, _|
21
- validate_field(rule)
23
+ @rules.each do |rule_name, rule|
24
+ validate_field(rule_name)
22
25
  end
23
26
  if @errors.any?
24
- # Only raise the first error to match expected behavior
25
- first_error = @errors.first
26
- raise RailsValidationApi::Error.new(first_error[:field], :unprocessable_entity, first_error[:message])
27
+ @errors.each do |error|
28
+ raise RailsValidationApi::Error.new(error[:field], :unprocessable_entity, error[:message])
29
+ end
27
30
  end
31
+ nil
28
32
  end
29
33
 
30
34
  private
@@ -39,37 +43,32 @@ module RailsValidationApi
39
43
 
40
44
  return unless field && type
41
45
 
42
- # Get the field value from params
43
- value = params[field]
44
-
45
46
  # Validate the main field
46
47
  opts.each do |opt|
47
48
  next unless opt.is_a?(Hash)
48
49
 
49
- # Check if field is required and missing
50
- if opt[:required] && (value.nil? || (value.is_a?(String) && value.empty?))
51
- message = opt[:message] || "Parameter #{field} is required"
52
- @errors << { field: field, message: message }
53
- next
54
- end
55
-
56
- # Skip further validation if field is not required and is nil/empty
57
- next if !opt[:required] && (value.nil? || (value.is_a?(String) && value.empty?))
58
-
59
- # Type validation
60
- unless value.nil? || valid_type?(value, type)
61
- message = opt[:message] || "Parameter #{field} must be of type #{type}"
50
+ begin
51
+ if type == Array && opt[:type] # => Nested item type inside Array
52
+ param! field, Array do |array_param, index|
53
+ begin
54
+ array_param.param! index, opt[:type], **opt.except(:type)
55
+ rescue RailsParam::InvalidParameterError => e
56
+ message = opt[:message] || e.message || "Invalid value at index #{index} for #{field}"
57
+ @errors << { field: "#{field}[#{index}]", message: message }
58
+ end
59
+ end
60
+ else
61
+ param! field, type, **opt
62
+ end
63
+ rescue RailsParam::InvalidParameterError => e
64
+ message = (e.message).include?(type.to_s) ? e.message : opt[:message]
62
65
  @errors << { field: field, message: message }
63
- next
64
66
  end
65
-
66
- # Additional validations
67
- validate_options(field, value, opt)
68
67
  end
69
68
 
70
69
  # Validate nested items if present (for Hash/Array fields)
71
- if items && value.is_a?(Hash)
72
- validate_nested_items(field, items, value)
70
+ if items && params[field].is_a?(Hash)
71
+ validate_nested_items(field, items, params[field])
73
72
  end
74
73
  end
75
74
 
@@ -85,88 +84,19 @@ module RailsValidationApi
85
84
 
86
85
  next unless item_field && item_type
87
86
 
88
- # Validate nested field
89
- value = nested_params[item_field]
87
+ # Create a temporary validator for nested validation
88
+ temp_validator = self.class.new(nested_params, {})
90
89
  item_opts.each do |opt|
91
90
  next unless opt.is_a?(Hash)
92
91
 
93
- # Check if field is required and missing
94
- if opt[:required] && (value.nil? || (value.is_a?(String) && value.empty?))
95
- message = opt[:message] || "Parameter #{parent_field}.#{item_field} is required"
92
+ begin
93
+ temp_validator.param! item_field, item_type, **opt
94
+ rescue RailsParam::InvalidParameterError => e
95
+ message = (e.message).include?(item_type.to_s) ? e.message : opt[:message]
96
96
  @errors << { field: "#{parent_field}.#{item_field}", message: message }
97
- next
98
97
  end
99
-
100
- # Skip further validation if field is not required and is nil/empty
101
- next if !opt[:required] && (value.nil? || (value.is_a?(String) && value.empty?))
102
-
103
- # Type validation
104
- unless value.nil? || valid_type?(value, item_type)
105
- message = opt[:message] || "Parameter #{parent_field}.#{item_field} must be of type #{item_type}"
106
- @errors << { field: "#{parent_field}.#{item_field}", message: message }
107
- next
108
- end
109
-
110
- # Additional validations
111
- validate_options("#{parent_field}.#{item_field}", value, opt)
112
98
  end
113
99
  end
114
100
  end
115
-
116
- def valid_type?(value, type)
117
- value = format_string_to_int(value, type)
118
- case type.to_s
119
- when String.to_s
120
- value.is_a?(String)
121
- when Integer.to_s
122
- value.is_a?(Integer)
123
- when Float.to_s
124
- value.is_a?(Float) || value.is_a?(Integer)
125
- when Hash.to_s
126
- value.is_a?(Hash)
127
- when Array.to_s
128
- value.is_a?(Array)
129
- when TrueClass.to_s, FalseClass.to_s
130
- value.is_a?(TrueClass) || value.is_a?(FalseClass)
131
- else
132
- value.is_a?(type)
133
- end
134
- end
135
-
136
- def validate_options(field, value, opt)
137
- # Handle min validation
138
- if opt[:min] && value.respond_to?(:to_i) && value.to_i < opt[:min]
139
- message = opt[:message] || "Parameter #{field} must be at least #{opt[:min]}"
140
- @errors << { field: field, message: message }
141
- end
142
-
143
- # Handle max validation
144
- if opt[:max] && value.respond_to?(:to_i) && value.to_i > opt[:max]
145
- message = opt[:message] || "Parameter #{field} must be at most #{opt[:max]}"
146
- @errors << { field: field, message: message }
147
- end
148
-
149
- # Handle format validation
150
- if opt[:format] && value.is_a?(String) && !(value =~ opt[:format])
151
- message = opt[:message] || "Parameter #{field} format is invalid"
152
- @errors << { field: field, message: message }
153
- end
154
-
155
- # Handle blank validation
156
- if opt[:blank] == false && value.is_a?(String) && value.strip.empty?
157
- message = opt[:message] || "Parameter #{field} cannot be blank"
158
- @errors << { field: field, message: message }
159
- end
160
- end
161
-
162
- def format_string_to_int(value, type)
163
- return value unless value.is_a?(String)
164
-
165
- return Integer(value) rescue value if type == Integer
166
- return Float(value) rescue value if type == Float
167
- value
168
- rescue ArgumentError
169
- value
170
- end
171
101
  end
172
102
  end
@@ -1,3 +1,3 @@
1
1
  module RailsValidationApi
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_validation_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linh Nguyen Quang
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: railties
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: debug
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -96,6 +110,8 @@ files:
96
110
  - exe/commands/usage.rb
97
111
  - exe/rails_validation_api
98
112
  - lib/auto_load_rails_validation_api.rb
113
+ - lib/generators/rails_validation_api/install_generator.rb
114
+ - lib/generators/rails_validation_api/templates/rails_validation_api.rb
99
115
  - lib/rails_validation_api.rb
100
116
  - lib/rails_validation_api/dsl.rb
101
117
  - lib/rails_validation_api/validator.rb