Commit 81d22318 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'fj-218516-add-validation-to-move-action' into 'master'

Add validation for move action in SnippetInputAction

See merge request gitlab-org/gitlab!34911
parents 577301cc 6ba6299d
......@@ -18,6 +18,7 @@ class SnippetInputAction
validates :file_path, presence: true
validates :content, presence: true, if: -> (action) { action.create_action? || action.update_action? }
validate :ensure_same_file_path_and_previous_path, if: :update_action?
validate :ensure_different_file_path_and_previous_path, if: :move_action?
validate :ensure_allowed_action
def initialize(action: nil, previous_path: nil, file_path: nil, content: nil, allowed_actions: nil)
......@@ -52,6 +53,12 @@ class SnippetInputAction
errors.add(:file_path, "can't be different from the previous_path attribute")
end
def ensure_different_file_path_and_previous_path
return if previous_path != file_path
errors.add(:file_path, 'must be different from the previous_path attribute')
end
def ensure_allowed_action
return if @allowed_actions.empty?
......
---
title: Add validation for move action in SnippetInputAction
merge_request: 34911
author:
type: other
......@@ -8,12 +8,12 @@ describe SnippetInputAction do
where(:action, :file_path, :content, :previous_path, :allowed_actions, :is_valid, :invalid_field) do
:create | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
:move | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
:move | 'foobar' | 'foobar' | 'foo1' | nil | true | nil
:delete | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
:update | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
:foo | 'foobar' | 'foobar' | 'foobar' | nil | false | :action
'create' | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
'move' | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
'move' | 'foobar' | 'foobar' | 'foo1' | nil | true | nil
'delete' | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
'update' | 'foobar' | 'foobar' | 'foobar' | nil | true | nil
'foo' | 'foobar' | 'foobar' | 'foobar' | nil | false | :action
......@@ -21,6 +21,12 @@ describe SnippetInputAction do
'' | 'foobar' | 'foobar' | 'foobar' | nil | false | :action
:move | 'foobar' | 'foobar' | nil | nil | false | :previous_path
:move | 'foobar' | 'foobar' | '' | nil | false | :previous_path
:move | 'foobar' | 'foobar' | 'foobar' | nil | false | :file_path
:move | nil | 'foobar' | 'foobar' | nil | false | :file_path
:move | '' | 'foobar' | 'foobar' | nil | false | :file_path
:move | nil | 'foobar' | 'foo1' | nil | false | :file_path
:move | 'foobar' | nil | 'foo1' | nil | true | nil
:move | 'foobar' | '' | 'foo1' | nil | true | nil
:create | 'foobar' | nil | 'foobar' | nil | false | :content
:create | 'foobar' | '' | 'foobar' | nil | false | :content
:create | nil | 'foobar' | 'foobar' | nil | false | :file_path
......
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