Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bf1ed85a
Commit
bf1ed85a
authored
Oct 25, 2018
by
Heinrich Lee Yu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor api validator to separate class
parent
d4e26636
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
8 deletions
+77
-8
lib/api/helpers/custom_validators.rb
lib/api/helpers/custom_validators.rb
+13
-0
lib/api/issues.rb
lib/api/issues.rb
+1
-4
lib/api/merge_requests.rb
lib/api/merge_requests.rb
+1
-4
spec/lib/api/helpers/custom_validators_spec.rb
spec/lib/api/helpers/custom_validators_spec.rb
+62
-0
No files found.
lib/api/helpers/custom_validators.rb
View file @
bf1ed85a
...
...
@@ -10,8 +10,21 @@ module API
raise
Grape
::
Exceptions
::
Validation
,
params:
[
@scope
.
full_name
(
attr_name
)],
message:
message
(
:absence
)
end
end
class
IntegerNoneAny
<
Grape
::
Validations
::
Base
def
validate_param!
(
attr_name
,
params
)
value
=
params
[
attr_name
]
return
if
value
.
is_a?
(
Integer
)
||
[
IssuableFinder
::
FILTER_NONE
,
IssuableFinder
::
FILTER_ANY
].
include?
(
value
)
raise
Grape
::
Exceptions
::
Validation
,
params:
[
@scope
.
full_name
(
attr_name
)],
message:
"should be an integer, 'None' or 'Any'"
end
end
end
end
end
Grape
::
Validations
.
register_validator
(
:absence
,
::
API
::
Helpers
::
CustomValidators
::
Absence
)
Grape
::
Validations
.
register_validator
(
:integer_none_any
,
::
API
::
Helpers
::
CustomValidators
::
IntegerNoneAny
)
lib/api/issues.rb
View file @
bf1ed85a
...
...
@@ -40,10 +40,7 @@ module API
optional
:updated_after
,
type:
DateTime
,
desc:
'Return issues updated after the specified time'
optional
:updated_before
,
type:
DateTime
,
desc:
'Return issues updated before the specified time'
optional
:author_id
,
type:
Integer
,
desc:
'Return issues which are authored by the user with the given ID'
optional
:assignee_id
,
types:
[
Integer
,
String
],
values:
->
(
v
)
{
v
.
is_a?
(
Integer
)
||
[
IssuableFinder
::
FILTER_NONE
,
IssuableFinder
::
FILTER_ANY
].
include?
(
v
)
},
optional
:assignee_id
,
types:
[
Integer
,
String
],
integer_none_any:
true
,
desc:
'Return issues which are assigned to the user with the given ID'
optional
:scope
,
type:
String
,
values:
%w[created-by-me assigned-to-me created_by_me assigned_to_me all]
,
desc:
'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
...
...
lib/api/merge_requests.rb
View file @
bf1ed85a
...
...
@@ -89,10 +89,7 @@ module API
optional
:updated_before
,
type:
DateTime
,
desc:
'Return merge requests updated before the specified time'
optional
:view
,
type:
String
,
values:
%w[simple]
,
desc:
'If simple, returns the `iid`, URL, title, description, and basic state of merge request'
optional
:author_id
,
type:
Integer
,
desc:
'Return merge requests which are authored by the user with the given ID'
optional
:assignee_id
,
types:
[
Integer
,
String
],
values:
->
(
v
)
{
v
.
is_a?
(
Integer
)
||
[
IssuableFinder
::
FILTER_NONE
,
IssuableFinder
::
FILTER_ANY
].
include?
(
v
)
},
optional
:assignee_id
,
types:
[
Integer
,
String
],
integer_none_any:
true
,
desc:
'Return merge requests which are assigned to the user with the given ID'
optional
:scope
,
type:
String
,
values:
%w[created-by-me assigned-to-me created_by_me assigned_to_me all]
,
desc:
'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
...
...
spec/lib/api/helpers/custom_validators_spec.rb
0 → 100644
View file @
bf1ed85a
require
'spec_helper'
describe
API
::
Helpers
::
CustomValidators
do
let
(
:scope
)
do
Struct
.
new
(
:opts
)
do
def
full_name
(
attr_name
)
attr_name
end
end
end
describe
API
::
Helpers
::
CustomValidators
::
Absence
do
subject
do
described_class
.
new
([
'test'
],
{},
false
,
scope
.
new
)
end
context
'empty param'
do
it
'does not raise a validation error'
do
expect_no_validation_error
({})
end
end
context
'invalid parameters'
do
it
'should raise a validation error'
do
expect_validation_error
({
'test'
=>
'some_value'
})
end
end
end
describe
API
::
Helpers
::
CustomValidators
::
IntegerNoneAny
do
subject
do
described_class
.
new
([
'test'
],
{},
false
,
scope
.
new
)
end
context
'valid parameters'
do
it
'does not raise a validation error'
do
expect_no_validation_error
({
'test'
=>
2
})
expect_no_validation_error
({
'test'
=>
100
})
expect_no_validation_error
({
'test'
=>
'None'
})
expect_no_validation_error
({
'test'
=>
'Any'
})
end
end
context
'invalid parameters'
do
it
'should raise a validation error'
do
expect_validation_error
({
'test'
=>
'some_other_string'
})
end
end
end
def
expect_no_validation_error
(
params
)
expect
{
validate_test_param!
(
params
)
}.
not_to
raise_error
end
def
expect_validation_error
(
params
)
expect
{
validate_test_param!
(
params
)
}.
to
raise_error
(
Grape
::
Exceptions
::
Validation
)
end
def
validate_test_param!
(
params
)
subject
.
validate_param!
(
'test'
,
params
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment