Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ehasa-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
WIMMA Lab 2019
Overflow
ehasa-backend
Commits
08f59052
Commit
08f59052
authored
5 years ago
by
L4168
Browse files
Options
Downloads
Patches
Plain Diff
forbidNonWhitelisted: true
parent
763d20b7
No related branches found
No related tags found
3 merge requests
!59
Development to master
,
!31
Development
,
!29
Json validation
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/shared/validation.pipe.ts
+47
-34
47 additions, 34 deletions
src/shared/validation.pipe.ts
with
47 additions
and
34 deletions
src/shared/validation.pipe.ts
+
47
−
34
View file @
08f59052
import
{
import
{
PipeTransform
,
Injectable
,
ArgumentMetadata
,
BadRequestException
,
HttpException
,
HttpStatus
}
from
'
@nestjs/common
'
;
PipeTransform
,
Injectable
,
ArgumentMetadata
,
HttpException
,
HttpStatus
,
}
from
'
@nestjs/common
'
;
import
{
validate
}
from
'
class-validator
'
;
import
{
validate
}
from
'
class-validator
'
;
import
{
plainToClass
}
from
'
class-transformer
'
;
import
{
plainToClass
}
from
'
class-transformer
'
;
@
Injectable
()
@
Injectable
()
export
class
ValidationPipe
implements
PipeTransform
<
any
>
{
export
class
ValidationPipe
implements
PipeTransform
<
any
>
{
async
transform
(
value
:
any
,
metadata
:
ArgumentMetadata
)
{
async
transform
(
value
:
any
,
metadata
:
ArgumentMetadata
)
{
if
(
value
instanceof
Object
&&
this
.
isEmpty
(
value
))
{
if
(
value
instanceof
Object
&&
this
.
isEmpty
(
value
))
{
throw
new
HttpException
(
throw
new
HttpException
(
'
Validation failed: No body submitted
'
,
'
Validation failed: No body submitted
'
,
HttpStatus
.
BAD_REQUEST
HttpStatus
.
BAD_REQUEST
,
);
);
}
const
{
metatype
}
=
metadata
;
if
(
!
metatype
||
!
this
.
toValidate
(
metatype
))
{
return
value
;
}
const
object
=
plainToClass
(
metatype
,
value
);
const
errors
=
await
validate
(
object
);
if
(
errors
.
length
>
0
)
{
throw
new
HttpException
(
`Validation failed:
${
this
.
formatErrors
(
errors
)}
`
,
HttpStatus
.
BAD_REQUEST
);
}
return
value
;
}
}
private
toValidate
(
metatype
:
Function
):
boolean
{
const
{
metatype
}
=
metadata
;
const
types
:
Function
[]
=
[
String
,
Boolean
,
Number
,
Array
,
Object
];
if
(
!
metatype
||
!
this
.
toValidate
(
metatype
))
{
return
!
types
.
includes
(
metatype
)
;
return
value
;
}
}
const
object
=
plainToClass
(
metatype
,
value
);
private
formatErrors
(
errors
:
any
[])
{
const
errors
=
await
validate
(
object
,
{
return
errors
.
map
(
err
=>
{
whitelist
:
true
,
for
(
let
property
in
err
.
constraints
)
{
forbidNonWhitelisted
:
true
,
return
err
.
constraints
[
property
]
});
}
if
(
errors
.
length
>
0
)
{
}).
join
(
"
,
"
);
throw
new
HttpException
(
`Validation failed:
${
this
.
formatErrors
(
errors
)}
`
,
HttpStatus
.
BAD_REQUEST
,
);
}
}
return
value
;
}
private
isEmpty
(
value
:
any
)
{
private
toValidate
(
metatype
:
Function
):
boolean
{
return
(
Object
.
keys
(
value
).
length
>
0
)
?
false
:
true
;
const
types
:
Function
[]
=
[
String
,
Boolean
,
Number
,
Array
,
Object
];
}
return
!
types
.
includes
(
metatype
);
}
}
\ No newline at end of file
private
formatErrors
(
errors
:
any
[])
{
return
errors
.
map
(
err
=>
{
for
(
let
property
in
err
.
constraints
)
{
return
err
.
constraints
[
property
];
}
})
.
join
(
'
,
'
);
}
private
isEmpty
(
value
:
any
)
{
return
Object
.
keys
(
value
).
length
>
0
?
false
:
true
;
}
}
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