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 +4 -4
- data/exe/commands/usage.rb +15 -14
- data/lib/rails_validation_api/validator.rb +32 -92
- data/lib/rails_validation_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f8b0173edc6150f0f3da1222f676773aa9546df4eeeeee80e0b0c4807a14a9c
|
4
|
+
data.tar.gz: 31d304de82e0e4e467e4d24bc2fea7fb53904e4aa9a440d9cdc8d5193d429df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f3e4352a23015c25f7d2dfabf4fcd6354db63222b7a543ffd52497f8f4ab256e1b52b2fea6ad009df82b29421bacb4d797b29605bc45983449dcc265f0ed0f7
|
7
|
+
data.tar.gz: afe6fb7862f8f704bfea8bbabc8dde452e6590f8e64f5acd19b3b8bda7f980d595156c00a5074593b2bc6aa600206161568e3badb0e86eb8c220f28b1c4685d1
|
data/exe/commands/usage.rb
CHANGED
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
13
|
-
|
14
|
-
|
|
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 "
|
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 |
|
21
|
-
validate_field(
|
23
|
+
@rules.each do |rule_name, rule|
|
24
|
+
validate_field(rule_name)
|
22
25
|
end
|
23
26
|
if @errors.any?
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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 &&
|
72
|
-
validate_nested_items(field, items,
|
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
|
-
#
|
89
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|