Commit 4a48c1ce authored by Florent Chehab's avatar Florent Chehab

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
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)
......
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)
......
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")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment