rails_validation_api 1.2.2 → 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: b4f6763bbc5879669935aedb9a4c25437da3a78c480ee2d0fd89a8c20ba52eec
4
- data.tar.gz: d262f3175cb60f276c0f28b286fa2fcf3ccf294275943da75c6eb9e8b5dc8810
3
+ metadata.gz: 0f8b0173edc6150f0f3da1222f676773aa9546df4eeeeee80e0b0c4807a14a9c
4
+ data.tar.gz: 31d304de82e0e4e467e4d24bc2fea7fb53904e4aa9a440d9cdc8d5193d429df1
5
5
  SHA512:
6
- metadata.gz: 0baf32fb6aca2cde5a15c874f5f46d69b3d7e3c0ab00f9f7cb51583359a70c454c2dc259bc981fd22dafe832c08758358d0659b6b4f881c3e8a58ca8a9ebe290
7
- data.tar.gz: 737cf138b2137716889652e0155f9dd9905527a4bb3f439b9b0395201db551d01ed9e115eeb3d855d08d7cc30b76c81922b3c11e65f2a4030bc28864bf8bcf29
6
+ metadata.gz: 4f3e4352a23015c25f7d2dfabf4fcd6354db63222b7a543ffd52497f8f4ab256e1b52b2fea6ad009df82b29421bacb4d797b29605bc45983449dcc265f0ed0f7
7
+ data.tar.gz: afe6fb7862f8f704bfea8bbabc8dde452e6590f8e64f5acd19b3b8bda7f980d595156c00a5074593b2bc6aa600206161568e3badb0e86eb8c220f28b1c4685d1
@@ -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,88 +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"
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]
96
95
  @errors << { field: "#{parent_field}.#{item_field}", message: message }
97
- next
98
96
  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
97
  end
113
98
  end
114
99
  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
100
  end
172
101
  end
@@ -1,3 +1,3 @@
1
1
  module RailsValidationApi
2
- VERSION = "1.2.2"
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.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linh Nguyen Quang