rails_validation_api 0.1.4 → 1.0.0
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/lib/rails_validation_api/validator.rb +92 -21
- data/lib/rails_validation_api/version.rb +1 -1
- data/lib/rails_validation_api.rb +4 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4738d992bee3205cae2870825ce733c1393b20287d6b6844fc620eddfdf5c5d2
|
4
|
+
data.tar.gz: 93541cc5680f3d92a7e95fb431e38e093ac1214b64e582a64cf2d1439f0b9867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72f830494882bbc9f489bc18c1ef5c453a26480657618d6b9dd5b3978af38c4b1bfe3ab21bd54d4749cc9a88f1c3530c0faa46dbdb065969ad7d985d35a8e543
|
7
|
+
data.tar.gz: 1e48b670f9b871b5882f5570c5d1bfdf934f7a3f9ec56e35f288e8f85e4008eb0fc3ad8945259410425e0ac0fcffffa75e3bc57dd85cdb4409276541f0eadb17
|
@@ -2,12 +2,9 @@ require "date"
|
|
2
2
|
require "time"
|
3
3
|
require "bigdecimal"
|
4
4
|
require "active_support/all"
|
5
|
-
require "
|
6
|
-
|
5
|
+
require "debug"
|
7
6
|
module RailsValidationApi
|
8
7
|
class Validator
|
9
|
-
include RailsParam
|
10
|
-
|
11
8
|
attr_accessor :params
|
12
9
|
attr_reader :errors
|
13
10
|
|
@@ -20,13 +17,13 @@ module RailsValidationApi
|
|
20
17
|
def validate
|
21
18
|
return false if @rules.nil? || @rules.empty?
|
22
19
|
|
23
|
-
@rules.each do |
|
24
|
-
validate_field(
|
20
|
+
@rules.each do |rule, _|
|
21
|
+
validate_field(rule)
|
25
22
|
end
|
26
23
|
if @errors.any?
|
27
|
-
|
28
|
-
|
29
|
-
|
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])
|
30
27
|
end
|
31
28
|
end
|
32
29
|
|
@@ -42,21 +39,37 @@ module RailsValidationApi
|
|
42
39
|
|
43
40
|
return unless field && type
|
44
41
|
|
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
|
-
message = (e.message).include?(type.to_s) ? e.message : opt[:message]
|
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"
|
53
52
|
@errors << { field: field, message: message }
|
53
|
+
next
|
54
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}"
|
62
|
+
@errors << { field: field, message: message }
|
63
|
+
next
|
64
|
+
end
|
65
|
+
|
66
|
+
# Additional validations
|
67
|
+
validate_options(field, value, opt)
|
55
68
|
end
|
56
69
|
|
57
70
|
# Validate nested items if present (for Hash/Array fields)
|
58
|
-
if items &&
|
59
|
-
validate_nested_items(field, items,
|
71
|
+
if items && value.is_a?(Hash)
|
72
|
+
validate_nested_items(field, items, value)
|
60
73
|
end
|
61
74
|
end
|
62
75
|
|
@@ -72,19 +85,77 @@ module RailsValidationApi
|
|
72
85
|
|
73
86
|
next unless item_field && item_type
|
74
87
|
|
75
|
-
#
|
76
|
-
|
88
|
+
# Validate nested field
|
89
|
+
value = nested_params[item_field]
|
77
90
|
item_opts.each do |opt|
|
78
91
|
next unless opt.is_a?(Hash)
|
79
92
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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}"
|
84
106
|
@errors << { field: "#{parent_field}.#{item_field}", message: message }
|
107
|
+
next
|
85
108
|
end
|
109
|
+
|
110
|
+
# Additional validations
|
111
|
+
validate_options("#{parent_field}.#{item_field}", value, opt)
|
86
112
|
end
|
87
113
|
end
|
88
114
|
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
|
89
160
|
end
|
90
161
|
end
|
data/lib/rails_validation_api.rb
CHANGED
@@ -3,8 +3,11 @@
|
|
3
3
|
require "logger"
|
4
4
|
require "active_model"
|
5
5
|
require "active_support"
|
6
|
-
require "rails_param"
|
7
6
|
require "active_support/core_ext/hash"
|
7
|
+
require "date"
|
8
|
+
require "time"
|
9
|
+
require "bigdecimal"
|
10
|
+
require "rails_param"
|
8
11
|
require_relative "rails_validation_api/dsl"
|
9
12
|
require_relative "rails_validation_api/validator"
|
10
13
|
require "active_support/concern"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_validation_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Linh Nguyen Quang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-07-
|
11
|
+
date: 2025-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
69
83
|
description: Rails Validation API provides a powerful DSL for validating request parameters
|
70
84
|
in Rails applications. Features include automatic validator loading based on controller/action
|
71
85
|
names, nested parameter validation, custom error handling, and seamless integration
|