serialization.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. from marshmallow import Schema, fields, validates_schema, validates
  2. from common.error_types import ApiException
  3. from .models import AnalysisTask
  4. class BooleanField(fields.Boolean):
  5. def _deserialize(self, value, attr, data, **kwargs):
  6. # 进行自定义验证
  7. if not isinstance(value, bool):
  8. raise ApiException(code=400, msg="isOcr not a valid boolean", msgZH="isOcr不是有效的布尔值")
  9. return value
  10. class AnalysisViewSchema(Schema):
  11. fileKey = fields.Str(required=True)
  12. fileName = fields.Str()
  13. taskType = fields.Str(required=True)
  14. isOcr = BooleanField()
  15. @validates_schema(pass_many=True)
  16. def validate_passwords(self, data, **kwargs):
  17. task_type = data['taskType']
  18. file_key = data['fileKey']
  19. if not file_key:
  20. raise ApiException(code=400, msg="fileKey cannot be empty", msgZH="fileKey不能为空")
  21. if not task_type:
  22. raise ApiException(code=400, msg="taskType cannot be empty", msgZH="taskType不能为空")