Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoBackendAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
Backend
DojoBackendAPI
Commits
c013a184
Commit
c013a184
authored
2 years ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
Add Dojo validator
parent
451d3f12
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ExpressAPI/src/helpers/DojoValidators.ts
+106
-0
106 additions, 0 deletions
ExpressAPI/src/helpers/DojoValidators.ts
with
106 additions
and
0 deletions
ExpressAPI/src/helpers/DojoValidators.ts
0 → 100644
+
106
−
0
View file @
c013a184
import
ApiRequest
from
'
../models/ApiRequest
'
;
import
Config
from
'
../config/Config
'
;
import
GitlabHelper
from
'
./GitlabHelper
'
;
import
{
StatusCodes
}
from
'
http-status-codes
'
;
import
{
CustomValidator
,
ErrorMessage
,
FieldMessageFactory
,
Meta
}
from
'
express-validator/src/base
'
;
import
{
BailOptions
,
ValidationChain
}
from
'
express-validator/src/chain
'
;
declare
type
DojoMeta
=
Meta
&
{
req
:
ApiRequest
};
declare
type
DojoCustomValidator
=
(
input
:
any
,
meta
:
DojoMeta
)
=>
any
;
class
DojoValidators
{
private
static
_instance
:
DojoValidators
;
private
constructor
()
{
}
public
static
get
instance
():
DojoValidators
{
if
(
!
DojoValidators
.
_instance
)
{
DojoValidators
.
_instance
=
new
DojoValidators
();
}
return
DojoValidators
.
_instance
;
}
private
toValidatorSchemaOptions
(
arg
:
{
errorMessage
?:
FieldMessageFactory
|
ErrorMessage
,
negated
?:
boolean
,
bail
?:
boolean
|
BailOptions
,
if
?:
DojoCustomValidator
|
ValidationChain
,
options
?:
DojoCustomValidator
})
{
// This is a hack to make the types work with req arg as ApiRequest instead of Request
return
arg
as
unknown
as
{
errorMessage
?:
FieldMessageFactory
|
ErrorMessage
,
negated
?:
boolean
,
bail
?:
boolean
|
BailOptions
,
if
?:
CustomValidator
|
ValidationChain
,
options
?:
CustomValidator
};
}
private
getParamValue
(
req
:
ApiRequest
,
path
:
string
):
any
{
return
'
body
'
in
req
&&
path
in
req
.
body
?
req
.
body
[
path
]
:
req
.
query
[
path
];
}
readonly
nullSanitizer
=
this
.
toValidatorSchemaOptions
({
options
:
(
value
,
{
req
,
location
,
path
})
=>
{
try
{
return
value
==
'
null
'
||
value
==
'
undefined
'
||
value
==
''
?
null
:
value
;
}
catch
(
e
)
{
return
value
;
}
}
});
readonly
arraySanitizer
=
this
.
toValidatorSchemaOptions
({
options
:
(
value
,
{
req
,
location
,
path
})
=>
{
try
{
return
JSON
.
parse
(
value
);
}
catch
(
e
)
{
return
value
;
}
}
});
readonly
templateUrlValidator
=
this
.
toValidatorSchemaOptions
({
bail
:
true
,
errorMessage
:
'
Template doesn
\'
t exist or you don
\'
t have access to it
'
,
options
:
(
value
,
{
req
,
location
,
path
})
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
template
=
this
.
getParamValue
(
req
,
path
);
if
(
template
)
{
GitlabHelper
.
checkTemplateAccess
(
template
,
req
).
then
((
templateAccess
)
=>
{
templateAccess
!==
StatusCodes
.
OK
?
reject
()
:
resolve
(
true
);
});
}
resolve
(
true
);
});
}
});
readonly
templateUrlSanitizer
=
this
.
toValidatorSchemaOptions
({
options
:
(
value
,
{
req
,
location
,
path
})
=>
{
try
{
const
template
=
this
.
getParamValue
(
req
,
path
);
if
(
template
)
{
return
`
${
Config
.
gitlab
.
urls
[
0
].
replace
(
/^
([
a-z
]{3,5}
:
\/{2})?(
.*
)
/
,
`$1
${
Config
.
gitlab
.
account
.
username
}
:
${
Config
.
gitlab
.
account
.
token
}
@$2`
)
}${
template
}
.git`
;
}
}
catch
(
e
)
{
}
return
value
;
}
});
}
export
default
DojoValidators
.
instance
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment