rails_validation_api 1.2.1 → 1.2.3

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: 3aba9716be3690814ad64c4423454b33e674df5f1b46306c4a029ab8c18e58e2
4
- data.tar.gz: f880f5c5f1238c1c2f84030698e9ff07a6403658155e7ae4d30fa0f2425e1fc4
3
+ metadata.gz: 0f8b0173edc6150f0f3da1222f676773aa9546df4eeeeee80e0b0c4807a14a9c
4
+ data.tar.gz: 31d304de82e0e4e467e4d24bc2fea7fb53904e4aa9a440d9cdc8d5193d429df1
5
5
  SHA512:
6
- metadata.gz: c0cdaefd1e1c058ae02114353e562f5d7e3fdca4edb2c4c223934df2608701e453ab7c9e045b7167d3387011eac835a6f07a89967ed55a9ac78b07c3a8f14529
7
- data.tar.gz: 4103a6c5597681e1ee4bb73f1a1f40afe7c3a91a3660c03340a27ec330098d81303d34856e4787e4c88e05568d084e150c970c3fbaf636ebfa9b8473e23d60ee
6
+ metadata.gz: 4f3e4352a23015c25f7d2dfabf4fcd6354db63222b7a543ffd52497f8f4ab256e1b52b2fea6ad009df82b29421bacb4d797b29605bc45983449dcc265f0ed0f7
7
+ data.tar.gz: afe6fb7862f8f704bfea8bbabc8dde452e6590f8e64f5acd19b3b8bda7f980d595156c00a5074593b2bc6aa600206161568e3badb0e86eb8c220f28b1c4685d1
@@ -1,16 +1,17 @@
1
1
  puts <<~TEXT
2
- -------------------------------------------------------------------------------------------------------------------------------------------------
3
- | Add "include AutoLoadRailsValidationApi" to your controller ex: API::BaseController < ApplicationController to use the RailsValidationApi |
4
- |-----------------------------------------------------------------------------------------------------------------------------------------------| |
5
- | To use the RailsValidationApi, you need to add the following line to your Gemfile: |
6
- | Usage: bundle exec rails_validation_api <command> [name] |
7
- | Commands: |
8
- | install Install the rails_validation_api gem. |
9
- | generate <name> Generate a parameter validator and a validator for the specified name. |
10
- | generate_parameter <name> Generate a parameter validator for the specified name. |
11
- | destroy <name> Destroy the validator and parameter validator for the specified name. |
12
- |------------------------------------------------------------------------------------------------------------------------------------------------
13
- | Example: |
14
- | bundle exec rails_validation_api generate purchaseorder |
15
- |------------------------------------------------------------------------------------------------------------------------------------------------
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. |
5
+ |-----------------------------------------------------------------------------------------------------------------------------------------------| |
6
+ | To use the RailsValidationApi, you need to add the following line to your Gemfile: |
7
+ | Usage: bundle exec rails_validation_api <command> [name] |
8
+ | Commands: |
9
+ | install Install the rails_validation_api gem. |
10
+ | generate <name> Generate a parameter validator and a validator for the specified name. |
11
+ | generate_parameter <name> Generate a parameter validator for the specified name. |
12
+ | destroy <name> Destroy the validator and parameter validator for the specified name. |
13
+ |------------------------------------------------------------------------------------------------------------------------------------------------
14
+ | Example: |
15
+ | bundle exec rails_validation_api generate purchaseorder |
16
+ |------------------------------------------------------------------------------------------------------------------------------------------------
16
17
  TEXT
@@ -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,13 +20,13 @@ 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
28
31
  end
29
32
 
@@ -39,37 +42,32 @@ module RailsValidationApi
39
42
 
40
43
  return unless field && type
41
44
 
42
- # Get the field value from params
43
- value = params[field]
44
-
45
45
  # Validate the main field
46
46
  opts.each do |opt|
47
47
  next unless opt.is_a?(Hash)
48
48
 
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}"
49
+ begin
50
+ if type == Array && opt[:type] # => Nested item type inside Array
51
+ param! field, Array do |array_param, index|
52
+ begin
53
+ array_param.param! index, opt[:type], **opt.except(:type)
54
+ rescue RailsParam::InvalidParameterError => e
55
+ message = opt[:message] || e.message || "Invalid value at index #{index} for #{field}"
56
+ @errors << { field: "#{field}[#{index}]", message: message }
57
+ end
58
+ end
59
+ else
60
+ param! field, type, **opt
61
+ end
62
+ rescue RailsParam::InvalidParameterError => e
63
+ message = (e.message).include?(type.to_s) ? e.message : opt[:message]
62
64
  @errors << { field: field, message: message }
63
- next
64
65
  end
65
-
66
- # Additional validations
67
- validate_options(field, value, opt)
68
66
  end
69
67
 
70
68
  # Validate nested items if present (for Hash/Array fields)
71
- if items && value.is_a?(Hash)
72
- validate_nested_items(field, items, value)
69
+ if items && params[field].is_a?(Hash)
70
+ validate_nested_items(field, items, params[field])
73
71
  end
74
72
  end
75
73
 
@@ -85,77 +83,19 @@ module RailsValidationApi
85
83
 
86
84
  next unless item_field && item_type
87
85
 
88
- # Validate nested field
89
- value = nested_params[item_field]
86
+ # Create a temporary validator for nested validation
87
+ temp_validator = self.class.new(nested_params, {})
90
88
  item_opts.each do |opt|
91
89
  next unless opt.is_a?(Hash)
92
90
 
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"
96
- @errors << { field: "#{parent_field}.#{item_field}", message: message }
97
- next
98
- 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}"
91
+ begin
92
+ temp_validator.param! item_field, item_type, **opt
93
+ rescue RailsParam::InvalidParameterError => e
94
+ message = (e.message).include?(item_type.to_s) ? e.message : opt[:message]
106
95
  @errors << { field: "#{parent_field}.#{item_field}", message: message }
107
- next
108
96
  end
109
-
110
- # Additional validations
111
- validate_options("#{parent_field}.#{item_field}", value, opt)
112
97
  end
113
98
  end
114
99
  end
115
-
116
- def valid_type?(value, type)
117
- case type
118
- when String
119
- value.is_a?(String)
120
- when Integer
121
- value.is_a?(Integer)
122
- when Float
123
- value.is_a?(Float) || value.is_a?(Integer)
124
- when Hash
125
- value.is_a?(Hash)
126
- when Array
127
- value.is_a?(Array)
128
- when TrueClass, FalseClass
129
- value.is_a?(TrueClass) || value.is_a?(FalseClass)
130
- else
131
- value.is_a?(type)
132
- end
133
- end
134
-
135
- def validate_options(field, value, opt)
136
- # Handle min validation
137
- if opt[:min] && value.respond_to?(:to_i) && value.to_i < opt[:min]
138
- message = opt[:message] || "Parameter #{field} must be at least #{opt[:min]}"
139
- @errors << { field: field, message: message }
140
- end
141
-
142
- # Handle max validation
143
- if opt[:max] && value.respond_to?(:to_i) && value.to_i > opt[:max]
144
- message = opt[:message] || "Parameter #{field} must be at most #{opt[:max]}"
145
- @errors << { field: field, message: message }
146
- end
147
-
148
- # Handle format validation
149
- if opt[:format] && value.is_a?(String) && !(value =~ opt[:format])
150
- message = opt[:message] || "Parameter #{field} format is invalid"
151
- @errors << { field: field, message: message }
152
- end
153
-
154
- # Handle blank validation
155
- if opt[:blank] == false && value.is_a?(String) && value.strip.empty?
156
- message = opt[:message] || "Parameter #{field} cannot be blank"
157
- @errors << { field: field, message: message }
158
- end
159
- end
160
100
  end
161
101
  end
@@ -1,3 +1,3 @@
1
1
  module RailsValidationApi
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.3"
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.1
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linh Nguyen Quang