44require 'ipaddr'
55require 'uri'
66
7- # rubocop:disable Metrics/ModuleLength
87module Minfraud
98 # @!visibility private
9+ # Validates provides validation helper methods for component input values.
10+ # These methods are used internally when validation is enabled via
11+ # Minfraud.enable_validation.
12+ #
13+ # rubocop:disable Metrics/ModuleLength
1014 module Validates
15+ # Validates that a string value does not exceed a maximum length.
16+ #
17+ # @param field [String] The field name for error messages.
18+ # @param length [Integer] The maximum allowed length.
19+ # @param value [String, nil] The value to validate.
20+ #
21+ # @raise [InvalidInputError] If the value exceeds the maximum length.
22+ #
23+ # @return [nil]
1124 def validate_string ( field , length , value )
1225 return if !value
1326
@@ -16,6 +29,14 @@ def validate_string(field, length, value)
1629 end
1730 end
1831
32+ # Validates that a value is a valid MD5 hash string.
33+ #
34+ # @param field [String] The field name for error messages.
35+ # @param value [String, nil] The value to validate.
36+ #
37+ # @raise [InvalidInputError] If the value is not a valid MD5 hash.
38+ #
39+ # @return [nil]
1940 def validate_md5 ( field , value )
2041 return if !value
2142
@@ -24,6 +45,14 @@ def validate_md5(field, value)
2445 end
2546 end
2647
48+ # Validates that a value is a valid UUID string.
49+ #
50+ # @param field [String] The field name for error messages.
51+ # @param value [String, nil] The value to validate.
52+ #
53+ # @raise [InvalidInputError] If the value is not a valid UUID.
54+ #
55+ # @return [nil]
2756 def validate_uuid ( field , value )
2857 return if !value
2958
@@ -37,6 +66,14 @@ def validate_uuid(field, value)
3766 end
3867 end
3968
69+ # Validates that a value is a valid ISO 3166-2 subdivision code.
70+ #
71+ # @param field [String] The field name for error messages.
72+ # @param value [String, nil] The value to validate.
73+ #
74+ # @raise [InvalidInputError] If the value is not a valid subdivision code.
75+ #
76+ # @return [nil]
4077 def validate_subdivision_code ( field , value )
4178 return if !value
4279
@@ -45,6 +82,14 @@ def validate_subdivision_code(field, value)
4582 end
4683 end
4784
85+ # Validates that a value is a valid ISO 3166-1 alpha-2 country code.
86+ #
87+ # @param field [String] The field name for error messages.
88+ # @param value [String, nil] The value to validate.
89+ #
90+ # @raise [InvalidInputError] If the value is not a valid country code.
91+ #
92+ # @return [nil]
4893 def validate_country_code ( field , value )
4994 return if !value
5095
@@ -53,6 +98,15 @@ def validate_country_code(field, value)
5398 end
5499 end
55100
101+ # Validates that a value is a valid telephone country code (1-4 digits).
102+ #
103+ # @param field [String] The field name for error messages.
104+ # @param value [String, nil] The value to validate.
105+ #
106+ # @raise [InvalidInputError] If the value is not a valid telephone country
107+ # code.
108+ #
109+ # @return [nil]
56110 def validate_telephone_country_code ( field , value )
57111 return if !value
58112
@@ -61,6 +115,15 @@ def validate_telephone_country_code(field, value)
61115 end
62116 end
63117
118+ # Validates that a value matches a regular expression pattern.
119+ #
120+ # @param field [String] The field name for error messages.
121+ # @param regex [Regexp] The regular expression pattern to match.
122+ # @param value [String, nil] The value to validate.
123+ #
124+ # @raise [InvalidInputError] If the value does not match the pattern.
125+ #
126+ # @return [nil]
64127 def validate_regex ( field , regex , value )
65128 return if !value
66129
@@ -69,6 +132,14 @@ def validate_regex(field, regex, value)
69132 end
70133 end
71134
135+ # Validates that a value is a valid credit card token.
136+ #
137+ # @param field [String] The field name for error messages.
138+ # @param value [String, nil] The value to validate.
139+ #
140+ # @raise [InvalidInputError] If the value is not a valid credit card token.
141+ #
142+ # @return [nil]
72143 def validate_credit_card_token ( field , value )
73144 return if !value
74145
@@ -83,6 +154,14 @@ def validate_credit_card_token(field, value)
83154 end
84155 end
85156
157+ # Validates a custom input value (boolean, numeric, or string).
158+ #
159+ # @param field [String] The field name for error messages.
160+ # @param value [Boolean, Numeric, String, nil] The value to validate.
161+ #
162+ # @raise [InvalidInputError] If the value is not valid.
163+ #
164+ # @return [nil]
86165 def validate_custom_input_value ( field , value )
87166 return if !value
88167
@@ -101,6 +180,14 @@ def validate_custom_input_value(field, value)
101180 validate_string ( field , 255 , value )
102181 end
103182
183+ # Validates that a value is a valid IPv4 or IPv6 address.
184+ #
185+ # @param field [String] The field name for error messages.
186+ # @param value [String, nil] The value to validate.
187+ #
188+ # @raise [InvalidInputError] If the value is not a valid IP address.
189+ #
190+ # @return [nil]
104191 def validate_ip ( field , value )
105192 return if !value
106193
@@ -120,6 +207,15 @@ def validate_ip(field, value)
120207 nil
121208 end
122209
210+ # Validates that a value is a non-negative number within range.
211+ #
212+ # @param field [String] The field name for error messages.
213+ # @param value [Numeric, nil] The value to validate.
214+ #
215+ # @raise [InvalidInputError] If the value is not a valid non-negative
216+ # number.
217+ #
218+ # @return [nil]
123219 def validate_nonnegative_number ( field , value )
124220 return if !value
125221
@@ -132,6 +228,15 @@ def validate_nonnegative_number(field, value)
132228 end
133229 end
134230
231+ # Validates that a value is a non-negative integer within range.
232+ #
233+ # @param field [String] The field name for error messages.
234+ # @param value [Integer, nil] The value to validate.
235+ #
236+ # @raise [InvalidInputError] If the value is not a valid non-negative
237+ # integer.
238+ #
239+ # @return [nil]
135240 def validate_nonnegative_integer ( field , value )
136241 return if !value
137242
@@ -144,6 +249,14 @@ def validate_nonnegative_integer(field, value)
144249 end
145250 end
146251
252+ # Validates that a value is a valid email address or MD5 hash of one.
253+ #
254+ # @param field [String] The field name for error messages.
255+ # @param value [String, nil] The value to validate.
256+ #
257+ # @raise [InvalidInputError] If the value is not a valid email or MD5 hash.
258+ #
259+ # @return [nil]
147260 def validate_email ( field , value )
148261 return if !value
149262
@@ -155,6 +268,14 @@ def validate_email(field, value)
155268 validate_md5 ( field , value )
156269 end
157270
271+ # Validates that a value is in RFC 3339 date-time format.
272+ #
273+ # @param field [String] The field name for error messages.
274+ # @param value [String, nil] The value to validate.
275+ #
276+ # @raise [InvalidInputError] If the value is not in RFC 3339 format.
277+ #
278+ # @return [nil]
158279 def validate_rfc3339 ( field , value )
159280 return if !value
160281
@@ -169,6 +290,14 @@ def validate_rfc3339(field, value)
169290 nil
170291 end
171292
293+ # Validates that a value is a boolean.
294+ #
295+ # @param field [String] The field name for error messages.
296+ # @param value [Boolean, nil] The value to validate.
297+ #
298+ # @raise [InvalidInputError] If the value is not a boolean.
299+ #
300+ # @return [nil]
172301 def validate_boolean ( field , value )
173302 return if !value
174303
@@ -177,6 +306,14 @@ def validate_boolean(field, value)
177306 end
178307 end
179308
309+ # Validates that a value is a valid absolute URI.
310+ #
311+ # @param field [String] The field name for error messages.
312+ # @param value [String, nil] The value to validate.
313+ #
314+ # @raise [InvalidInputError] If the value is not a valid absolute URI.
315+ #
316+ # @return [nil]
180317 def validate_uri ( field , value )
181318 return if !value
182319
@@ -195,6 +332,6 @@ def validate_uri(field, value)
195332 end
196333 # rubocop:enable Style/RescueStandardError
197334 end
335+ # rubocop:enable Metrics/ModuleLength
198336 end
199337end
200- # rubocop:enable Metrics/ModuleLength
0 commit comments