pastore 0.0.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pastore
4
+ module Params
5
+ # Implements the validation logic for number parameters.
6
+ class NumberValidation < Validation
7
+ def initialize(name, value, modifier, **options)
8
+ @min = options[:min]
9
+ @max = options[:max]
10
+ @clamp = options[:clamp] || [-Float::INFINITY, Float::INFINITY]
11
+
12
+ super(name, 'number', value, modifier, **options)
13
+ end
14
+
15
+ private
16
+
17
+ def validate!
18
+ # check for value presence and if it's allowed to be blank
19
+ check_presence!
20
+
21
+ # don't go further if value is blank
22
+ return if value.to_s.strip == ''
23
+
24
+ # check if value is a number
25
+ # check if number is between min and max
26
+ # check if value is within specified clamping range, and correct if necessary
27
+ return unless check_if_number! && check_min_max! && check_clamp!
28
+
29
+ # check if value is in the list of allowed values
30
+ check_allowed_values!
31
+
32
+ # apply the modifier
33
+ apply_modifier!
34
+ end
35
+
36
+ def check_if_number!
37
+ return true if value.is_a?(Integer) || value.is_a?(Float)
38
+
39
+ if value.is_a?(String) && numeric?
40
+ @value = value.to_f
41
+ @value = value.to_i if value.modulo(1).zero?
42
+
43
+ return true
44
+ end
45
+
46
+ add_error(:invalid_type, "#{@name} has invalid type: #{@type} expected")
47
+
48
+ false
49
+ end
50
+
51
+ def check_min_max!
52
+ min_invalid = @min && value < @min
53
+ max_invalid = @max && value > @max
54
+
55
+ add_error(:too_small, "#{@name} should be greater than #{@min}") if min_invalid
56
+ add_error(:too_large, "#{@name} should be smaller than #{@max}") if max_invalid
57
+
58
+ min_invalid || max_invalid ? false : true
59
+ end
60
+
61
+ def check_clamp!
62
+ @value = @value.clamp(@clamp.first || -Float::INFINITY, @clamp.last || Float::INFINITY)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pastore
4
+ module Params
5
+ # Implements the validation logic for object parameters.
6
+ class ObjectValidation < Validation
7
+ def initialize(name, value, modifier, **options)
8
+ super(name, 'object', value, modifier, **options)
9
+ end
10
+
11
+ private
12
+
13
+ def validate!
14
+ # check for value presence and if it's allowed to be blank
15
+ check_presence!
16
+
17
+ # don't go further if value is blank
18
+ return if value.to_s.strip == ''
19
+
20
+ # check if value is a boolean
21
+ return unless check_if_object!
22
+
23
+ # check if value is in the list of allowed values
24
+ check_allowed_values!
25
+
26
+ # apply the modifier
27
+ apply_modifier!
28
+ end
29
+
30
+ def check_if_object!
31
+ return true if [Hash, HashWithIndifferentAccess, ActionController::Parameters, Array].include?(value.class)
32
+
33
+ # When value is a string, try to parse it as JSON
34
+ if value.is_a?(String)
35
+ begin
36
+ @value = JSON.parse(value)
37
+ return true
38
+ rescue JSON::ParserError
39
+ # Do nothing
40
+ end
41
+ end
42
+
43
+ add_error(:invalid_type, "#{@name} has invalid type: #{@type} expected")
44
+
45
+ false
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pastore
4
+ module Params
5
+ # Implements the validation logic for string parameters.
6
+ class StringValidation < Validation
7
+ def initialize(name, value, modifier, **options)
8
+ @format = options[:format]
9
+
10
+ super(name, 'string', value, modifier, **options)
11
+ end
12
+
13
+ private
14
+
15
+ def validate!
16
+ # check for value presence and if it's allowed to be blank
17
+ check_presence!
18
+
19
+ # don't go further if value is blank
20
+ return if value.to_s.strip == ''
21
+
22
+ # check if value is a string
23
+ return unless check_if_string! && check_format!
24
+
25
+ # check if value is in the list of allowed values
26
+ check_allowed_values!
27
+
28
+ # apply the modifier
29
+ apply_modifier!
30
+ end
31
+
32
+ def check_if_string!
33
+ return true if value.is_a?(String)
34
+
35
+ @value = @value.to_s
36
+
37
+ true
38
+ end
39
+
40
+ # Check if the value matches the specified format
41
+ def check_format!
42
+ return true if @format.nil?
43
+
44
+ add_error(:invalid_format, "#{@name} has invalid format") if value.match(@format).nil?
45
+
46
+ true
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'params/settings'
5
+
6
+ module Pastore
7
+ # Implements the features for Rails controller params validation.
8
+ module Params
9
+ class ParamAlreadyDefinedError < StandardError; end
10
+ class ScopeConflictError < StandardError; end
11
+ class InvalidValidationTypeError < StandardError; end
12
+ class InvalidParamTypeError < StandardError; end
13
+ class InvalidValueError < StandardError; end
14
+
15
+ extend ActiveSupport::Concern
16
+
17
+ included do
18
+ before_action do
19
+ pastore_params = self.class.pastore_params
20
+ validation_errors = pastore_params.validate(params, action_name)
21
+ next if validation_errors.empty?
22
+
23
+ if pastore_params.invalid_params_cbk.present?
24
+ instance_eval(&pastore_params.invalid_params_cbk)
25
+ response.status = pastore_params.response_status
26
+ else
27
+ render json: { message: 'Unprocessable Entity', errors: validation_errors }, status: pastore_params.response_status
28
+ end
29
+ end
30
+ end
31
+
32
+ # Implement Pastore::Params class methods
33
+ module ClassMethods
34
+ attr_accessor :_pastore_params
35
+
36
+ def pastore_params
37
+ self._pastore_params ||= Settings.new(superclass)
38
+ end
39
+
40
+ def on_invalid_params(&block)
41
+ pastore_params.invalid_params_cbk = block
42
+ end
43
+
44
+ def invalid_params_status(status)
45
+ pastore_params.response_status = status
46
+ end
47
+
48
+ def param(name, **options)
49
+ pastore_params.add(name, **options)
50
+ end
51
+
52
+ def scope(*keys, &block)
53
+ return unless block_given?
54
+
55
+ pastore_params.set_scope(*keys)
56
+ block.call
57
+ pastore_params.reset_scope!
58
+ end
59
+
60
+ def method_added(name, *args, &block)
61
+ pastore_params.save_for(name)
62
+ super
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pastore
4
- VERSION = '0.0.4'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/pastore.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'pastore/version'
4
4
  require_relative 'pastore/guards'
5
- require_relative 'pastore/params_validators'
5
+ require_relative 'pastore/params'
6
6
 
7
7
  module Pastore
8
8
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pastore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Groza Sergiu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-02 00:00:00.000000000 Z
11
+ date: 2023-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -42,6 +42,7 @@ extra_rdoc_files: []
42
42
  files:
43
43
  - ".rspec"
44
44
  - ".rubocop.yml"
45
+ - ".vscode/launch.json"
45
46
  - CHANGELOG.md
46
47
  - CODE_OF_CONDUCT.md
47
48
  - Gemfile
@@ -49,10 +50,20 @@ files:
49
50
  - LICENSE
50
51
  - README.md
51
52
  - Rakefile
53
+ - docs/Guards.md
54
+ - docs/Params.md
52
55
  - lib/pastore.rb
53
56
  - lib/pastore/guards.rb
54
57
  - lib/pastore/guards/settings.rb
55
- - lib/pastore/params_validators.rb
58
+ - lib/pastore/params.rb
59
+ - lib/pastore/params/action_param.rb
60
+ - lib/pastore/params/settings.rb
61
+ - lib/pastore/params/validation.rb
62
+ - lib/pastore/params/validations/boolean_validation.rb
63
+ - lib/pastore/params/validations/date_validation.rb
64
+ - lib/pastore/params/validations/number_validation.rb
65
+ - lib/pastore/params/validations/object_validation.rb
66
+ - lib/pastore/params/validations/string_validation.rb
56
67
  - lib/pastore/version.rb
57
68
  homepage: https://github.com/demetra-it/pastore
58
69
  licenses:
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pastore
4
- # Implements the features for Rails controller params validation.
5
- module ParamsValidators
6
- end
7
- end