diff --git a/apps/expertproducts/views.py b/apps/expertproducts/views.py index dc725b0..5e54efa 100644 --- a/apps/expertproducts/views.py +++ b/apps/expertproducts/views.py @@ -48,11 +48,26 @@ class ContentAnalysisAPI(APIView): ] for field in required_fields: - if field not in data: + value = data.get(field) + if value is None or (isinstance(value, str) and value.strip() == ""): return Response( - {"error": f"Field '{field}' is required."}, + {"error": f"字段 '{field}' 不能为空"}, status=status.HTTP_400_BAD_REQUEST ) + # 新增校验:price和stock必须为数字且>=0 + if field in ["product_info.price", "product_info.stock"]: + try: + num_value = float(value) + if num_value < 0: + return Response( + {"error": f"'{field}' 不合法"}, + status=status.HTTP_400_BAD_REQUEST + ) + except (ValueError, TypeError): + return Response( + {"error": f"'{field}' 不合法"}, + status=status.HTTP_400_BAD_REQUEST + ) # 3. 解析 JSON 字段 try: