Skip to content

Commit 4485e78

Browse files
authored
Merge pull request #607 from code0-tech/593-validation-result
Implement base construct from ValidationResult
2 parents d42a043 + 9a8210b commit 4485e78

22 files changed

+336
-541
lines changed

app/graphql/types/node_parameter_value_type.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ class NodeParameterValueType < Types::BaseUnion
99

1010
def self.resolve_type(object, _context)
1111
case object
12-
when LiteralValue
13-
Types::LiteralValueType
1412
when ReferenceValue
1513
Types::ReferenceValueType
1614
when NodeFunction
1715
Types::NodeFunctionType
1816
else
19-
raise "Unexpected value type: #{object.class}"
17+
Types::LiteralValueType
2018
end
2119
end
2220
end

app/services/namespaces/projects/flows/validation/data_type/data_type_identifier_validation_service.rb

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,45 @@ def initialize(current_authentication, flow, node, data_type_identifier)
1919
end
2020

2121
def execute
22+
errors = []
2223
logger.debug(message: 'Validating data_type_identifier', data_type_identifier: data_type_identifier.id,
2324
flow_id: flow.id)
2425

25-
transactional do |t|
26-
if data_type_identifier.invalid?
27-
logger.debug(message: 'Data type identifier validation failed',
28-
flow: flow.id,
29-
data_type_identifier: data_type_identifier.id,
30-
errors: data_type_identifier.errors.full_messages)
31-
t.rollback_and_return!(
32-
ServiceResponse.error(
33-
message: 'Data type identifier is invalid',
34-
payload: data_type_identifier.errors
35-
)
36-
)
37-
end
38-
if data_type_identifier.runtime != flow.project.primary_runtime
39-
logger.debug(message: 'Data type identifier runtime mismatch',
40-
primary_runtime: flow.project.primary_runtime.id,
41-
given_runtime: data_type_identifier.runtime.id,
42-
flow: flow.id,
43-
data_type_identifier: data_type_identifier.id)
44-
t.rollback_and_return!(
45-
ServiceResponse.error(
46-
message: 'Data type identifier runtime does not match the primary runtime of the project',
47-
payload: :runtime_mismatch
48-
)
49-
)
50-
end
26+
if data_type_identifier.invalid?
27+
logger.debug(message: 'Data type identifier validation failed',
28+
flow: flow.id,
29+
data_type_identifier: data_type_identifier.id,
30+
errors: data_type_identifier.errors.full_messages)
31+
errors << ValidationResult.error(:data_type_identifier_model_invalid, data_type_identifier.errors)
32+
end
33+
if data_type_identifier.runtime != flow.project.primary_runtime
34+
logger.debug(message: 'Data type identifier runtime mismatch',
35+
primary_runtime: flow.project.primary_runtime.id,
36+
given_runtime: data_type_identifier.runtime.id,
37+
flow: flow.id,
38+
data_type_identifier: data_type_identifier.id)
39+
errors << ValidationResult.error(:data_type_identifier_runtime_mismatch)
40+
end
5141

52-
if data_type_identifier.generic_key.present?
53-
unless node.runtime_function.generic_keys.include?(data_type_identifier.generic_key)
54-
t.rollback_and_return!(
55-
ServiceResponse.error(
56-
message: "Data type identifier #{data_type_identifier.id} " \
57-
'does not have a generic key which exists in the node function ' \
58-
"#{node.runtime_function.generic_keys}",
59-
payload: :generic_key_not_found
60-
)
61-
)
62-
end
63-
elsif data_type_identifier.generic_type.present?
64-
::NodeFunction::GenericTypeValidationService.new(
65-
current_authentication,
66-
flow,
67-
data_type_identifier.generic_type
68-
).execute
69-
elsif data_type_identifier.data_type.present?
70-
DataTypeValidationService.new(
71-
current_authentication,
72-
flow,
73-
data_type_identifier.data_type
74-
).execute
42+
if data_type_identifier.generic_key.present?
43+
unless node.runtime_function.generic_keys.include?(data_type_identifier.generic_key)
44+
errors << ValidationResult.error(:data_type_identifier_generic_key_not_found)
7545
end
76-
77-
nil
46+
elsif data_type_identifier.generic_type.present?
47+
errors += ::NodeFunction::GenericTypeValidationService.new(
48+
current_authentication,
49+
flow,
50+
data_type_identifier.generic_type
51+
).execute
52+
elsif data_type_identifier.data_type.present?
53+
errors += DataTypeValidationService.new(
54+
current_authentication,
55+
flow,
56+
data_type_identifier.data_type
57+
).execute
7858
end
59+
60+
errors
7961
end
8062
end
8163
end

app/services/namespaces/projects/flows/validation/data_type/data_type_rule_validation_service.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ def initialize(current_authentication, flow, data_type, rule)
1919
end
2020

2121
def execute
22+
errors = []
2223
logger.debug(message: 'Validating data type rule', rule_id: rule.id)
2324

24-
transactional do |t|
25+
transactional do |_t|
2526
if rule.invalid?
2627
logger.debug(message: 'Data type rule validation (model) failed',
2728
flow: flow.id,
2829
data_type: data_type.id,
2930
rule: rule.id,
3031
errors: rule.errors.full_messages)
31-
t.rollback_and_return! ServiceResponse.error(
32-
message: 'Data type rule is invalid',
33-
payload: rule.errors
34-
)
32+
errors << ValidationResult.error(:data_type_rule_model_invalid, rule.errors)
3533
end
3634
end
35+
errors
3736
end
3837
end
3938
end

app/services/namespaces/projects/flows/validation/data_type/data_type_validation_service.rb

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,51 @@ def initialize(current_authentication, flow, data_type)
1818
end
1919

2020
def execute
21-
logger.debug("Validating data type: #{data_type.id} for flow: #{flow.id}")
21+
errors = []
2222

23-
transactional do |t|
24-
if data_type.invalid?
25-
logger.debug(message: 'Data type validation failed',
26-
flow: flow.id,
27-
data_type: data_type.id,
28-
errors: data_type.errors.full_messages)
29-
t.rollback_and_return!(
30-
ServiceResponse.error(
31-
message: 'Data type is invalid',
32-
payload: data_type.errors
33-
)
34-
)
35-
end
23+
logger.debug("Validating data type: #{data_type.id} for flow: #{flow.id}")
3624

37-
primary_runtime = flow.project.primary_runtime
25+
if data_type.invalid?
26+
logger.debug(message: 'Data type validation failed',
27+
flow: flow.id,
28+
data_type: data_type.id,
29+
errors: data_type.errors.full_messages)
30+
errors << ValidationResult.error(:data_type_model_invalid, data_type.errors)
31+
end
3832

39-
if primary_runtime != data_type.runtime
40-
logger.debug(message: 'Data type runtime mismatch',
41-
primary_runtime: primary_runtime.id,
42-
given_runtime: data_type.runtime.id,
43-
flow: flow.id,
44-
data_type: data_type.id)
45-
t.rollback_and_return!(
46-
ServiceResponse.error(
47-
message: 'Data type runtime does not match the primary runtime of the project',
48-
payload: :runtime_mismatch
49-
)
50-
)
51-
end
33+
primary_runtime = flow.project.primary_runtime
5234

53-
data_type.parent_type&.tap do |parent_type|
54-
logger.debug("Validating parent type: #{parent_type.id} for data type: #{data_type.id}")
55-
DataTypeIdentifierValidationService.new(
56-
current_authentication,
57-
flow,
58-
nil,
59-
parent_type
60-
).execute
61-
end
35+
if primary_runtime != data_type.runtime
36+
logger.debug(message: 'Data type runtime mismatch',
37+
primary_runtime: primary_runtime.id,
38+
given_runtime: data_type.runtime.id,
39+
flow: flow.id,
40+
data_type: data_type.id)
41+
errors << ValidationResult.error(:data_type_runtime_mismatch)
42+
end
6243

63-
data_type.rules.each do |rule|
64-
logger.debug("Validating data type rule: #{rule.id} for data type: #{data_type.id}")
65-
DataTypeRuleValidationService.new(
66-
current_authentication,
67-
flow,
68-
data_type,
69-
rule
70-
).execute
71-
end
44+
data_type.parent_type&.tap do |parent_type|
45+
logger.debug("Validating parent type: #{parent_type.id} for data type: #{data_type.id}")
46+
errors += DataTypeIdentifierValidationService.new(
47+
current_authentication,
48+
flow,
49+
nil,
50+
parent_type
51+
).execute
52+
end
7253

73-
logger.debug(message: 'Data type is valid', flow: flow.id, data_type: data_type.id)
74-
nil
54+
data_type.rules.each do |rule|
55+
logger.debug("Validating data type rule: #{rule.id} for data type: #{data_type.id}")
56+
errors += DataTypeRuleValidationService.new(
57+
current_authentication,
58+
flow,
59+
data_type,
60+
rule
61+
).execute
7562
end
63+
64+
logger.debug(message: 'Data type is valid', flow: flow.id, data_type: data_type.id)
65+
errors
7666
end
7767
end
7868
end

app/services/namespaces/projects/flows/validation/flow_setting_validation_service.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@ def initialize(current_authentication, flow, setting)
1717
end
1818

1919
def execute
20+
errors = []
2021
logger.debug("Validating setting: #{setting.inspect} for flow: #{flow.id}")
2122

22-
transactional do |t|
23-
if setting.invalid?
24-
logger.debug("Invalid setting: #{setting.errors.full_messages.join(', ')}")
25-
t.rollback_and_return! ServiceResponse.error(
26-
message: 'Invalid flow setting',
27-
payload: setting.errors
28-
)
29-
end
23+
if setting.invalid?
24+
logger.debug("Invalid setting: #{setting.errors.full_messages.join(', ')}")
25+
errors << ValidationResult.error(:flow_setting_model_invalid, setting.errors)
3026
end
27+
errors
3128
end
3229
end
3330
end

app/services/namespaces/projects/flows/validation/flow_type_validation_service.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ def initialize(current_authentication, flow, flow_type)
1717
end
1818

1919
def execute
20+
errors = []
2021
logger.debug("Validating flow_type: #{flow_type.inspect} for flow: #{flow.id}")
2122

22-
transactional do |t|
23-
if flow_type.runtime != flow.project.primary_runtime
24-
t.rollback_and_return!(
25-
ServiceResponse.error(
26-
message: 'Flow type runtime definition does not match the primary runtime of the project',
27-
payload: :runtime_mismatch
28-
)
29-
)
30-
end
23+
if flow_type.runtime != flow.project.primary_runtime
24+
errors << ValidationResult.error(:flow_type_runtime_mismatch)
3125
end
26+
errors
3227
end
3328
end
3429
end

app/services/namespaces/projects/flows/validation/node_function/generic_mapper_validation_service.rb

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,29 @@ def initialize(current_authentication, flow, parameter, generic_mapper)
1919
end
2020

2121
def execute
22+
errors = []
2223
logger.debug("Validating generic mapper: #{generic_mapper.inspect} for flow: #{flow.id}")
2324

24-
transactional do |t|
25-
target = generic_mapper.target
26-
27-
# Validate the target the identifier gets validated later
28-
unless parameter.node_function.runtime_function.generic_keys.include?(target)
29-
t.rollback_and_return!(
30-
ServiceResponse.error(
31-
message: "Runtime function definition #{parameter.node_function.runtime_function} " \
32-
"does not have a generic key for target #{target}",
33-
payload: :generic_key_not_found
34-
)
35-
)
36-
end
37-
38-
generic_mapper.generic_combination_strategies.each do |_strategy|
39-
# https://github.com/code0-tech/sagittarius/issues/509
40-
end
41-
42-
generic_mapper.sources.each do |source|
43-
Namespaces::Projects::Flows::Validation::DataType::DataTypeIdentifierValidationService.new(
44-
current_authentication,
45-
flow,
46-
parameter.node_function,
47-
source
48-
).execute
49-
end
25+
target = generic_mapper.target
26+
27+
# Validate the target the identifier gets validated later
28+
unless parameter.node_function.runtime_function.generic_keys.include?(target)
29+
errors << ValidationResult.error(:generic_key_not_found)
30+
end
31+
32+
generic_mapper.generic_combination_strategies.each do |_strategy|
33+
# https://github.com/code0-tech/sagittarius/issues/509
34+
end
35+
36+
generic_mapper.sources.each do |source|
37+
errors += Namespaces::Projects::Flows::Validation::DataType::DataTypeIdentifierValidationService.new(
38+
current_authentication,
39+
flow,
40+
parameter.node_function,
41+
source
42+
).execute
5043
end
51-
nil
44+
errors
5245
end
5346
end
5447
end

app/services/namespaces/projects/flows/validation/node_function/generic_type_validation_service.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ def initialize(current_authentication, flow, node_function, generic_type)
1919
end
2020

2121
def execute
22-
::DataType::DataTypeValidationService.new(
22+
errors = []
23+
errors += ::DataType::DataTypeValidationService.new(
2324
current_authentication,
2425
flow,
2526
generic_type.data_type
2627
).execute
2728

2829
generic_type.generic_mappers.each do |generic_mapper|
29-
::DataType::GenericMapperValidationService.new(
30+
errors += ::DataType::GenericMapperValidationService.new(
3031
current_authentication,
3132
flow,
3233
parameter,
3334
generic_mapper
3435
).execute
3536
end
37+
errors
3638
end
3739
end
3840
end

0 commit comments

Comments
 (0)