servactory 1.9.6 → 2.0.0.rc3
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/config/locales/en.yml +19 -4
- data/config/locales/ru.yml +21 -6
- data/lib/servactory/configuration/dsl.rb +2 -0
- data/lib/servactory/configuration/factory.rb +8 -0
- data/lib/servactory/configuration/setup.rb +20 -4
- data/lib/servactory/context/workspace/inputs.rb +41 -7
- data/lib/servactory/context/workspace/outputs.rb +1 -1
- data/lib/servactory/context/workspace.rb +0 -1
- data/lib/servactory/dsl.rb +17 -0
- data/lib/servactory/inputs/collection.rb +1 -1
- data/lib/servactory/inputs/dsl.rb +2 -0
- data/lib/servactory/inputs/input.rb +101 -82
- data/lib/servactory/inputs/tools/distributor.rb +24 -0
- data/lib/servactory/inputs/tools/rules.rb +3 -3
- data/lib/servactory/inputs/tools/{find_unnecessary.rb → unnecessary.rb} +4 -4
- data/lib/servactory/inputs/tools/validation.rb +5 -7
- data/lib/servactory/inputs/validations/base.rb +1 -1
- data/lib/servactory/inputs/validations/inclusion.rb +15 -10
- data/lib/servactory/inputs/validations/must.rb +26 -17
- data/lib/servactory/inputs/validations/required.rb +16 -11
- data/lib/servactory/inputs/validations/type.rb +19 -69
- data/lib/servactory/inputs/workspace.rb +6 -3
- data/lib/servactory/internals/dsl.rb +6 -1
- data/lib/servactory/internals/internal.rb +82 -14
- data/lib/servactory/internals/validations/base.rb +1 -1
- data/lib/servactory/internals/validations/type.rb +6 -43
- data/lib/servactory/maintenance/attributes/define_conflict.rb +15 -0
- data/lib/servactory/maintenance/attributes/define_method.rb +17 -0
- data/lib/servactory/maintenance/attributes/option.rb +115 -0
- data/lib/servactory/maintenance/attributes/option_helper.rb +17 -0
- data/lib/servactory/maintenance/attributes/option_helpers_collection.rb +20 -0
- data/lib/servactory/maintenance/attributes/options_collection.rb +48 -0
- data/lib/servactory/maintenance/collection_mode/class_names_collection.rb +16 -0
- data/lib/servactory/maintenance/hash_mode/class_names_collection.rb +16 -0
- data/lib/servactory/maintenance/validations/collection.rb +66 -0
- data/lib/servactory/maintenance/validations/object_schema.rb +116 -0
- data/lib/servactory/maintenance/validations/types.rb +164 -0
- data/lib/servactory/methods/workspace.rb +2 -0
- data/lib/servactory/outputs/dsl.rb +6 -1
- data/lib/servactory/outputs/output.rb +80 -19
- data/lib/servactory/outputs/validations/base.rb +1 -1
- data/lib/servactory/outputs/validations/type.rb +6 -45
- data/lib/servactory/version.rb +5 -4
- metadata +17 -11
- data/lib/servactory/inputs/define_input_conflict.rb +0 -13
- data/lib/servactory/inputs/define_input_method.rb +0 -15
- data/lib/servactory/inputs/option.rb +0 -98
- data/lib/servactory/inputs/option_helper.rb +0 -15
- data/lib/servactory/inputs/option_helpers_collection.rb +0 -18
- data/lib/servactory/inputs/options_collection.rb +0 -46
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Inputs
|
5
|
+
module Tools
|
6
|
+
class Distributor
|
7
|
+
def self.assign!(...)
|
8
|
+
new(...).assign!
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(incoming_arguments, collection_of_inputs)
|
12
|
+
@incoming_arguments = incoming_arguments
|
13
|
+
@collection_of_inputs = collection_of_inputs
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign!
|
17
|
+
@collection_of_inputs.map do |input|
|
18
|
+
input.value = @incoming_arguments.fetch(input.name, nil)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -4,8 +4,8 @@ module Servactory
|
|
4
4
|
module Inputs
|
5
5
|
module Tools
|
6
6
|
class Rules
|
7
|
-
def self.
|
8
|
-
new(...).
|
7
|
+
def self.check!(...)
|
8
|
+
new(...).check!
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(context, collection_of_inputs)
|
@@ -13,7 +13,7 @@ module Servactory
|
|
13
13
|
@collection_of_inputs = collection_of_inputs
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def check!
|
17
17
|
@collection_of_inputs.each do |input|
|
18
18
|
check_for!(input)
|
19
19
|
end
|
@@ -3,9 +3,9 @@
|
|
3
3
|
module Servactory
|
4
4
|
module Inputs
|
5
5
|
module Tools
|
6
|
-
class
|
7
|
-
def self.
|
8
|
-
new(...).
|
6
|
+
class Unnecessary
|
7
|
+
def self.find!(...)
|
8
|
+
new(...).find!
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(context, incoming_arguments, collection_of_inputs)
|
@@ -14,7 +14,7 @@ module Servactory
|
|
14
14
|
@collection_of_inputs = collection_of_inputs
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def find!
|
18
18
|
return if unnecessary_attributes.empty?
|
19
19
|
|
20
20
|
message_text = I18n.t(
|
@@ -8,9 +8,8 @@ module Servactory
|
|
8
8
|
new(...).validate!
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(context,
|
11
|
+
def initialize(context, collection_of_inputs)
|
12
12
|
@context = context
|
13
|
-
@incoming_arguments = incoming_arguments
|
14
13
|
@collection_of_inputs = collection_of_inputs
|
15
14
|
end
|
16
15
|
|
@@ -32,8 +31,8 @@ module Servactory
|
|
32
31
|
|
33
32
|
def process_option(check_key, check_options, input:)
|
34
33
|
validation_classes_from(input).each do |validation_class|
|
35
|
-
errors_from_checks =
|
36
|
-
validation_class,
|
34
|
+
errors_from_checks = process_validation_class(
|
35
|
+
validation_class: validation_class,
|
37
36
|
input: input,
|
38
37
|
check_key: check_key,
|
39
38
|
check_options: check_options
|
@@ -43,8 +42,8 @@ module Servactory
|
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
|
-
def
|
47
|
-
validation_class
|
45
|
+
def process_validation_class(
|
46
|
+
validation_class:,
|
48
47
|
input:,
|
49
48
|
check_key:,
|
50
49
|
check_options:
|
@@ -52,7 +51,6 @@ module Servactory
|
|
52
51
|
validation_class.check(
|
53
52
|
context: @context,
|
54
53
|
input: input,
|
55
|
-
value: @incoming_arguments.fetch(input.name, nil),
|
56
54
|
check_key: check_key,
|
57
55
|
check_options: check_options
|
58
56
|
)
|
@@ -16,43 +16,48 @@ module Servactory
|
|
16
16
|
|
17
17
|
private_constant :DEFAULT_MESSAGE
|
18
18
|
|
19
|
-
def self.check(context:, input:,
|
20
|
-
return unless should_be_checked_for?(input,
|
19
|
+
def self.check(context:, input:, check_key:, **)
|
20
|
+
return unless should_be_checked_for?(input, check_key)
|
21
21
|
|
22
|
-
new(context: context, input: input
|
22
|
+
new(context: context, input: input).check
|
23
23
|
end
|
24
24
|
|
25
|
-
def self.should_be_checked_for?(input,
|
25
|
+
def self.should_be_checked_for?(input, check_key)
|
26
26
|
check_key == :inclusion && (
|
27
27
|
input.required? || (
|
28
28
|
input.optional? && !input.default.nil?
|
29
29
|
) || (
|
30
|
-
input.optional? && !value.nil?
|
30
|
+
input.optional? && !input.value.nil?
|
31
31
|
)
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
35
35
|
##########################################################################
|
36
36
|
|
37
|
-
def initialize(context:, input
|
37
|
+
def initialize(context:, input:)
|
38
38
|
super()
|
39
39
|
|
40
40
|
@context = context
|
41
41
|
@input = input
|
42
|
-
@value = value
|
43
42
|
end
|
44
43
|
|
45
44
|
def check
|
46
45
|
inclusion_in, message = @input.inclusion.values_at(:in, :message)
|
47
46
|
|
48
47
|
return if inclusion_in.nil?
|
49
|
-
return if inclusion_in.include?(@value)
|
48
|
+
return if inclusion_in.include?(@input.value)
|
50
49
|
|
50
|
+
add_error_with(message)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def add_error_with(message)
|
51
56
|
add_error(
|
52
|
-
message.presence || DEFAULT_MESSAGE,
|
57
|
+
message: message.presence || DEFAULT_MESSAGE,
|
53
58
|
service_class_name: @context.class.name,
|
54
59
|
input: @input,
|
55
|
-
value: @value
|
60
|
+
value: @input.value
|
56
61
|
)
|
57
62
|
end
|
58
63
|
end
|
@@ -27,10 +27,10 @@ module Servactory
|
|
27
27
|
|
28
28
|
private_constant :DEFAULT_MESSAGE, :SYNTAX_ERROR_MESSAGE
|
29
29
|
|
30
|
-
def self.check(context:, input:,
|
30
|
+
def self.check(context:, input:, check_key:, check_options:)
|
31
31
|
return unless should_be_checked_for?(input, check_key)
|
32
32
|
|
33
|
-
new(context: context, input: input,
|
33
|
+
new(context: context, input: input, check_options: check_options).check
|
34
34
|
end
|
35
35
|
|
36
36
|
def self.should_be_checked_for?(input, check_key)
|
@@ -39,28 +39,21 @@ module Servactory
|
|
39
39
|
|
40
40
|
##########################################################################
|
41
41
|
|
42
|
-
def initialize(context:, input:,
|
42
|
+
def initialize(context:, input:, check_options:)
|
43
43
|
super()
|
44
44
|
|
45
45
|
@context = context
|
46
46
|
@input = input
|
47
|
-
@value = value
|
48
47
|
@check_options = check_options
|
49
48
|
end
|
50
49
|
|
51
|
-
def check
|
50
|
+
def check
|
52
51
|
@check_options.each do |code, options|
|
53
52
|
message = call_or_fetch_message_from(code, options)
|
54
53
|
|
55
54
|
next if message.blank?
|
56
55
|
|
57
|
-
|
58
|
-
message,
|
59
|
-
service_class_name: @context.class.name,
|
60
|
-
input: @input,
|
61
|
-
value: @value,
|
62
|
-
code: code
|
63
|
-
)
|
56
|
+
add_error_with(message, code)
|
64
57
|
end
|
65
58
|
|
66
59
|
errors
|
@@ -68,20 +61,36 @@ module Servactory
|
|
68
61
|
|
69
62
|
private
|
70
63
|
|
71
|
-
def call_or_fetch_message_from(code, options)
|
64
|
+
def call_or_fetch_message_from(code, options)
|
72
65
|
check, message = options.values_at(:is, :message)
|
73
66
|
|
74
|
-
return if check.call(value: @value)
|
67
|
+
return if check.call(value: @input.value)
|
75
68
|
|
76
69
|
message.presence || DEFAULT_MESSAGE
|
77
70
|
rescue StandardError => e
|
71
|
+
add_syntax_error_with(SYNTAX_ERROR_MESSAGE, code, e.message)
|
72
|
+
end
|
73
|
+
|
74
|
+
########################################################################
|
75
|
+
|
76
|
+
def add_error_with(message, code)
|
78
77
|
add_error(
|
79
|
-
|
78
|
+
message: message,
|
80
79
|
service_class_name: @context.class.name,
|
81
80
|
input: @input,
|
82
|
-
value: @value,
|
81
|
+
value: @input.value,
|
82
|
+
code: code
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_syntax_error_with(message, code, exception_message)
|
87
|
+
add_error(
|
88
|
+
message: message,
|
89
|
+
service_class_name: @context.class.name,
|
90
|
+
input: @input,
|
91
|
+
value: @input.value,
|
83
92
|
code: code,
|
84
|
-
exception_message:
|
93
|
+
exception_message: exception_message
|
85
94
|
)
|
86
95
|
end
|
87
96
|
end
|
@@ -6,7 +6,7 @@ module Servactory
|
|
6
6
|
class Required < Base
|
7
7
|
DEFAULT_MESSAGE = lambda do |service_class_name:, input:, value:|
|
8
8
|
i18n_key = "servactory.inputs.checks.required.default_error."
|
9
|
-
i18n_key += input.
|
9
|
+
i18n_key += input.collection_mode? && value.present? ? "for_collection" : "default"
|
10
10
|
|
11
11
|
I18n.t(
|
12
12
|
i18n_key,
|
@@ -17,10 +17,10 @@ module Servactory
|
|
17
17
|
|
18
18
|
private_constant :DEFAULT_MESSAGE
|
19
19
|
|
20
|
-
def self.check(context:, input:,
|
20
|
+
def self.check(context:, input:, check_key:, **)
|
21
21
|
return unless should_be_checked_for?(input, check_key)
|
22
22
|
|
23
|
-
new(context: context, input: input
|
23
|
+
new(context: context, input: input).check
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.should_be_checked_for?(input, check_key)
|
@@ -29,28 +29,33 @@ module Servactory
|
|
29
29
|
|
30
30
|
##########################################################################
|
31
31
|
|
32
|
-
def initialize(context:, input
|
32
|
+
def initialize(context:, input:)
|
33
33
|
super()
|
34
34
|
|
35
35
|
@context = context
|
36
36
|
@input = input
|
37
|
-
@value = value
|
38
37
|
end
|
39
38
|
|
40
|
-
def check
|
41
|
-
if @input.
|
42
|
-
return if @value.respond_to?(:all?) && @value.all?(&:present?)
|
43
|
-
elsif Servactory::Utils.value_present?(@value)
|
39
|
+
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)
|
44
43
|
return
|
45
44
|
end
|
46
45
|
|
47
46
|
_, message = @input.required.values_at(:is, :message)
|
48
47
|
|
48
|
+
add_error_with(message)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def add_error_with(message)
|
49
54
|
add_error(
|
50
|
-
message.presence || DEFAULT_MESSAGE,
|
55
|
+
message: message.presence || DEFAULT_MESSAGE,
|
51
56
|
service_class_name: @context.class.name,
|
52
57
|
input: @input,
|
53
|
-
value: @value
|
58
|
+
value: @input.value
|
54
59
|
)
|
55
60
|
end
|
56
61
|
end
|
@@ -4,97 +4,47 @@ module Servactory
|
|
4
4
|
module Inputs
|
5
5
|
module Validations
|
6
6
|
class Type < Base
|
7
|
-
|
8
|
-
|
9
|
-
array_message = input.array[:message]
|
7
|
+
def self.check(context:, input:, check_key:, **)
|
8
|
+
return unless should_be_checked_for?(input, check_key)
|
10
9
|
|
11
|
-
|
12
|
-
array_message.call(input: input, expected_type: expected_type)
|
13
|
-
elsif array_message.is_a?(String) && array_message.present?
|
14
|
-
array_message
|
15
|
-
else
|
16
|
-
I18n.t(
|
17
|
-
"servactory.inputs.checks.type.default_error.for_array",
|
18
|
-
service_class_name: service_class_name,
|
19
|
-
input_name: input.name,
|
20
|
-
expected_type: expected_type,
|
21
|
-
given_type: given_type
|
22
|
-
)
|
23
|
-
end
|
24
|
-
else
|
25
|
-
I18n.t(
|
26
|
-
"servactory.inputs.checks.type.default_error.default",
|
27
|
-
service_class_name: service_class_name,
|
28
|
-
input_name: input.name,
|
29
|
-
expected_type: expected_type,
|
30
|
-
given_type: given_type
|
31
|
-
)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
private_constant :DEFAULT_MESSAGE
|
36
|
-
|
37
|
-
def self.check(context:, input:, value:, check_key:, check_options:)
|
38
|
-
return unless should_be_checked_for?(input, value, check_key)
|
39
|
-
|
40
|
-
new(context: context, input: input, value: value, types: check_options).check
|
10
|
+
new(context: context, input: input).check
|
41
11
|
end
|
42
12
|
|
43
|
-
def self.should_be_checked_for?(input,
|
13
|
+
def self.should_be_checked_for?(input, check_key)
|
44
14
|
check_key == :types && (
|
45
15
|
input.required? || (
|
46
16
|
input.optional? && !input.default.nil?
|
47
17
|
) || (
|
48
|
-
input.optional? && !value.nil?
|
18
|
+
input.optional? && !input.value.nil?
|
49
19
|
)
|
50
20
|
)
|
51
21
|
end
|
52
22
|
|
53
|
-
|
54
|
-
|
55
|
-
def initialize(context:, input:, value:, types:)
|
23
|
+
def initialize(context:, input:)
|
56
24
|
super()
|
57
25
|
|
58
26
|
@context = context
|
59
27
|
@input = input
|
60
|
-
@value = value
|
61
|
-
@types = types
|
62
28
|
end
|
63
29
|
|
64
|
-
def check
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
add_error(
|
75
|
-
DEFAULT_MESSAGE,
|
76
|
-
service_class_name: @context.class.name,
|
77
|
-
input: @input,
|
78
|
-
expected_type: prepared_types.join(", "),
|
79
|
-
given_type: prepared_value.class.name
|
30
|
+
def check
|
31
|
+
Servactory::Maintenance::Validations::Types.validate!(
|
32
|
+
context: @context,
|
33
|
+
attribute: @input,
|
34
|
+
types: @input.types,
|
35
|
+
value: prepared_value,
|
36
|
+
error_callback: ->(**args) { add_error(**args) }
|
80
37
|
)
|
81
38
|
end
|
82
39
|
|
83
|
-
|
84
|
-
|
85
|
-
def prepared_types
|
86
|
-
@prepared_types ||=
|
87
|
-
@types.map do |type|
|
88
|
-
if type.is_a?(String)
|
89
|
-
Object.const_get(type)
|
90
|
-
else
|
91
|
-
type
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
40
|
+
private
|
95
41
|
|
96
42
|
def prepared_value
|
97
|
-
@prepared_value ||= @input.optional? && !@input.default.nil? && @value.blank?
|
43
|
+
@prepared_value ||= if @input.optional? && !@input.default.nil? && @input.value.blank?
|
44
|
+
@input.default
|
45
|
+
else
|
46
|
+
@input.value
|
47
|
+
end
|
98
48
|
end
|
99
49
|
end
|
100
50
|
end
|
@@ -3,12 +3,15 @@
|
|
3
3
|
module Servactory
|
4
4
|
module Inputs
|
5
5
|
module Workspace
|
6
|
+
private
|
7
|
+
|
6
8
|
def call!(incoming_arguments:, collection_of_inputs:, **)
|
7
9
|
super
|
8
10
|
|
9
|
-
Tools::
|
10
|
-
Tools::
|
11
|
-
|
11
|
+
Tools::Unnecessary.find!(self, incoming_arguments, collection_of_inputs)
|
12
|
+
Tools::Distributor.assign!(incoming_arguments, collection_of_inputs)
|
13
|
+
Tools::Rules.check!(self, collection_of_inputs)
|
14
|
+
Tools::Validation.validate!(self, collection_of_inputs)
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
@@ -17,7 +17,12 @@ module Servactory
|
|
17
17
|
private
|
18
18
|
|
19
19
|
def internal(name, **options)
|
20
|
-
collection_of_internals << Internal.new(
|
20
|
+
collection_of_internals << Internal.new(
|
21
|
+
name,
|
22
|
+
collection_mode_class_names: config.collection_mode_class_names,
|
23
|
+
hash_mode_class_names: config.hash_mode_class_names,
|
24
|
+
**options
|
25
|
+
)
|
21
26
|
end
|
22
27
|
|
23
28
|
def collection_of_internals
|
@@ -4,29 +4,97 @@ module Servactory
|
|
4
4
|
module Internals
|
5
5
|
class Internal
|
6
6
|
attr_reader :name,
|
7
|
-
:
|
8
|
-
:
|
7
|
+
:collection_mode_class_names,
|
8
|
+
:hash_mode_class_names
|
9
9
|
|
10
|
-
def initialize(
|
10
|
+
def initialize(
|
11
|
+
name,
|
12
|
+
type:,
|
13
|
+
collection_mode_class_names:,
|
14
|
+
hash_mode_class_names:,
|
15
|
+
**options
|
16
|
+
)
|
11
17
|
@name = name
|
12
|
-
@
|
18
|
+
@collection_mode_class_names = collection_mode_class_names
|
19
|
+
@hash_mode_class_names = hash_mode_class_names
|
13
20
|
|
14
|
-
|
21
|
+
add_basic_options_with(type: type, options: options)
|
15
22
|
end
|
16
23
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
24
|
+
def method_missing(name, *args, &block)
|
25
|
+
option = collection_of_options.find_by(name: name)
|
26
|
+
|
27
|
+
return super if option.nil?
|
28
|
+
|
29
|
+
option.body
|
30
|
+
end
|
31
|
+
|
32
|
+
def respond_to_missing?(name, *)
|
33
|
+
collection_of_options.names.include?(name) || super
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_basic_options_with(type:, options:)
|
37
|
+
# Check Class: Servactory::Internals::Validations::Type
|
38
|
+
add_types_option_with(type)
|
39
|
+
add_collection_option_with(type, options)
|
40
|
+
add_object_option_with(type, options)
|
22
41
|
end
|
23
42
|
|
24
|
-
def
|
25
|
-
|
43
|
+
def add_types_option_with(type)
|
44
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
45
|
+
name: :types,
|
46
|
+
attribute: self,
|
47
|
+
validation_class: Servactory::Internals::Validations::Type,
|
48
|
+
original_value: Array(type),
|
49
|
+
need_for_checks: true,
|
50
|
+
body_fallback: nil,
|
51
|
+
with_advanced_mode: false
|
52
|
+
)
|
26
53
|
end
|
27
54
|
|
28
|
-
def
|
29
|
-
|
55
|
+
def add_collection_option_with(type, options) # rubocop:disable Metrics/MethodLength
|
56
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
57
|
+
name: :consists_of,
|
58
|
+
attribute: self,
|
59
|
+
validation_class: Servactory::Internals::Validations::Type,
|
60
|
+
define_methods: [
|
61
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
62
|
+
name: :collection_mode?,
|
63
|
+
content: ->(**) { collection_mode_class_names.include?(type) }
|
64
|
+
)
|
65
|
+
],
|
66
|
+
need_for_checks: false,
|
67
|
+
body_key: :type,
|
68
|
+
body_value: String,
|
69
|
+
body_fallback: String,
|
70
|
+
**options
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def add_object_option_with(type, options) # rubocop:disable Metrics/MethodLength
|
75
|
+
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
76
|
+
name: :schema,
|
77
|
+
attribute: self,
|
78
|
+
validation_class: Servactory::Inputs::Validations::Type,
|
79
|
+
define_methods: [
|
80
|
+
Servactory::Maintenance::Attributes::DefineMethod.new(
|
81
|
+
name: :hash_mode?,
|
82
|
+
content: ->(**) { hash_mode_class_names.include?(type) }
|
83
|
+
)
|
84
|
+
],
|
85
|
+
need_for_checks: false,
|
86
|
+
body_fallback: {},
|
87
|
+
with_advanced_mode: false,
|
88
|
+
**options
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
def collection_of_options
|
93
|
+
@collection_of_options ||= Servactory::Maintenance::Attributes::OptionsCollection.new
|
94
|
+
end
|
95
|
+
|
96
|
+
def options_for_checks
|
97
|
+
collection_of_options.options_for_checks
|
30
98
|
end
|
31
99
|
end
|
32
100
|
end
|
@@ -6,7 +6,7 @@ module Servactory
|
|
6
6
|
class Base
|
7
7
|
protected
|
8
8
|
|
9
|
-
def raise_error_with(message
|
9
|
+
def raise_error_with(message:, **attributes)
|
10
10
|
message = message.call(**attributes) if message.is_a?(Proc)
|
11
11
|
|
12
12
|
raise @context.class.config.internal_error_class.new(message: message)
|
@@ -4,24 +4,10 @@ module Servactory
|
|
4
4
|
module Internals
|
5
5
|
module Validations
|
6
6
|
class Type < Base
|
7
|
-
DEFAULT_MESSAGE = lambda do |service_class_name:, internal:, expected_type:, given_type:|
|
8
|
-
I18n.t(
|
9
|
-
"servactory.internals.checks.type.default_error",
|
10
|
-
service_class_name: service_class_name,
|
11
|
-
internal_name: internal.name,
|
12
|
-
expected_type: expected_type,
|
13
|
-
given_type: given_type
|
14
|
-
)
|
15
|
-
end
|
16
|
-
|
17
|
-
private_constant :DEFAULT_MESSAGE
|
18
|
-
|
19
7
|
def self.validate!(...)
|
20
8
|
new(...).validate!
|
21
9
|
end
|
22
10
|
|
23
|
-
##########################################################################
|
24
|
-
|
25
11
|
def initialize(context:, internal:, value:)
|
26
12
|
super()
|
27
13
|
|
@@ -31,37 +17,14 @@ module Servactory
|
|
31
17
|
end
|
32
18
|
|
33
19
|
def validate!
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
service_class_name: @context.class.name,
|
41
|
-
internal: @internal,
|
42
|
-
expected_type: prepared_types.join(", "),
|
43
|
-
given_type: @value.class.name
|
20
|
+
Servactory::Maintenance::Validations::Types.validate!(
|
21
|
+
context: @context,
|
22
|
+
attribute: @internal,
|
23
|
+
types: @internal.types,
|
24
|
+
value: @value,
|
25
|
+
error_callback: ->(**args) { raise_error_with(**args) }
|
44
26
|
)
|
45
27
|
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def should_be_checked?
|
50
|
-
@internal.required? || (
|
51
|
-
@internal.optional? && !@value.nil?
|
52
|
-
)
|
53
|
-
end
|
54
|
-
|
55
|
-
def prepared_types
|
56
|
-
@prepared_types ||=
|
57
|
-
Array(@internal.types).map do |type|
|
58
|
-
if type.is_a?(String)
|
59
|
-
Object.const_get(type)
|
60
|
-
else
|
61
|
-
type
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
28
|
end
|
66
29
|
end
|
67
30
|
end
|