Add update validations to SnippetInputAction

parent 951fd679
...@@ -16,7 +16,8 @@ class SnippetInputAction ...@@ -16,7 +16,8 @@ class SnippetInputAction
validates :action, inclusion: { in: ACTIONS, message: "%{value} is not a valid action" } validates :action, inclusion: { in: ACTIONS, message: "%{value} is not a valid action" }
validates :previous_path, presence: true, if: :move_action? validates :previous_path, presence: true, if: :move_action?
validates :file_path, presence: true validates :file_path, presence: true
validates :content, presence: true, if: :create_action? validates :content, presence: true, if: -> (action) { action.create_action? || action.update_action? }
validate :ensure_same_file_path_and_previous_path, if: :update_action?
def initialize(action: nil, previous_path: nil, file_path: nil, content: nil) def initialize(action: nil, previous_path: nil, file_path: nil, content: nil)
@action = action @action = action
...@@ -41,4 +42,10 @@ class SnippetInputAction ...@@ -41,4 +42,10 @@ class SnippetInputAction
previous_path.presence || file_path previous_path.presence || file_path
end end
def ensure_same_file_path_and_previous_path
return if previous_path == file_path
errors.add(:file_path, "can't be different from the previous_path attribute")
end
end end
---
title: Add update validations to SnippetInputAction
merge_request: 33379
author:
type: changed
...@@ -20,6 +20,8 @@ describe SnippetInputAction do ...@@ -20,6 +20,8 @@ describe SnippetInputAction do
'create' | 'foobar' | '' | 'foobar' | false 'create' | 'foobar' | '' | 'foobar' | false
'create' | nil | 'foobar' | 'foobar' | false 'create' | nil | 'foobar' | 'foobar' | false
'create' | '' | 'foobar' | 'foobar' | false 'create' | '' | 'foobar' | 'foobar' | false
'update' | 'foobar' | nil | 'foobar' | false
'update' | 'other' | 'foobar' | 'foobar' | false
end end
with_them do with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment