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
1fadf33b
Commit
1fadf33b
authored
Mar 04, 2020
by
Rajendra Kadam
Committed by
Peter Leitzen
Mar 04, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom sha validator
parent
ced9b0ca
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
0 deletions
+49
-0
changelogs/unreleased/sha-params-validator.yml
changelogs/unreleased/sha-params-validator.yml
+5
-0
lib/api/helpers/custom_validators.rb
lib/api/helpers/custom_validators.rb
+12
-0
spec/lib/api/helpers/custom_validators_spec.rb
spec/lib/api/helpers/custom_validators_spec.rb
+32
-0
No files found.
changelogs/unreleased/sha-params-validator.yml
0 → 100644
View file @
1fadf33b
---
title
:
Add grape custom validator for sha params
merge_request
:
26220
author
:
Rajendra Kadam
type
:
added
lib/api/helpers/custom_validators.rb
View file @
1fadf33b
...
...
@@ -14,6 +14,17 @@ module API
end
end
class
GitSha
<
Grape
::
Validations
::
Base
def
validate_param!
(
attr_name
,
params
)
sha
=
params
[
attr_name
]
return
if
Commit
::
EXACT_COMMIT_SHA_PATTERN
.
match?
(
sha
)
raise
Grape
::
Exceptions
::
Validation
,
params:
[
@scope
.
full_name
(
attr_name
)],
message:
"should be a valid sha"
end
end
class
Absence
<
Grape
::
Validations
::
Base
def
validate_param!
(
attr_name
,
params
)
return
if
params
.
respond_to?
(
:key?
)
&&
!
params
.
key?
(
attr_name
)
...
...
@@ -50,6 +61,7 @@ module API
end
Grape
::
Validations
.
register_validator
(
:file_path
,
::
API
::
Helpers
::
CustomValidators
::
FilePath
)
Grape
::
Validations
.
register_validator
(
:git_sha
,
::
API
::
Helpers
::
CustomValidators
::
GitSha
)
Grape
::
Validations
.
register_validator
(
:absence
,
::
API
::
Helpers
::
CustomValidators
::
Absence
)
Grape
::
Validations
.
register_validator
(
:integer_none_any
,
::
API
::
Helpers
::
CustomValidators
::
IntegerNoneAny
)
Grape
::
Validations
.
register_validator
(
:array_none_any
,
::
API
::
Helpers
::
CustomValidators
::
ArrayNoneAny
)
spec/lib/api/helpers/custom_validators_spec.rb
View file @
1fadf33b
...
...
@@ -29,6 +29,38 @@ describe API::Helpers::CustomValidators do
end
end
describe
API
::
Helpers
::
CustomValidators
::
GitSha
do
let
(
:sha
)
{
RepoHelpers
.
sample_commit
.
id
}
let
(
:short_sha
)
{
sha
[
0
,
Gitlab
::
Git
::
Commit
::
MIN_SHA_LENGTH
]
}
let
(
:too_short_sha
)
{
sha
[
0
,
Gitlab
::
Git
::
Commit
::
MIN_SHA_LENGTH
-
1
]
}
subject
do
described_class
.
new
([
'test'
],
{},
false
,
scope
.
new
)
end
context
'valid sha'
do
it
'does not raise a validation error'
do
expect_no_validation_error
(
'test'
=>
sha
)
expect_no_validation_error
(
'test'
=>
short_sha
)
end
end
context
'empty params'
do
it
'raises a validation error'
do
expect_validation_error
(
'test'
=>
nil
)
expect_validation_error
(
'test'
=>
''
)
end
end
context
'invalid sha'
do
it
'raises a validation error'
do
expect_validation_error
(
'test'
=>
"
#{
sha
}
2"
)
# Sha length > 40
expect_validation_error
(
'test'
=>
'somestring'
)
expect_validation_error
(
'test'
=>
too_short_sha
)
# sha length < MIN_SHA_LENGTH (7)
end
end
end
describe
API
::
Helpers
::
CustomValidators
::
FilePath
do
subject
do
described_class
.
new
([
'test'
],
{},
false
,
scope
.
new
)
...
...
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