servactory 2.0.0.rc2 → 2.0.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/en.yml +2 -1
- data/config/locales/ru.yml +2 -1
- data/lib/servactory/{methods/method.rb → actions/action.rb} +2 -2
- data/lib/servactory/{methods/aliases_for_make → actions/aliases}/collection.rb +2 -2
- data/lib/servactory/{methods/method_collection.rb → actions/collection.rb} +3 -3
- data/lib/servactory/{methods → actions}/dsl.rb +8 -8
- data/lib/servactory/{methods/shortcuts_for_make → actions/shortcuts}/collection.rb +2 -2
- data/lib/servactory/actions/stages/collection.rb +20 -0
- data/lib/servactory/actions/stages/stage.rb +30 -0
- data/lib/servactory/{methods → actions}/tools/runner.rb +1 -1
- data/lib/servactory/{methods → actions}/workspace.rb +2 -2
- data/lib/servactory/base.rb +1 -1
- data/lib/servactory/configuration/dsl.rb +2 -2
- data/lib/servactory/configuration/factory.rb +5 -5
- data/lib/servactory/configuration/setup.rb +4 -4
- data/lib/servactory/context/workspace/inputs.rb +7 -5
- data/lib/servactory/dsl.rb +1 -1
- data/lib/servactory/inputs/input.rb +3 -3
- data/lib/servactory/inputs/validations/base.rb +1 -1
- data/lib/servactory/inputs/validations/inclusion.rb +1 -1
- data/lib/servactory/inputs/validations/must.rb +2 -2
- data/lib/servactory/inputs/validations/required.rb +1 -1
- data/lib/servactory/inputs/validations/type.rb +13 -114
- data/lib/servactory/internals/internal.rb +3 -3
- data/lib/servactory/internals/validations/base.rb +1 -1
- data/lib/servactory/internals/validations/type.rb +6 -124
- data/lib/servactory/maintenance/attributes/option.rb +13 -7
- data/lib/servactory/maintenance/validations/collection.rb +66 -0
- data/lib/servactory/maintenance/validations/object_schema.rb +15 -18
- data/lib/servactory/maintenance/validations/types.rb +181 -0
- data/lib/servactory/outputs/output.rb +3 -3
- data/lib/servactory/outputs/validations/base.rb +1 -1
- data/lib/servactory/outputs/validations/type.rb +6 -124
- data/lib/servactory/version.rb +1 -1
- metadata +21 -19
- data/lib/servactory/methods/stage.rb +0 -28
- data/lib/servactory/methods/stage_collection.rb +0 -18
@@ -4,65 +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:, value:, key_name:, expected_type:, given_type:| # rubocop:disable Metrics/BlockLength
|
8
|
-
if internal.collection_mode?
|
9
|
-
collection_message = internal.consists_of.fetch(:message)
|
10
|
-
|
11
|
-
if collection_message.is_a?(Proc)
|
12
|
-
collection_message.call(internal: internal, expected_type: expected_type)
|
13
|
-
elsif collection_message.is_a?(String) && collection_message.present?
|
14
|
-
collection_message
|
15
|
-
elsif value.is_a?(internal.types.fetch(0, Array))
|
16
|
-
I18n.t(
|
17
|
-
"servactory.internals.checks.type.default_error.for_collection.wrong_element_type",
|
18
|
-
service_class_name: service_class_name,
|
19
|
-
internal_name: internal.name,
|
20
|
-
expected_type: expected_type,
|
21
|
-
given_type: given_type
|
22
|
-
)
|
23
|
-
else
|
24
|
-
I18n.t(
|
25
|
-
"servactory.internals.checks.type.default_error.for_collection.wrong_type",
|
26
|
-
service_class_name: service_class_name,
|
27
|
-
internal_name: internal.name,
|
28
|
-
expected_type: internal.types.fetch(0, Array),
|
29
|
-
given_type: value.class.name
|
30
|
-
)
|
31
|
-
end
|
32
|
-
elsif internal.hash_mode? && key_name.present?
|
33
|
-
I18n.t(
|
34
|
-
"servactory.internals.checks.type.default_error.for_hash.wrong_element_type",
|
35
|
-
service_class_name: service_class_name,
|
36
|
-
internal_name: internal.name,
|
37
|
-
key_name: key_name,
|
38
|
-
expected_type: expected_type,
|
39
|
-
given_type: given_type
|
40
|
-
)
|
41
|
-
else
|
42
|
-
I18n.t(
|
43
|
-
"servactory.internals.checks.type.default_error.default",
|
44
|
-
service_class_name: service_class_name,
|
45
|
-
internal_name: internal.name,
|
46
|
-
expected_type: expected_type,
|
47
|
-
given_type: given_type
|
48
|
-
)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
private_constant :DEFAULT_MESSAGE
|
53
|
-
|
54
7
|
def self.validate!(...)
|
55
|
-
return unless should_be_checked?
|
56
|
-
|
57
8
|
new(...).validate!
|
58
9
|
end
|
59
10
|
|
60
|
-
def self.should_be_checked?
|
61
|
-
true
|
62
|
-
end
|
63
|
-
|
64
|
-
##########################################################################
|
65
|
-
|
66
11
|
def initialize(context:, internal:, value:)
|
67
12
|
super()
|
68
13
|
|
@@ -71,76 +16,13 @@ module Servactory
|
|
71
16
|
@value = value
|
72
17
|
end
|
73
18
|
|
74
|
-
def validate!
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
@value.is_a?(@internal.types.fetch(0, Array)) &&
|
80
|
-
@value.respond_to?(:all?) && @value.all?(type)
|
81
|
-
elsif @internal.hash_mode?
|
82
|
-
object_schema_validator = Servactory::Maintenance::Validations::ObjectSchema.validate(
|
83
|
-
object: @value,
|
84
|
-
schema: @internal.schema
|
85
|
-
)
|
86
|
-
|
87
|
-
object_schema_validator.valid?
|
88
|
-
else
|
89
|
-
@value.is_a?(type)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
if (first_error = object_schema_validator&.errors&.first).present?
|
94
|
-
raise_default_object_error_with(first_error)
|
95
|
-
end
|
96
|
-
|
97
|
-
raise_default_error
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
|
102
|
-
def prepared_types
|
103
|
-
@prepared_types ||=
|
104
|
-
if @internal.collection_mode?
|
105
|
-
prepared_types_from(Array(@internal.consists_of.fetch(:type, [])))
|
106
|
-
else
|
107
|
-
prepared_types_from(@internal.types)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def prepared_types_from(types)
|
112
|
-
types.map do |type|
|
113
|
-
if type.is_a?(String)
|
114
|
-
Object.const_get(type)
|
115
|
-
else
|
116
|
-
type
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
########################################################################
|
122
|
-
|
123
|
-
def raise_default_object_error_with(error)
|
124
|
-
raise_error_with(
|
125
|
-
DEFAULT_MESSAGE,
|
126
|
-
service_class_name: @context.class.name,
|
127
|
-
internal: @internal,
|
128
|
-
value: @value,
|
129
|
-
key_name: error.fetch(:name),
|
130
|
-
expected_type: error.fetch(:expected_type),
|
131
|
-
given_type: error.fetch(:given_type)
|
132
|
-
)
|
133
|
-
end
|
134
|
-
|
135
|
-
def raise_default_error
|
136
|
-
raise_error_with(
|
137
|
-
DEFAULT_MESSAGE,
|
138
|
-
service_class_name: @context.class.name,
|
139
|
-
internal: @internal,
|
19
|
+
def validate!
|
20
|
+
Servactory::Maintenance::Validations::Types.validate!(
|
21
|
+
context: @context,
|
22
|
+
attribute: @internal,
|
23
|
+
types: @internal.types,
|
140
24
|
value: @value,
|
141
|
-
|
142
|
-
expected_type: prepared_types.join(", "),
|
143
|
-
given_type: @value.class.name
|
25
|
+
error_callback: ->(**args) { raise_error_with(**args) }
|
144
26
|
)
|
145
27
|
end
|
146
28
|
end
|
@@ -75,6 +75,7 @@ module Servactory
|
|
75
75
|
)
|
76
76
|
end
|
77
77
|
|
78
|
+
# rubocop:disable Metrics/MethodLength
|
78
79
|
def prepare_advanced_for(
|
79
80
|
body:,
|
80
81
|
body_key:,
|
@@ -82,17 +83,22 @@ module Servactory
|
|
82
83
|
body_fallback:
|
83
84
|
)
|
84
85
|
if body.is_a?(Hash)
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
86
|
+
if @name == :schema && body.fetch(body_key, nil).nil?
|
87
|
+
DEFAULT_BODY.call(key: body_key, body: body)
|
88
|
+
else
|
89
|
+
message = body.fetch(:message, nil)
|
90
|
+
|
91
|
+
DEFAULT_BODY.call(
|
92
|
+
key: body_key,
|
93
|
+
body: body.fetch(body_key, message.present? ? body_value : body_fallback),
|
94
|
+
message: message
|
95
|
+
)
|
96
|
+
end
|
92
97
|
else
|
93
98
|
DEFAULT_BODY.call(key: body_key, body: body)
|
94
99
|
end
|
95
100
|
end
|
101
|
+
# rubocop:enable Metrics/MethodLength
|
96
102
|
|
97
103
|
def prepare_methods_for(attribute)
|
98
104
|
attribute.instance_eval(define_methods_template) if define_methods_template.present?
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Maintenance
|
5
|
+
module Validations
|
6
|
+
class Collection
|
7
|
+
attr_reader :errors
|
8
|
+
|
9
|
+
def self.validate(...)
|
10
|
+
new(...).validate
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(value:, types:, type:)
|
14
|
+
@value = value
|
15
|
+
@types = types
|
16
|
+
@type = type
|
17
|
+
|
18
|
+
@errors = []
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate
|
22
|
+
unless @value.is_a?(prepared_type)
|
23
|
+
add_error(
|
24
|
+
expected_type: prepared_type.name,
|
25
|
+
given_type: @value.class.name
|
26
|
+
)
|
27
|
+
|
28
|
+
return self
|
29
|
+
end
|
30
|
+
|
31
|
+
validate_value!
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid?
|
37
|
+
@errors.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def prepared_type
|
43
|
+
@prepared_type ||= @types.fetch(0, Array)
|
44
|
+
end
|
45
|
+
|
46
|
+
def validate_value!
|
47
|
+
@value.each do |value_item|
|
48
|
+
next if value_item.is_a?(@type)
|
49
|
+
|
50
|
+
add_error(
|
51
|
+
expected_type: @type,
|
52
|
+
given_type: value_item.class.name
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def add_error(expected_type:, given_type:)
|
58
|
+
@errors << {
|
59
|
+
expected_type: expected_type,
|
60
|
+
given_type: given_type
|
61
|
+
}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -15,31 +15,30 @@ module Servactory
|
|
15
15
|
|
16
16
|
def initialize(object:, schema:)
|
17
17
|
@object = object
|
18
|
-
@schema = schema
|
18
|
+
@schema = schema.fetch(:is)
|
19
19
|
|
20
|
-
@valid = false
|
21
20
|
@errors = []
|
22
21
|
end
|
23
22
|
|
24
23
|
def validate
|
25
|
-
|
24
|
+
validate_for(object: @object, schema: @schema)
|
26
25
|
|
27
26
|
self
|
28
27
|
end
|
29
28
|
|
30
29
|
def valid?
|
31
|
-
@
|
30
|
+
@errors.empty?
|
32
31
|
end
|
33
32
|
|
34
33
|
private
|
35
34
|
|
36
|
-
def validate_for(object:, schema:, root_schema_key: nil) # rubocop:disable Metrics/MethodLength
|
35
|
+
def validate_for(object:, schema:, root_schema_key: nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
37
36
|
unless object.respond_to?(:fetch)
|
38
|
-
add_error(
|
39
|
-
return
|
37
|
+
add_error(key_name: root_schema_key, expected_type: Hash.name, given_type: object.class.name)
|
38
|
+
return
|
40
39
|
end
|
41
40
|
|
42
|
-
schema.
|
41
|
+
schema.each do |schema_key, schema_value|
|
43
42
|
attribute_type = schema_value.fetch(:type, String)
|
44
43
|
|
45
44
|
if attribute_type == Hash
|
@@ -57,15 +56,13 @@ module Servactory
|
|
57
56
|
attribute_required: schema_value.fetch(:required, true)
|
58
57
|
)
|
59
58
|
|
60
|
-
|
61
|
-
add_error(
|
62
|
-
name: schema_key,
|
63
|
-
expected_type: attribute_type,
|
64
|
-
given_type: object.fetch(schema_key, nil).class
|
65
|
-
)
|
66
|
-
end
|
59
|
+
next if is_success
|
67
60
|
|
68
|
-
|
61
|
+
add_error(
|
62
|
+
key_name: schema_key,
|
63
|
+
expected_type: attribute_type,
|
64
|
+
given_type: object.fetch(schema_key, nil).class.name
|
65
|
+
)
|
69
66
|
end
|
70
67
|
end
|
71
68
|
end
|
@@ -106,9 +103,9 @@ module Servactory
|
|
106
103
|
value.fetch(:default, nil)
|
107
104
|
end
|
108
105
|
|
109
|
-
def add_error(
|
106
|
+
def add_error(key_name:, expected_type:, given_type:)
|
110
107
|
@errors << {
|
111
|
-
|
108
|
+
key_name: key_name,
|
112
109
|
expected_type: expected_type,
|
113
110
|
given_type: given_type
|
114
111
|
}
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servactory
|
4
|
+
module Maintenance
|
5
|
+
module Validations
|
6
|
+
class Types # rubocop:disable Metrics/ClassLength
|
7
|
+
DEFAULT_MESSAGE = lambda do | # rubocop:disable Metrics/BlockLength
|
8
|
+
service_class_name:,
|
9
|
+
attribute_system_name:,
|
10
|
+
attribute:,
|
11
|
+
value:,
|
12
|
+
key_name:,
|
13
|
+
expected_type:,
|
14
|
+
given_type:
|
15
|
+
| # do
|
16
|
+
if attribute.collection_mode?
|
17
|
+
collection_message = attribute.consists_of.fetch(:message)
|
18
|
+
|
19
|
+
if collection_message.is_a?(Proc)
|
20
|
+
collection_message.call(
|
21
|
+
"#{attribute_system_name}_name": attribute.name,
|
22
|
+
expected_type: expected_type,
|
23
|
+
given_type: given_type
|
24
|
+
)
|
25
|
+
elsif collection_message.is_a?(String) && collection_message.present?
|
26
|
+
collection_message
|
27
|
+
elsif value.is_a?(attribute.types.fetch(0, Array))
|
28
|
+
I18n.t(
|
29
|
+
"servactory.#{attribute_system_name.to_s.pluralize}.checks.type.default_error.for_collection.wrong_element_type", # rubocop:disable Layout/LineLength
|
30
|
+
service_class_name: service_class_name,
|
31
|
+
"#{attribute_system_name}_name": attribute.name,
|
32
|
+
expected_type: expected_type,
|
33
|
+
given_type: given_type
|
34
|
+
)
|
35
|
+
else
|
36
|
+
I18n.t(
|
37
|
+
"servactory.#{attribute_system_name.to_s.pluralize}.checks.type.default_error.for_collection.wrong_type", # rubocop:disable Layout/LineLength
|
38
|
+
service_class_name: service_class_name,
|
39
|
+
"#{attribute_system_name}_name": attribute.name,
|
40
|
+
expected_type: attribute.types.fetch(0, Array),
|
41
|
+
given_type: value.class.name
|
42
|
+
)
|
43
|
+
end
|
44
|
+
elsif attribute.hash_mode? && key_name.present?
|
45
|
+
hash_message = attribute.schema.fetch(:message)
|
46
|
+
|
47
|
+
if hash_message.is_a?(Proc)
|
48
|
+
hash_message.call(
|
49
|
+
"#{attribute_system_name}_name": attribute.name,
|
50
|
+
key_name: key_name,
|
51
|
+
expected_type: expected_type,
|
52
|
+
given_type: given_type
|
53
|
+
)
|
54
|
+
elsif hash_message.is_a?(String) && hash_message.present?
|
55
|
+
hash_message
|
56
|
+
else
|
57
|
+
I18n.t(
|
58
|
+
"servactory.#{attribute_system_name.to_s.pluralize}.checks.type.default_error.for_hash.wrong_element_type", # rubocop:disable Layout/LineLength
|
59
|
+
service_class_name: service_class_name,
|
60
|
+
"#{attribute_system_name}_name": attribute.name,
|
61
|
+
key_name: key_name,
|
62
|
+
expected_type: expected_type,
|
63
|
+
given_type: given_type
|
64
|
+
)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
I18n.t(
|
68
|
+
"servactory.#{attribute_system_name.to_s.pluralize}.checks.type.default_error.default",
|
69
|
+
service_class_name: service_class_name,
|
70
|
+
"#{attribute_system_name}_name": attribute.name,
|
71
|
+
expected_type: expected_type,
|
72
|
+
given_type: given_type
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private_constant :DEFAULT_MESSAGE
|
78
|
+
|
79
|
+
def self.validate!(...)
|
80
|
+
new(...).validate!
|
81
|
+
end
|
82
|
+
|
83
|
+
def initialize(context:, attribute:, types:, value:, error_callback:)
|
84
|
+
@context = context
|
85
|
+
@attribute = attribute
|
86
|
+
@types = types
|
87
|
+
@value = value
|
88
|
+
@error_callback = error_callback
|
89
|
+
end
|
90
|
+
|
91
|
+
def validate! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
92
|
+
collection_validator = nil
|
93
|
+
object_schema_validator = nil
|
94
|
+
|
95
|
+
return if prepared_types.any? do |type|
|
96
|
+
if @attribute.collection_mode?
|
97
|
+
collection_validator = Servactory::Maintenance::Validations::Collection.validate(
|
98
|
+
value: @value,
|
99
|
+
types: @attribute.types,
|
100
|
+
type: type
|
101
|
+
)
|
102
|
+
|
103
|
+
collection_validator.valid?
|
104
|
+
elsif @attribute.hash_mode?
|
105
|
+
object_schema_validator = Servactory::Maintenance::Validations::ObjectSchema.validate(
|
106
|
+
object: @value,
|
107
|
+
schema: @attribute.schema
|
108
|
+
)
|
109
|
+
|
110
|
+
object_schema_validator.valid?
|
111
|
+
else
|
112
|
+
@value.is_a?(type)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
if (first_error = collection_validator&.errors&.first).present?
|
117
|
+
return @error_callback.call(
|
118
|
+
message: DEFAULT_MESSAGE,
|
119
|
+
service_class_name: @context.class.name,
|
120
|
+
attribute_system_name: attribute_system_name,
|
121
|
+
attribute: @attribute,
|
122
|
+
value: @value,
|
123
|
+
key_name: nil,
|
124
|
+
expected_type: first_error.fetch(:expected_type),
|
125
|
+
given_type: first_error.fetch(:given_type)
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
if (first_error = object_schema_validator&.errors&.first).present?
|
130
|
+
return @error_callback.call(
|
131
|
+
message: DEFAULT_MESSAGE,
|
132
|
+
service_class_name: @context.class.name,
|
133
|
+
attribute_system_name: attribute_system_name,
|
134
|
+
attribute: @attribute,
|
135
|
+
value: @value,
|
136
|
+
key_name: first_error.fetch(:key_name),
|
137
|
+
expected_type: first_error.fetch(:expected_type),
|
138
|
+
given_type: first_error.fetch(:given_type)
|
139
|
+
)
|
140
|
+
end
|
141
|
+
|
142
|
+
@error_callback.call(
|
143
|
+
message: DEFAULT_MESSAGE,
|
144
|
+
service_class_name: @context.class.name,
|
145
|
+
attribute_system_name: attribute_system_name,
|
146
|
+
attribute: @attribute,
|
147
|
+
value: @value,
|
148
|
+
key_name: nil,
|
149
|
+
expected_type: prepared_types.join(", "),
|
150
|
+
given_type: @value.class.name
|
151
|
+
)
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
|
156
|
+
def attribute_system_name
|
157
|
+
@attribute.class.name.demodulize.downcase.to_sym
|
158
|
+
end
|
159
|
+
|
160
|
+
def prepared_types
|
161
|
+
@prepared_types ||=
|
162
|
+
if @attribute.collection_mode?
|
163
|
+
prepared_types_from(Array(@attribute.consists_of.fetch(:type, [])))
|
164
|
+
else
|
165
|
+
prepared_types_from(@types)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def prepared_types_from(types)
|
170
|
+
types.map do |type|
|
171
|
+
if type.is_a?(String)
|
172
|
+
Object.const_get(type)
|
173
|
+
else
|
174
|
+
type
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -37,7 +37,7 @@ module Servactory
|
|
37
37
|
# Check Class: Servactory::Outputs::Validations::Type
|
38
38
|
add_types_option_with(type)
|
39
39
|
add_collection_option_with(type, options)
|
40
|
-
|
40
|
+
add_hash_option_with(type, options)
|
41
41
|
end
|
42
42
|
|
43
43
|
def add_types_option_with(type)
|
@@ -71,7 +71,7 @@ module Servactory
|
|
71
71
|
)
|
72
72
|
end
|
73
73
|
|
74
|
-
def
|
74
|
+
def add_hash_option_with(type, options) # rubocop:disable Metrics/MethodLength
|
75
75
|
collection_of_options << Servactory::Maintenance::Attributes::Option.new(
|
76
76
|
name: :schema,
|
77
77
|
attribute: self,
|
@@ -83,8 +83,8 @@ module Servactory
|
|
83
83
|
)
|
84
84
|
],
|
85
85
|
need_for_checks: false,
|
86
|
+
body_key: :is,
|
86
87
|
body_fallback: {},
|
87
|
-
with_advanced_mode: false,
|
88
88
|
**options
|
89
89
|
)
|
90
90
|
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.output_error_class.new(message: message)
|
@@ -4,65 +4,10 @@ module Servactory
|
|
4
4
|
module Outputs
|
5
5
|
module Validations
|
6
6
|
class Type < Base
|
7
|
-
DEFAULT_MESSAGE = lambda do |service_class_name:, output:, value:, key_name:, expected_type:, given_type:| # rubocop:disable Metrics/BlockLength
|
8
|
-
if output.collection_mode?
|
9
|
-
collection_message = output.consists_of.fetch(:message)
|
10
|
-
|
11
|
-
if collection_message.is_a?(Proc)
|
12
|
-
collection_message.call(output: output, expected_type: expected_type)
|
13
|
-
elsif collection_message.is_a?(String) && collection_message.present?
|
14
|
-
collection_message
|
15
|
-
elsif value.is_a?(output.types.fetch(0, Array))
|
16
|
-
I18n.t(
|
17
|
-
"servactory.outputs.checks.type.default_error.for_collection.wrong_element_type",
|
18
|
-
service_class_name: service_class_name,
|
19
|
-
output_name: output.name,
|
20
|
-
expected_type: expected_type,
|
21
|
-
given_type: given_type
|
22
|
-
)
|
23
|
-
else
|
24
|
-
I18n.t(
|
25
|
-
"servactory.outputs.checks.type.default_error.for_collection.wrong_type",
|
26
|
-
service_class_name: service_class_name,
|
27
|
-
output_name: output.name,
|
28
|
-
expected_type: output.types.fetch(0, Array),
|
29
|
-
given_type: value.class.name
|
30
|
-
)
|
31
|
-
end
|
32
|
-
elsif output.hash_mode? && key_name.present?
|
33
|
-
I18n.t(
|
34
|
-
"servactory.outputs.checks.type.default_error.for_hash.wrong_element_type",
|
35
|
-
service_class_name: service_class_name,
|
36
|
-
output_name: output.name,
|
37
|
-
key_name: key_name,
|
38
|
-
expected_type: expected_type,
|
39
|
-
given_type: given_type
|
40
|
-
)
|
41
|
-
else
|
42
|
-
I18n.t(
|
43
|
-
"servactory.outputs.checks.type.default_error.default",
|
44
|
-
service_class_name: service_class_name,
|
45
|
-
output_name: output.name,
|
46
|
-
expected_type: expected_type,
|
47
|
-
given_type: given_type
|
48
|
-
)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
private_constant :DEFAULT_MESSAGE
|
53
|
-
|
54
7
|
def self.validate!(...)
|
55
|
-
return unless should_be_checked?
|
56
|
-
|
57
8
|
new(...).validate!
|
58
9
|
end
|
59
10
|
|
60
|
-
def self.should_be_checked?
|
61
|
-
true
|
62
|
-
end
|
63
|
-
|
64
|
-
##########################################################################
|
65
|
-
|
66
11
|
def initialize(context:, output:, value:)
|
67
12
|
super()
|
68
13
|
|
@@ -71,76 +16,13 @@ module Servactory
|
|
71
16
|
@value = value
|
72
17
|
end
|
73
18
|
|
74
|
-
def validate!
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
@value.is_a?(@output.types.fetch(0, Array)) &&
|
80
|
-
@value.respond_to?(:all?) && @value.all?(type)
|
81
|
-
elsif @output.hash_mode?
|
82
|
-
object_schema_validator = Servactory::Maintenance::Validations::ObjectSchema.validate(
|
83
|
-
object: @value,
|
84
|
-
schema: @output.schema
|
85
|
-
)
|
86
|
-
|
87
|
-
object_schema_validator.valid?
|
88
|
-
else
|
89
|
-
@value.is_a?(type)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
if (first_error = object_schema_validator&.errors&.first).present?
|
94
|
-
raise_default_object_error_with(first_error)
|
95
|
-
end
|
96
|
-
|
97
|
-
raise_default_error
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
|
102
|
-
def prepared_types
|
103
|
-
@prepared_types ||=
|
104
|
-
if @output.collection_mode?
|
105
|
-
prepared_types_from(Array(@output.consists_of.fetch(:type, [])))
|
106
|
-
else
|
107
|
-
prepared_types_from(@output.types)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def prepared_types_from(types)
|
112
|
-
types.map do |type|
|
113
|
-
if type.is_a?(String)
|
114
|
-
Object.const_get(type)
|
115
|
-
else
|
116
|
-
type
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
########################################################################
|
122
|
-
|
123
|
-
def raise_default_object_error_with(error)
|
124
|
-
raise_error_with(
|
125
|
-
DEFAULT_MESSAGE,
|
126
|
-
service_class_name: @context.class.name,
|
127
|
-
output: @output,
|
128
|
-
value: @value,
|
129
|
-
key_name: error.fetch(:name),
|
130
|
-
expected_type: error.fetch(:expected_type),
|
131
|
-
given_type: error.fetch(:given_type)
|
132
|
-
)
|
133
|
-
end
|
134
|
-
|
135
|
-
def raise_default_error
|
136
|
-
raise_error_with(
|
137
|
-
DEFAULT_MESSAGE,
|
138
|
-
service_class_name: @context.class.name,
|
139
|
-
output: @output,
|
19
|
+
def validate!
|
20
|
+
Servactory::Maintenance::Validations::Types.validate!(
|
21
|
+
context: @context,
|
22
|
+
attribute: @output,
|
23
|
+
types: @output.types,
|
140
24
|
value: @value,
|
141
|
-
|
142
|
-
expected_type: prepared_types.join(", "),
|
143
|
-
given_type: @value.class.name
|
25
|
+
error_callback: ->(**args) { raise_error_with(**args) }
|
144
26
|
)
|
145
27
|
end
|
146
28
|
end
|