rails_validation_api 1.2.0 → 1.2.2

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: b2e0c05e2f9557b374836280b8519994e5975e1863e690cc37ca3371a1124d89
4
- data.tar.gz: bc433ab541ae2b18f683c61b2f3070d3e97966b77592ec10bc65cad19d9f27aa
3
+ metadata.gz: b4f6763bbc5879669935aedb9a4c25437da3a78c480ee2d0fd89a8c20ba52eec
4
+ data.tar.gz: d262f3175cb60f276c0f28b286fa2fcf3ccf294275943da75c6eb9e8b5dc8810
5
5
  SHA512:
6
- metadata.gz: 88d8a71badbcadf927b7c44768a30843ed4d6c5d4ed333c20d4a9ca410cdc1fa5cec0ddc5229eb0cccf781ab3d78ddb5e611be72179c48ef6e13b1eafbc3ce88
7
- data.tar.gz: 9f8cecae3ccc4fb1648e7afaf8e130ad351aa1ca3fe59d647ae8711d40388d9b24b29bc2da763f9bc7e7c1e4034b51c2d17392201225eda53ee4877de5fbdccd
6
+ metadata.gz: 0baf32fb6aca2cde5a15c874f5f46d69b3d7e3c0ab00f9f7cb51583359a70c454c2dc259bc981fd22dafe832c08758358d0659b6b4f881c3e8a58ca8a9ebe290
7
+ data.tar.gz: 737cf138b2137716889652e0155f9dd9905527a4bb3f439b9b0395201db551d01ed9e115eeb3d855d08d7cc30b76c81922b3c11e65f2a4030bc28864bf8bcf29
@@ -0,0 +1,86 @@
1
+ module GenerateValidator
2
+ def generate_parameter_validator(name)
3
+ api = check_inflection(name)
4
+ class_name = "#{name.camelize}Validator"
5
+ path = "app/validators/api/validate_parameters/#{name}_validator.rb"
6
+
7
+ FileUtils.mkdir_p(File.dirname(path))
8
+
9
+ if File.exist?(path)
10
+ puts "⚠️ Validator already exists at #{path}"
11
+ else
12
+ File.write(path, <<~RUBY)
13
+ # frozen_string_literal: true
14
+
15
+ class #{api}::ValidateParameters::#{class_name}
16
+ FIELDS_VALIDATES = {
17
+ # account_id_validate:
18
+ # {
19
+ # field: :account_id, type: Integer, opts: [
20
+ # { required: true, message: "Account id is required" }
21
+ # ]
22
+ # }
23
+ }.freeze
24
+
25
+ def index
26
+ [
27
+ # FIELDS_VALIDATES[:account_id_validate]
28
+ ]
29
+ end
30
+ end
31
+ RUBY
32
+
33
+ puts "✅ Created #{path}"
34
+ end
35
+ end
36
+
37
+ def destroy_validator(name)
38
+ params_path = "app/validators/api/validate_parameters/#{name}_validator.rb"
39
+ path = "app/validators/api/#{name}_validator.rb"
40
+
41
+ if File.exist?(path)
42
+ File.delete(path)
43
+ puts "🗑️ Deleted #{path}"
44
+ else
45
+ puts "⚠️ File not found: #{path}"
46
+ end
47
+ if File.exist?(params_path)
48
+ File.delete(params_path)
49
+ puts "🗑️ Deleted #{params_path}"
50
+ else
51
+ puts "⚠️ File not found: #{params_path}"
52
+ end
53
+ end
54
+
55
+ def generate_validator(name)
56
+ api = check_inflection(name)
57
+ class_name = "#{name.camelize}Validator"
58
+ path = "app/validators/api/#{name}_validator.rb"
59
+
60
+ FileUtils.mkdir_p(File.dirname(path))
61
+
62
+ if File.exist?(path)
63
+ puts "⚠️ Validator already exists at #{path}"
64
+ else
65
+ File.write(path, <<~RUBY)
66
+ # frozen_string_literal: true
67
+
68
+ class #{api}::#{class_name}
69
+ def initialize(model_name, account)
70
+ @model_name = model_name
71
+ @account = account
72
+ end
73
+
74
+ def index(opts)
75
+ end
76
+ end
77
+ RUBY
78
+
79
+ puts "✅ Created #{path}"
80
+ end
81
+ end
82
+
83
+ def usage
84
+ require_relative "./usage"
85
+ end
86
+ end
@@ -0,0 +1,17 @@
1
+ puts <<~TEXT
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
+ |------------------------------------------------------------------------------------------------------------------------------------------------
17
+ TEXT
@@ -114,18 +114,19 @@ module RailsValidationApi
114
114
  end
115
115
 
116
116
  def valid_type?(value, type)
117
- case type
118
- when String
117
+ value = format_string_to_int(value, type)
118
+ case type.to_s
119
+ when String.to_s
119
120
  value.is_a?(String)
120
- when Integer
121
+ when Integer.to_s
121
122
  value.is_a?(Integer)
122
- when Float
123
+ when Float.to_s
123
124
  value.is_a?(Float) || value.is_a?(Integer)
124
- when Hash
125
+ when Hash.to_s
125
126
  value.is_a?(Hash)
126
- when Array
127
+ when Array.to_s
127
128
  value.is_a?(Array)
128
- when TrueClass, FalseClass
129
+ when TrueClass.to_s, FalseClass.to_s
129
130
  value.is_a?(TrueClass) || value.is_a?(FalseClass)
130
131
  else
131
132
  value.is_a?(type)
@@ -157,5 +158,15 @@ module RailsValidationApi
157
158
  @errors << { field: field, message: message }
158
159
  end
159
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
160
171
  end
161
172
  end
@@ -1,3 +1,3 @@
1
1
  module RailsValidationApi
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.2"
3
3
  end
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: 1.2.0
4
+ version: 1.2.2
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-05 00:00:00.000000000 Z
11
+ date: 2025-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description: Rails Validation API provides a powerful DSL for validating request parameters
83
+ description: Rails Validation API provides a powerful for validating request parameters
84
84
  in Rails applications. Features include automatic validator loading based on controller/action
85
85
  names, nested parameter validation, custom error handling, and seamless integration
86
86
  with Rails controllers through concerns. Perfect for API applications requiring
@@ -92,6 +92,8 @@ executables:
92
92
  extensions: []
93
93
  extra_rdoc_files: []
94
94
  files:
95
+ - exe/commands/generate_parameter_validator.rb
96
+ - exe/commands/usage.rb
95
97
  - exe/rails_validation_api
96
98
  - lib/auto_load_rails_validation_api.rb
97
99
  - lib/rails_validation_api.rb