Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
4a48c1ce
Commit
4a48c1ce
authored
Aug 20, 2018
by
Florent Chehab
Browse files
Validation error changed from djago to REST framework to prevent crashs
Errors are returned as JSON when used with API
parent
7fc417da
Pipeline
#26331
passed with stages
in 1 minute and 23 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
rex/models/module/scholarship.py
View file @
4a48c1ce
from
django.db
import
models
from
rex.models.module
import
BasicModule
from
rex.models.tools
import
Currency
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
serializers
from
django.core.validators
import
MinValueValidator
...
...
@@ -24,7 +24,7 @@ class Scholarship(BasicModule):
# Need custom validation
def
clean
(
self
,
*
args
,
**
kwargs
):
if
self
.
amount_max
<
self
.
amount_min
:
raise
ValidationError
(
raise
serializers
.
ValidationError
(
"Amount_max should be greater or equal than amount_min"
)
super
(
Scholarship
,
self
).
clean
(
*
args
,
**
kwargs
)
...
...
rex/models/tools/usefullLinksField.py
View file @
4a48c1ce
from
django.contrib.postgres.fields
import
JSONField
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
serializers
from
rex.models.tools.validateWithRestFramework
import
validate_with_rest_framework
...
...
@@ -21,7 +19,8 @@ def validate_usefull_links(value):
"""
if
type
(
value
)
is
not
list
:
raise
ValidationError
(
"Usefull links must be a JSON array !"
)
raise
serializers
.
ValidationError
(
"Usefull links must be a JSON array !"
)
for
obj
in
value
:
validate_with_rest_framework
(
UrlAndDescriptionSerializer
,
obj
)
...
...
rex/models/tools/validateWithRestFramework.py
View file @
4a48c1ce
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
serializers
def
validate_with_rest_framework
(
serializer
,
value
):
...
...
@@ -10,11 +10,12 @@ def validate_with_rest_framework(serializer, value):
valid_ser
=
serializer
(
data
=
value
)
if
not
valid_ser
.
is_valid
():
raise
ValidationError
(
str
(
valid_ser
.
errors
))
raise
serializers
.
ValidationError
(
str
(
valid_ser
.
errors
))
# Also checks that no extra fields were added
if
type
(
value
)
is
not
list
:
allowed_keys
=
list
(
valid_ser
.
get_fields
())
for
key
in
value
.
keys
():
if
key
not
in
allowed_keys
:
raise
ValidationError
(
"Expected JSON schema not respected"
)
raise
serializers
.
ValidationError
(
"Expected JSON schema not respected"
)
Write
Preview
Supports
Markdown
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