Exemple #1
0
def dict_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    if field_data is None:
        return None
    if not isinstance(field_data, dict):
        raise ValidationError('参数错误, %s需要是dict类型' % field)
    return field_data
Exemple #2
0
def date_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    try:
        return datetime_help.parse_date(field_data) if (field_data
                                                        is not None) else None
    except:
        raise ValidationError('参数错误, %s需要是ISO_8601格式的日期字符串' % field)
Exemple #3
0
def bool_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    if str(field_data).lower() == 'true':
        return True
    if str(field_data).lower() == 'false':
        return False
    raise ValidationError('参数错误, %s需要是bool类型' % field)
Exemple #4
0
def _field(data, field=None, required=True):
    data = {} if data is None else data
    field_data = data.get(field) if field else data

    if required and field_data is None:
        raise ValidationError('参数错误, %s为必填参数' % field)
    if data is None:
        return None
    return field_data
Exemple #5
0
def float_field(data, field=None, required=True):
    field_data = _field(data, field, required)
    try:
        return float(field_data) if (field_data is not None) else None
    except:
        raise ValidationError('参数错误, %s需要是float类型' % field)
Exemple #6
0
 def validate(self):
     if not self.validate_data.get('user_id'):
         raise ValidationError('user_id不能为空')
     return self.validate_data