Django-rest-framework: تمرر العوامات الكبيرة التحقق من صحة DecimalField بشكل غير صحيح

تم إنشاؤها على ٥ أغسطس ٢٠١٥  ·  4تعليقات  ·  مصدر: encode/django-rest-framework

العوامات الأكبر حجمًا لا تفشل في التحقق من صحة DecimalField .

فمثلا:

>>> serializers.DecimalField(max_digits=3, decimal_places=1).run_validation(200000000000.0)
>>> Decimal('2E+11')

أتوقع زيادة ValidationError لأن هذا الرقم يحتوي على أكثر من 3 أرقام. يلقي التحقق من صحة نموذج Django بالخطأ المتوقع:

>>> forms.DecimalField(max_digits=3, decimal_places=1).clean(200000000000.0)
ValidationError: [u'Ensure that there are no more than 3 digits in total.']

ValidationError إذا قمت بإسقاط صفر من نموذج الإدخال أعلاه:

>>> serializers.DecimalField(max_digits=3, decimal_places=1).run_validation(20000000000.0)
ValidationError: [u'Ensure that there are no more than 3 digits in total.']

وبالمثل ، يظهر الاستثناء أيضًا باستخدام الرقم الأصلي ولكن كـ Decimal و int:

>>> serializers.DecimalField(max_digits=3, decimal_places=1).run_validation(200000000000)
ValidationError: [u'Ensure that there are no more than 3 digits in total.']
>>> serializers.DecimalField(max_digits=3, decimal_places=1).run_validation(Decimal('200000000000.0'))
ValidationError: [u'Ensure that there are no more than 3 digits in total.']

تم تغيير سطر رئيسي مؤخرًا في https://github.com/tomchristie/django-rest-framework/pull/2948 ولكنه لا يبدو صحيحًا وينحرف عن التحقق من صحة Django forms.DecimalField والذي يبدو أنه تم نسخه بخلاف ذلك حرفي:

decimals = exponent * decimal.Decimal(-1) if exponent < 0 else 0

لا أفهم حقًا المشكلة الأصلية التي تم تناولها في # 2948 ، لذا لا أعرف سبب تغيير الخط. يسعدني جدًا العمل على حل المشكلة إذا فهمت المشكلة الأصلية.

يوجد أدناه تصحيح مع اختبار فاشل:

diff --git a/tests/test_fields.py b/tests/test_fields.py
index 0427873..cf41a5b 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -773,6 +773,7 @@ class TestDecimalField(FieldValues):
         (Decimal('Nan'), ["A valid number is required."]),
         (Decimal('Inf'), ["A valid number is required."]),
         ('12.345', ["Ensure that there are no more than 3 digits in total."]),
+        (200000000000.0, ["Ensure that there are no more than 3 digits in total."]),
         ('0.01', ["Ensure that there are no more than 1 decimal places."]),
         (123, ["Ensure that there are no more than 2 digits before the decimal point."])
     )
Bug

التعليق الأكثر فائدة

تم حلها الآن ، مع بعض المنطق الأكثر شفافية ووضوحًا.

ال 4 كومينتر

شكرا ريان.

تم حلها الآن ، مع بعض المنطق الأكثر شفافية ووضوحًا.

tomchristieryankask - أرى وأضيف هذا إلى الوراء إطلاق علامة فارقة في عام 2015. وأنا لا تزال تواجه هذه المشكلة على نسخة 3.11.0 . هل انتهى الإصلاح بإطلاق سراحه؟ الرمز أدناه:

class RecommendationSerializer(serializers.Serializer):
    total_owed = serializers.DecimalField(decimal_places=2, max_digits=8, min_value='1.00',
                                          rounding=ROUND_DOWN)
    term_length = serializers.IntegerField(min_value=1)

    class Meta:
        fields = ['total_owed', 'term_length']


serializer = RecommendationSerializer(data={'total_owed': '12.333333333333333', 'term_length': 6})
serializer.is_valid(raise_exception=True)

>>> {"total_owed": ["Ensure that there are no more than 8 digits in total."]}

Audace تحصل على خطأ التحقق المتوقع. مجموعة المناقشة هي أفضل مكان لإجراء هذه المناقشة وأسئلة الاستخدام الأخرى. شكرا!

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات