rails_validation_api 0.1.2 → 0.1.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: 7bf4211205049054bf10b62a8fbfed2d4ac067989040c4f33c79bdd549569d9b
4
- data.tar.gz: 68ad48faa55a846431b5a0b09ae0cfeb380b08aaf06b263a37f318dfbd5f6631
3
+ metadata.gz: 6235b21332f0a7bfaf4a75c875cea68a5f5ad0ce8ca389b08bff7a74f3727a03
4
+ data.tar.gz: 965b096a46b34935f3d0233b22d03bff04ee24b9a7f09b1d0d75baaf7fb97a21
5
5
  SHA512:
6
- metadata.gz: 66f22d26a6e9646bb16e394ca1e0cc9221f62d91554a3c71f3872c61e2daf01d9d3e2ab5d45b68a9b40a3ac5b1a5112feb90cbc686980e2df1bb9181ec4b534d
7
- data.tar.gz: a00c52ce98c62d774849af3851020cf76d78cf48fb404f92d4ab651ef2893658a1acee2a830e1013a166ee7906c6d5ffd9aea4a3f5e8de0c77c9f853dd47b543
6
+ metadata.gz: 3fa88927a1182eaeaaff9603d618935329a60e6db922132ce1f3ff44e212384f5a87bda6a28d7d7712089bec3dd6a82e4ad6ffb0d9ab141e9db6791d6077253f
7
+ data.tar.gz: 2f29585b5d46fb9c0c6231fe0797570ed7a814f8de8a5a27d3bc13961aaba959d7590c5313d015f74b9f9025179f62b9bbd0970bf621777f0ff0b2a691fdc86b
@@ -3,24 +3,19 @@
3
3
  require "active_support/core_ext/string/inflections"
4
4
  require "fileutils"
5
5
  require "debug"
6
+ require_relative "./commands/generate_parameter_validator"
7
+
8
+ include GenerateValidator
6
9
 
7
10
  COMMAND = ARGV.shift
8
11
  NAME = ARGV.shift&.downcase
9
12
 
10
- def usage
11
- puts <<~TEXT
12
- Usage:
13
- rails_validation_api install
14
- rails_validation_api generate [validator_name]
15
- rails_validation_api destroy [validator_name]
16
- Example:
17
- rails_validation_api generate purchaseorder
18
- rails_validation_api destroy purchaseorder
19
- TEXT
20
- end
21
13
 
22
14
  def check_inflection(name)
23
- require Rails.root.join('config/initializers/inflections')
15
+ app_root = defined?(Rails) ? Rails.root : Dir.pwd
16
+ inflection_path = File.join(app_root, 'config', 'initializers', 'inflections.rb')
17
+
18
+ require inflection_path if File.exist?(inflection_path)
24
19
  if ActiveSupport::Inflector.inflections.acronyms.key?("api")
25
20
  'API'
26
21
  else
@@ -28,99 +23,15 @@ def check_inflection(name)
28
23
  end
29
24
  end
30
25
 
31
- def generate_parameter_validator(name)
32
- api = check_inflection(name)
33
- class_name = "#{name.camelize}Validator"
34
- path = "app/validators/api/validate_parameters/#{name}_validator.rb"
35
-
36
- FileUtils.mkdir_p(File.dirname(path))
37
-
38
- if File.exist?(path)
39
- puts "⚠️ Validator already exists at #{path}"
40
- else
41
- File.write(path, <<~RUBY)
42
- # frozen_string_literal: true
43
-
44
- class #{api}::ValidateParameters::#{class_name}
45
- FIELDS_VALIDATES = {
46
- # account_id_validate:
47
- # {
48
- # field: :account_id, type: Integer, opts: [
49
- # { required: true, message: "Account id is required" }
50
- # ]
51
- # }
52
- }.freeze
53
-
54
- def index
55
- [
56
- # FIELDS_VALIDATES[:account_id_validate]
57
- ]
58
- end
59
- end
60
- RUBY
61
-
62
- puts "✅ Created #{path}"
63
- end
64
- end
65
-
66
- def destroy_validator(name)
67
- params_path = "app/validators/api/validate_parameters/#{name}_validator.rb"
68
- path = "app/validators/api/#{name}_validator.rb"
69
-
70
- if File.exist?(path)
71
- File.delete(path)
72
- puts "🗑️ Deleted #{path}"
73
- else
74
- puts "⚠️ File not found: #{path}"
75
- end
76
- if File.exist?(params_path)
77
- File.delete(params_path)
78
- puts "🗑️ Deleted #{params_path}"
79
- else
80
- puts "⚠️ File not found: #{params_path}"
81
- end
82
- end
83
-
84
- def generate_validator(name)
85
- api = check_inflection(name)
86
- class_name = "#{name.camelize}Validator"
87
- path = "app/validators/api/#{name}_validator.rb"
88
-
89
- FileUtils.mkdir_p(File.dirname(path))
90
-
91
- if File.exist?(path)
92
- puts "⚠️ Validator already exists at #{path}"
93
- else
94
- File.write(path, <<~RUBY)
95
- # frozen_string_literal: true
96
-
97
- class #{api}::#{class_name}
98
- def initialize(model_name, account)
99
- @model_name = model_name
100
- @account = account
101
- end
102
-
103
- def index(opts)
104
- end
105
- end
106
- RUBY
107
-
108
- puts "✅ Created #{path}"
109
- end
110
- end
111
-
112
26
  case COMMAND
113
- when "install"
114
- puts "🚀 Installing rails_validation_api..."
115
- generate_validator("application")
116
- when "generate"
27
+ when "generate"
117
28
  if NAME.nil?
118
29
  puts "❌ Please provide a name. Example: rails_validation_api generate purchaseorder"
119
30
  else
120
31
  generate_parameter_validator(NAME)
121
32
  generate_validator(NAME)
122
33
  end
123
- when "destroy"
34
+ when "destroy"
124
35
  if NAME.nil?
125
36
  puts "❌ Please provide a name. Example: rails_validation_api generate purchaseorder"
126
37
  else
@@ -5,6 +5,7 @@ module AutoLoadRailsValidationApi
5
5
  included do
6
6
  before_action :set_validation_context
7
7
  before_action :auto_load_method
8
+ rescue_from RailsValidationApi::Error, with: :render_validation_error
8
9
  end
9
10
 
10
11
  def auto_load_method
@@ -30,14 +31,15 @@ module AutoLoadRailsValidationApi
30
31
  end
31
32
 
32
33
  def validator_name
33
- "API::#{@model_name}Validator"
34
+ "#{@name}::#{@model_name}Validator"
34
35
  end
35
36
 
36
37
  def param_object_validator_name
37
- "API::ValidateParameters::#{@model_name}Validator"
38
+ "#{@name}::ValidateParameters::#{@model_name}Validator"
38
39
  end
39
40
 
40
41
  def call_validator(action: nil, opts: {})
42
+ check_inflection
41
43
  set_validator
42
44
  action = (action || action_name).to_sym
43
45
  opts = opts.blank? ? params : opts
@@ -69,4 +71,26 @@ module AutoLoadRailsValidationApi
69
71
  end
70
72
  end
71
73
  end
74
+
75
+ def check_inflection
76
+ app_root = defined?(Rails) ? Rails.root : Dir.pwd
77
+ inflection_path = File.join(app_root, "config", "initializers", "inflections.rb")
78
+
79
+ require inflection_path if File.exist?(inflection_path)
80
+ @name ||= if ActiveSupport::Inflector.inflections.acronyms.key?("api")
81
+ "API"
82
+ else
83
+ "Api"
84
+ end
85
+ end
86
+
87
+ def render_validation_error(exception)
88
+ render json: {
89
+ errors: {
90
+ field: exception.field,
91
+ message: exception.message,
92
+ additional_info: exception.additional_info
93
+ }
94
+ }, status: exception.status
95
+ end
72
96
  end
@@ -48,7 +48,7 @@ module RailsValidationApi
48
48
 
49
49
  begin
50
50
  param! field, type, **opt
51
- rescue RailsParam::Param::InvalidParameterError => e
51
+ rescue RailsParam::InvalidParameterError => e
52
52
  message = (e.message).include?(type.to_s) ? e.message : opt[:message]
53
53
  @errors << { field: field, message: message }
54
54
  end
@@ -79,7 +79,7 @@ module RailsValidationApi
79
79
 
80
80
  begin
81
81
  temp_validator.param! item_field, item_type, **opt
82
- rescue RailsParam::Param::InvalidParameterError => e
82
+ rescue RailsParam::InvalidParameterError => e
83
83
  message = (e.message).include?(item_type.to_s) ? e.message : opt[:message]
84
84
  @errors << { field: "#{parent_field}.#{item_field}", message: message }
85
85
  end
@@ -1,3 +1,3 @@
1
1
  module RailsValidationApi
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.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: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linh Nguyen Quang
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails_param
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.9.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.9.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activemodel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '5.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '5.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: debug
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +80,6 @@ extra_rdoc_files: []
80
80
  files:
81
81
  - exe/rails_validation_api
82
82
  - lib/auto_load_rails_validation_api.rb
83
- - lib/error/custom_error.rb
84
83
  - lib/rails_validation_api.rb
85
84
  - lib/rails_validation_api/dsl.rb
86
85
  - lib/rails_validation_api/validator.rb
@@ -1,11 +0,0 @@
1
- module Error
2
- class CustomError < StandardError
3
- attr_reader :status, :details
4
-
5
- def initialize(status = :bad_request, message = nil, details: nil)
6
- @status = status
7
- @details = details
8
- super(message || "Something went wrong")
9
- end
10
- end
11
- end