servactory 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/config/locales/en.yml +3 -3
  3. data/config/locales/ru.yml +3 -3
  4. data/lib/servactory/context/workspace/inputs.rb +8 -6
  5. data/lib/servactory/context/workspace/internals.rb +2 -2
  6. data/lib/servactory/context/workspace/outputs.rb +2 -2
  7. data/lib/servactory/context/workspace.rb +5 -0
  8. data/lib/servactory/inputs/input.rb +20 -2
  9. data/lib/servactory/inputs/tools/validation.rb +5 -3
  10. data/lib/servactory/inputs/validations/required.rb +10 -9
  11. data/lib/servactory/inputs/workspace.rb +1 -2
  12. data/lib/servactory/internals/internal.rb +22 -1
  13. data/lib/servactory/maintenance/attributes/options/registrar.rb +11 -11
  14. data/lib/servactory/maintenance/attributes/tools/check_errors.rb +22 -0
  15. data/lib/servactory/maintenance/attributes/tools/validation.rb +81 -0
  16. data/lib/servactory/maintenance/attributes/validations/base.rb +23 -0
  17. data/lib/servactory/maintenance/attributes/validations/errors.rb +18 -0
  18. data/lib/servactory/maintenance/attributes/validations/inclusion.rb +75 -0
  19. data/lib/servactory/maintenance/attributes/validations/must.rb +102 -0
  20. data/lib/servactory/maintenance/attributes/validations/type.rb +62 -0
  21. data/lib/servactory/maintenance/validations/types.rb +10 -18
  22. data/lib/servactory/outputs/output.rb +22 -1
  23. data/lib/servactory/version.rb +2 -2
  24. metadata +10 -12
  25. data/lib/servactory/inputs/tools/check_errors.rb +0 -20
  26. data/lib/servactory/inputs/tools/distributor.rb +0 -24
  27. data/lib/servactory/inputs/validations/inclusion.rb +0 -66
  28. data/lib/servactory/inputs/validations/must.rb +0 -99
  29. data/lib/servactory/inputs/validations/type.rb +0 -52
  30. data/lib/servactory/internals/validations/base.rb +0 -17
  31. data/lib/servactory/internals/validations/type.rb +0 -31
  32. data/lib/servactory/outputs/validations/base.rb +0 -17
  33. data/lib/servactory/outputs/validations/type.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4c3af571fde0fe1cc117e71ed04c7b13ab3d212f5524ad684382e905462fc8d
4
- data.tar.gz: 95c2ad162f85d874eb250aa1db1104e8aae685e1f738ea9b37d94f19ebc6c92a
3
+ metadata.gz: 1fd508e1e2a77b4d74acb00666dbc5149009fdb11f927144554f3d4d66a63887
4
+ data.tar.gz: ac3cd518c70d56f1089e2975afffedca41983555e4c54fc5d815d94ea0d1fa4c
5
5
  SHA512:
6
- metadata.gz: 4a1385bd8a58e5931523772619812f53168593ab5135b2c9e92f6eda4ed90097a37bd7665e56669fca6e99303d406ad9ebe68684713f63ed0b8c530cf7f5bfb9
7
- data.tar.gz: a720e57fece727a1374802a6b419c40eca107be60325a7282aa34559b9b6e519f8891f210c0c2185ea614c70b343b881803c24a5662dc9c863100f3a2cefdb22
6
+ metadata.gz: bcf0363a0d2d83cfd6ddffd4ea19254a51c2cf93eb33b83f79ee6129f7e38a8fe1bbedfc19cde97d3cfc78ee3d48d8550afa39c7d1d78488a10bfb8d7be609e8
7
+ data.tar.gz: 5ad30d0c89119bd1a9ccfc6b536c14276cd6197f28ef2d449f0f38a3ce3c962bba6bea7a60c4b365ff76adaa1bd284a8ddc0908b6319dc3bd895458f5d7d8ddd
@@ -4,7 +4,7 @@ en:
4
4
  undefined:
5
5
  getter: "[%{service_class_name}] Undefined input attribute `%{input_name}`"
6
6
  setter: "[%{service_class_name}] Undefined input attribute `%{input_name}`"
7
- checks:
7
+ validations:
8
8
  inclusion:
9
9
  default_error: "[%{service_class_name}] Wrong value in `%{input_name}`, must be one of `%{input_inclusion}`"
10
10
  must:
@@ -30,7 +30,7 @@ en:
30
30
  undefined:
31
31
  getter: "[%{service_class_name}] Undefined internal attribute `%{internal_name}`"
32
32
  setter: "[%{service_class_name}] Undefined internal attribute `%{internal_name}`"
33
- checks:
33
+ validations:
34
34
  type:
35
35
  default_error:
36
36
  default: "[%{service_class_name}] Wrong type of internal attribute `%{internal_name}`, expected `%{expected_type}`, got `%{given_type}`"
@@ -43,7 +43,7 @@ en:
43
43
  undefined:
44
44
  getter: "[%{service_class_name}] Undefined output attribute `%{output_name}`"
45
45
  setter: "[%{service_class_name}] Undefined output attribute `%{output_name}`"
46
- checks:
46
+ validations:
47
47
  type:
48
48
  default_error:
49
49
  default: "[%{service_class_name}] Wrong type of output attribute `%{output_name}`, expected `%{expected_type}`, got `%{given_type}`"
@@ -4,7 +4,7 @@ ru:
4
4
  undefined:
5
5
  getter: "[%{service_class_name}] Неизвестный входящий атрибут `%{input_name}`"
6
6
  setter: "[%{service_class_name}] Неизвестный входящий атрибут `%{input_name}`"
7
- checks:
7
+ validations:
8
8
  inclusion:
9
9
  default_error: "[%{service_class_name}] Неправильное значение в `%{input_name}`, должно быть одним из `%{input_inclusion}`"
10
10
  must:
@@ -31,7 +31,7 @@ ru:
31
31
  undefined:
32
32
  getter: "[%{service_class_name}] Неизвестный внутренний атрибут `%{internal_name}`"
33
33
  setter: "[%{service_class_name}] Неизвестный внутренний атрибут `%{internal_name}`"
34
- checks:
34
+ validations:
35
35
  type:
36
36
  default_error:
37
37
  default: "[%{service_class_name}] Неправильный тип внутреннего атрибута `%{internal_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
@@ -44,7 +44,7 @@ ru:
44
44
  undefined:
45
45
  getter: "[%{service_class_name}] Неизвестный выходящий атрибут `%{output_name}`"
46
46
  setter: "[%{service_class_name}] Неизвестный выходящий атрибут `%{output_name}`"
47
- checks:
47
+ validations:
48
48
  type:
49
49
  default: "[%{service_class_name}] Неправильный тип выходящего атрибута `%{output_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
50
50
  for_collection:
@@ -7,8 +7,9 @@ module Servactory
7
7
  RESERVED_ATTRIBUTES = %i[type required default].freeze
8
8
  private_constant :RESERVED_ATTRIBUTES
9
9
 
10
- def initialize(context:, collection_of_inputs:)
10
+ def initialize(context:, incoming_arguments:, collection_of_inputs:)
11
11
  @context = context
12
+ @incoming_arguments = incoming_arguments
12
13
  @collection_of_inputs = collection_of_inputs
13
14
  end
14
15
 
@@ -47,19 +48,20 @@ module Servactory
47
48
 
48
49
  return yield if input.nil?
49
50
 
50
- input.value = input.default if input.optional? && input.value.blank?
51
+ input_value = @incoming_arguments.fetch(input.name, nil)
52
+ input_value = input.default if input.optional? && input_value.blank?
51
53
 
52
54
  if input.hash_mode? && (tmp_schema = input.schema.fetch(:is)).present?
53
- input.value = prepare_hash_values_inside(object: input.value, schema: tmp_schema)
55
+ input_value = prepare_hash_values_inside(object: input_value, schema: tmp_schema)
54
56
  end
55
57
 
56
58
  input_prepare = input.prepare.fetch(:in, nil)
57
- input.value = input_prepare.call(value: input.value) if input_prepare.present?
59
+ input_value = input_prepare.call(value: input_value) if input_prepare.present?
58
60
 
59
61
  if name.to_s.end_with?("?")
60
- Servactory::Utils.query_attribute(input.value)
62
+ Servactory::Utils.query_attribute(input_value)
61
63
  else
62
- input.value
64
+ input_value
63
65
  end
64
66
  end
65
67
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Lint/UnusedMethodArgument
@@ -44,9 +44,9 @@ module Servactory
44
44
 
45
45
  return yield if internal.nil?
46
46
 
47
- Servactory::Internals::Validations::Type.validate!(
47
+ Servactory::Maintenance::Attributes::Tools::Validation.validate!(
48
48
  context: @context,
49
- internal: internal,
49
+ attribute: internal,
50
50
  value: value
51
51
  )
52
52
 
@@ -44,9 +44,9 @@ module Servactory
44
44
 
45
45
  return yield if output.nil?
46
46
 
47
- Servactory::Outputs::Validations::Type.validate!(
47
+ Servactory::Maintenance::Attributes::Tools::Validation.validate!(
48
48
  context: @context,
49
- output: output,
49
+ attribute: output,
50
50
  value: value
51
51
  )
52
52
 
@@ -6,6 +6,7 @@ module Servactory
6
6
  def inputs
7
7
  @inputs ||= Inputs.new(
8
8
  context: self,
9
+ incoming_arguments: incoming_arguments,
9
10
  collection_of_inputs: collection_of_inputs
10
11
  )
11
12
  end
@@ -35,6 +36,10 @@ module Servactory
35
36
  raise self.class.config.failure_class.new(message: message, meta: meta)
36
37
  end
37
38
 
39
+ def fail_result!(service_result)
40
+ fail!(message: service_result.error.message, meta: service_result.error.meta)
41
+ end
42
+
38
43
  private
39
44
 
40
45
  attr_reader :incoming_arguments,
@@ -3,8 +3,6 @@
3
3
  module Servactory
4
4
  module Inputs
5
5
  class Input
6
- attr_accessor :value
7
-
8
6
  attr_reader :name,
9
7
  :internal_name,
10
8
  :collection_of_options
@@ -86,9 +84,29 @@ module Servactory
86
84
  @collection_of_options.defined_conflict_code
87
85
  end
88
86
 
87
+ def system_name
88
+ self.class.name.demodulize.downcase.to_sym
89
+ end
90
+
91
+ def i18n_name
92
+ system_name.to_s.pluralize
93
+ end
94
+
89
95
  def with_conflicts?
90
96
  conflict_code.present?
91
97
  end
98
+
99
+ def input?
100
+ true
101
+ end
102
+
103
+ def internal?
104
+ false
105
+ end
106
+
107
+ def output?
108
+ false
109
+ end
92
110
  end
93
111
  end
94
112
  end
@@ -8,8 +8,9 @@ module Servactory
8
8
  new(...).validate!
9
9
  end
10
10
 
11
- def initialize(context, collection_of_inputs)
11
+ def initialize(context, incoming_arguments, collection_of_inputs)
12
12
  @context = context
13
+ @incoming_arguments = incoming_arguments
13
14
  @collection_of_inputs = collection_of_inputs
14
15
  end
15
16
 
@@ -50,7 +51,8 @@ module Servactory
50
51
  )
51
52
  validation_class.check(
52
53
  context: @context,
53
- input: input,
54
+ attribute: input,
55
+ value: @incoming_arguments.fetch(input.name, nil),
54
56
  check_key: check_key,
55
57
  check_options: check_options
56
58
  )
@@ -65,7 +67,7 @@ module Servactory
65
67
  ########################################################################
66
68
 
67
69
  def errors
68
- @errors ||= CheckErrors.new
70
+ @errors ||= Servactory::Maintenance::Attributes::Tools::CheckErrors.new
69
71
  end
70
72
 
71
73
  def raise_errors
@@ -5,7 +5,7 @@ module Servactory
5
5
  module Validations
6
6
  class Required < Base
7
7
  DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
8
- i18n_key = "servactory.inputs.checks.required.default_error."
8
+ i18n_key = "servactory.inputs.validations.required.default_error."
9
9
  i18n_key += input.collection_mode? && value.present? ? "for_collection" : "default"
10
10
 
11
11
  I18n.t(
@@ -17,10 +17,10 @@ module Servactory
17
17
 
18
18
  private_constant :DEFAULT_MESSAGE
19
19
 
20
- def self.check(context:, input:, check_key:, **)
21
- return unless should_be_checked_for?(input, check_key)
20
+ def self.check(context:, attribute:, value:, check_key:, **)
21
+ return unless should_be_checked_for?(attribute, check_key)
22
22
 
23
- new(context: context, input: input).check
23
+ new(context: context, input: attribute, value: value).check
24
24
  end
25
25
 
26
26
  def self.should_be_checked_for?(input, check_key)
@@ -29,17 +29,18 @@ module Servactory
29
29
 
30
30
  ##########################################################################
31
31
 
32
- def initialize(context:, input:)
32
+ def initialize(context:, input:, value:)
33
33
  super()
34
34
 
35
35
  @context = context
36
36
  @input = input
37
+ @value = value
37
38
  end
38
39
 
39
40
  def check
40
- if @input.collection_mode? && Servactory::Utils.value_present?(@input.value)
41
- return if @input.value.respond_to?(:all?) && @input.value.all?(&:present?)
42
- elsif Servactory::Utils.value_present?(@input.value)
41
+ if @input.collection_mode? && Servactory::Utils.value_present?(@value)
42
+ return if @value.respond_to?(:all?) && @value.all?(&:present?)
43
+ elsif Servactory::Utils.value_present?(@value)
43
44
  return
44
45
  end
45
46
 
@@ -55,7 +56,7 @@ module Servactory
55
56
  message: message.presence || DEFAULT_MESSAGE,
56
57
  service_class_name: @context.class.name,
57
58
  input: @input,
58
- value: @input.value
59
+ value: @value
59
60
  )
60
61
  end
61
62
  end
@@ -9,9 +9,8 @@ module Servactory
9
9
  super
10
10
 
11
11
  Tools::Unnecessary.find!(self, incoming_arguments, collection_of_inputs)
12
- Tools::Distributor.assign!(incoming_arguments, collection_of_inputs)
13
12
  Tools::Rules.check!(self, collection_of_inputs)
14
- Tools::Validation.validate!(self, collection_of_inputs)
13
+ Tools::Validation.validate!(self, incoming_arguments, collection_of_inputs)
15
14
  end
16
15
  end
17
16
  end
@@ -3,7 +3,8 @@
3
3
  module Servactory
4
4
  module Internals
5
5
  class Internal
6
- attr_reader :name
6
+ attr_reader :name,
7
+ :collection_of_options
7
8
 
8
9
  def initialize(
9
10
  name,
@@ -49,6 +50,26 @@ module Servactory
49
50
  def options_for_checks
50
51
  @collection_of_options.options_for_checks
51
52
  end
53
+
54
+ def system_name
55
+ self.class.name.demodulize.downcase.to_sym
56
+ end
57
+
58
+ def i18n_name
59
+ system_name.to_s.pluralize
60
+ end
61
+
62
+ def input?
63
+ false
64
+ end
65
+
66
+ def internal?
67
+ true
68
+ end
69
+
70
+ def output?
71
+ false
72
+ end
52
73
  end
53
74
  end
54
75
  end
@@ -33,22 +33,22 @@ module Servactory
33
33
  ########################################################################
34
34
 
35
35
  def register # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
36
- # Check Class: Servactory::Inputs::Validations::Required
36
+ # Validation Class: Servactory::Inputs::Validations::Required
37
37
  register_required_option if @features.fetch(:required)
38
38
 
39
- # Check Class: Servactory::Inputs::Validations::Type
39
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Type
40
40
  register_types_option if @features.fetch(:types)
41
41
  register_default_option if @features.fetch(:default)
42
42
  register_collection_option if @features.fetch(:collection)
43
43
  register_hash_option if @features.fetch(:hash)
44
44
 
45
- # Check Class: Servactory::Inputs::Validations::Inclusion
45
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Inclusion
46
46
  register_inclusion_option if @features.fetch(:inclusion)
47
47
 
48
- # Check Class: Servactory::Inputs::Validations::Must
48
+ # Validation Class: Servactory::Maintenance::Attributes::Validations::Must
49
49
  register_must_option if @features.fetch(:must)
50
50
 
51
- # Check Class: nil
51
+ # Validation Class: nil
52
52
  register_prepare_option if @features.fetch(:prepare)
53
53
 
54
54
  self
@@ -87,7 +87,7 @@ module Servactory
87
87
  collection << Servactory::Maintenance::Attributes::Option.new(
88
88
  name: :types,
89
89
  attribute: @attribute,
90
- validation_class: Servactory::Inputs::Validations::Type,
90
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
91
91
  original_value: Array(@options.fetch(:type)),
92
92
  need_for_checks: true,
93
93
  body_fallback: nil,
@@ -99,7 +99,7 @@ module Servactory
99
99
  collection << Servactory::Maintenance::Attributes::Option.new(
100
100
  name: :default,
101
101
  attribute: @attribute,
102
- validation_class: Servactory::Inputs::Validations::Type,
102
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
103
103
  define_methods: [
104
104
  Servactory::Maintenance::Attributes::DefineMethod.new(
105
105
  name: :default_value_present?,
@@ -117,7 +117,7 @@ module Servactory
117
117
  collection << Servactory::Maintenance::Attributes::Option.new(
118
118
  name: :consists_of,
119
119
  attribute: @attribute,
120
- validation_class: Servactory::Inputs::Validations::Type,
120
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
121
121
  define_methods: [
122
122
  Servactory::Maintenance::Attributes::DefineMethod.new(
123
123
  name: :collection_mode?,
@@ -143,7 +143,7 @@ module Servactory
143
143
  collection << Servactory::Maintenance::Attributes::Option.new(
144
144
  name: :schema,
145
145
  attribute: @attribute,
146
- validation_class: Servactory::Inputs::Validations::Type,
146
+ validation_class: Servactory::Maintenance::Attributes::Validations::Type,
147
147
  define_methods: [
148
148
  Servactory::Maintenance::Attributes::DefineMethod.new(
149
149
  name: :hash_mode?,
@@ -166,7 +166,7 @@ module Servactory
166
166
  collection << Servactory::Maintenance::Attributes::Option.new(
167
167
  name: :inclusion,
168
168
  attribute: @attribute,
169
- validation_class: Servactory::Inputs::Validations::Inclusion,
169
+ validation_class: Servactory::Maintenance::Attributes::Validations::Inclusion,
170
170
  define_methods: [
171
171
  Servactory::Maintenance::Attributes::DefineMethod.new(
172
172
  name: :inclusion_present?,
@@ -184,7 +184,7 @@ module Servactory
184
184
  collection << Servactory::Maintenance::Attributes::Option.new(
185
185
  name: :must,
186
186
  attribute: @attribute,
187
- validation_class: Servactory::Inputs::Validations::Must,
187
+ validation_class: Servactory::Maintenance::Attributes::Validations::Must,
188
188
  define_methods: [
189
189
  Servactory::Maintenance::Attributes::DefineMethod.new(
190
190
  name: :must_present?,
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Tools
7
+ class CheckErrors
8
+ extend Forwardable
9
+ def_delegators :@collection, :merge, :reject, :first, :empty?
10
+
11
+ def initialize(collection = Set.new)
12
+ @collection = collection
13
+ end
14
+
15
+ def not_blank
16
+ CheckErrors.new(reject(&:blank?))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Tools
7
+ class Validation
8
+ def self.validate!(...)
9
+ new(...).validate!
10
+ end
11
+
12
+ def initialize(context:, attribute:, value:)
13
+ @context = context
14
+ @attribute = attribute
15
+ @value = value
16
+ end
17
+
18
+ def validate!
19
+ process
20
+
21
+ raise_errors
22
+ end
23
+
24
+ private
25
+
26
+ def process
27
+ @attribute.options_for_checks.each do |check_key, check_options|
28
+ process_option(check_key, check_options)
29
+ end
30
+ end
31
+
32
+ def process_option(check_key, check_options)
33
+ validation_classes.each do |validation_class|
34
+ errors_from_checks = process_validation_class(
35
+ validation_class: validation_class,
36
+ check_key: check_key,
37
+ check_options: check_options
38
+ )
39
+
40
+ errors.merge(errors_from_checks.to_a)
41
+ end
42
+ end
43
+
44
+ def process_validation_class(
45
+ validation_class:,
46
+ check_key:,
47
+ check_options:
48
+ )
49
+ validation_class.check(
50
+ context: @context,
51
+ attribute: @attribute,
52
+ value: @value,
53
+ check_key: check_key,
54
+ check_options: check_options
55
+ )
56
+ end
57
+
58
+ ########################################################################
59
+
60
+ def validation_classes
61
+ @attribute.collection_of_options.validation_classes
62
+ end
63
+
64
+ ########################################################################
65
+
66
+ def errors
67
+ @errors ||= Servactory::Maintenance::Attributes::Tools::CheckErrors.new
68
+ end
69
+
70
+ def raise_errors
71
+ return if (tmp_errors = errors.not_blank).empty?
72
+
73
+ raise @context.class.config
74
+ .public_send(:"#{@attribute.system_name}_error_class")
75
+ .new(message: tmp_errors.first)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Base
8
+ private
9
+
10
+ def add_error(message:, **attributes)
11
+ message = message.call(**attributes) if message.is_a?(Proc)
12
+
13
+ errors << message
14
+ end
15
+
16
+ def errors
17
+ @errors ||= Errors.new
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Errors
8
+ extend Forwardable
9
+ def_delegators :@collection, :<<, :to_a
10
+
11
+ def initialize(*)
12
+ @collection = Set.new
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module Maintenance
5
+ module Attributes
6
+ module Validations
7
+ class Inclusion < Base
8
+ DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
9
+ I18n.t(
10
+ "servactory.#{input.i18n_name}.validations.inclusion.default_error",
11
+ service_class_name: service_class_name,
12
+ "#{input.system_name}_name": input.name,
13
+ input_inclusion: input.inclusion[:in],
14
+ value: value
15
+ )
16
+ end
17
+
18
+ private_constant :DEFAULT_MESSAGE
19
+
20
+ def self.check(context:, attribute:, value:, check_key:, **)
21
+ return unless should_be_checked_for?(attribute, value, check_key)
22
+
23
+ new(context: context, attribute: attribute, value: value).check
24
+ end
25
+
26
+ # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
27
+ def self.should_be_checked_for?(attribute, value, check_key)
28
+ check_key == :inclusion && (
29
+ (
30
+ attribute.input? && (
31
+ attribute.required? || (
32
+ attribute.optional? && !attribute.default.nil?
33
+ ) || (
34
+ attribute.optional? && !value.nil?
35
+ )
36
+ )
37
+ ) || attribute.internal? || attribute.output?
38
+ )
39
+ end
40
+ # rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41
+
42
+ ##########################################################################
43
+
44
+ def initialize(context:, attribute:, value:)
45
+ super()
46
+
47
+ @context = context
48
+ @attribute = attribute
49
+ @value = value
50
+ end
51
+
52
+ def check
53
+ inclusion_in, message = @attribute.inclusion.values_at(:in, :message)
54
+
55
+ return if inclusion_in.nil?
56
+ return if inclusion_in.include?(@value)
57
+
58
+ add_error_with(message)
59
+ end
60
+
61
+ private
62
+
63
+ def add_error_with(message)
64
+ add_error(
65
+ message: message.presence || DEFAULT_MESSAGE,
66
+ service_class_name: @context.class.name,
67
+ "#{@attribute.system_name}": @attribute,
68
+ value: @value
69
+ )
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end