mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-04-01 09:51:29 +08:00
Add non-cyclical validation to connections [SCI-6287] (#3702)
* Add non-cyclical validation to connections [SCI-6287] * Prevent theoretical infinite loop possiblity [SCI-6287]
This commit is contained in:
parent
7d4f265506
commit
16b3ee3d94
2 changed files with 26 additions and 0 deletions
|
@ -3,4 +3,26 @@
|
|||
class Connection < ApplicationRecord
|
||||
belongs_to :to, class_name: 'MyModule', foreign_key: 'input_id', inverse_of: :inputs
|
||||
belongs_to :from, class_name: 'MyModule', foreign_key: 'output_id', inverse_of: :outputs
|
||||
|
||||
validate :ensure_non_cyclical
|
||||
|
||||
private
|
||||
|
||||
def ensure_non_cyclical
|
||||
connections = Connection.where(
|
||||
input_id: to.experiment.my_modules.select(:id)
|
||||
).pluck(:input_id, :output_id).to_h
|
||||
|
||||
visited_nodes = [output_id]
|
||||
|
||||
current_input_id = output_id
|
||||
|
||||
while (current_input_id = connections[current_input_id])
|
||||
if current_input_id == input_id || visited_nodes.include?(current_input_id)
|
||||
errors.add(:output_id, :creates_cycle) and return
|
||||
end
|
||||
|
||||
visited_nodes.push(current_input_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -163,6 +163,10 @@ en:
|
|||
disabled: 'Webhooks are disabled'
|
||||
url:
|
||||
not_valid: 'Not valid URL'
|
||||
connection:
|
||||
attributes:
|
||||
output_id:
|
||||
creates_cycle: "mustn't create cycle"
|
||||
|
||||
helpers:
|
||||
label:
|
||||
|
|
Loading…
Add table
Reference in a new issue