ai_v/venv/Lib/site-packages/botocore/validate.py

385 lines
13 KiB
Python
Raw Normal View History

feat(api): 实现图像生成及后台同步功能 - 新增图像生成接口,支持试用、积分和自定义API Key模式 - 实现生成图片结果异步上传至MinIO存储,带重试机制 - 优化积分预扣除和异常退还逻辑,保障用户积分准确 - 添加获取生成历史记录接口,支持时间范围和分页 - 提供本地字典配置接口,支持模型、比例、提示模板和尺寸 - 实现图片批量上传接口,支持S3兼容对象存储 feat(admin): 增加管理员角色管理与权限分配接口 - 实现角色列表查询、角色创建、更新及删除功能 - 增加权限列表查询接口 - 实现用户角色分配接口,便于统一管理用户权限 - 增加系统字典增删查改接口,支持分类过滤和排序 - 权限控制全面覆盖管理接口,保证安全访问 feat(auth): 完善用户登录注册及权限相关接口与页面 - 实现手机号验证码发送及校验功能,保障注册安全 - 支持手机号注册、登录及退出接口,集成日志记录 - 增加修改密码功能,验证原密码后更新 - 提供动态导航菜单接口,基于权限展示不同菜单 - 实现管理界面路由及日志、角色、字典管理页面访问权限控制 - 添加系统日志查询接口,支持关键词和等级筛选 feat(app): 初始化Flask应用并配置蓝图与数据库 - 创建应用程序工厂,加载配置,初始化数据库和Redis客户端 - 注册认证、API及管理员蓝图,整合路由 - 根路由渲染主页模板 - 应用上下文中自动创建数据库表,保证运行环境准备完毕 feat(database): 提供数据库创建与迁移支持脚本 - 新增数据库创建脚本,支持自动检测是否已存在 - 添加数据库表初始化脚本,支持创建和删除所有表 - 实现RBAC权限初始化,包含基础权限和角色创建 - 新增字段手动修复脚本,添加用户API Key和积分字段 - 强制迁移脚本支持清理连接和修复表结构,初始化默认数据及角色分配 feat(config): 新增系统配置参数 - 配置数据库、Redis、Session和MinIO相关参数 - 添加AI接口地址及试用Key配置 - 集成阿里云短信服务配置及开发模式相关参数 feat(extensions): 初始化数据库、Redis和MinIO客户端 - 创建全局SQLAlchemy数据库实例和Redis客户端 - 配置基于boto3的MinIO兼容S3客户端 chore(logs): 添加示例系统日志文件 - 记录用户请求、验证码发送成功与失败的日志信息
2026-01-12 00:53:31 +08:00
"""User input parameter validation.
This module handles user input parameter validation
against a provided input model.
Note that the objects in this module do *not* mutate any
arguments. No type version happens here. It is up to another
layer to properly convert arguments to any required types.
Validation Errors
-----------------
"""
import decimal
import json
from datetime import datetime
from botocore.exceptions import ParamValidationError
from botocore.utils import is_json_value_header, parse_to_aware_datetime
def validate_parameters(params, shape):
"""Validates input parameters against a schema.
This is a convenience function that validates parameters against a schema.
You can also instantiate and use the ParamValidator class directly if you
want more control.
If there are any validation errors then a ParamValidationError
will be raised. If there are no validation errors than no exception
is raised and a value of None is returned.
:param params: The user provided input parameters.
:type shape: botocore.model.Shape
:param shape: The schema which the input parameters should
adhere to.
:raise: ParamValidationError
"""
validator = ParamValidator()
report = validator.validate(params, shape)
if report.has_errors():
raise ParamValidationError(report=report.generate_report())
def type_check(valid_types):
def _create_type_check_guard(func):
def _on_passes_type_check(self, param, shape, errors, name):
if _type_check(param, errors, name):
return func(self, param, shape, errors, name)
def _type_check(param, errors, name):
if not isinstance(param, valid_types):
valid_type_names = [str(t) for t in valid_types]
errors.report(
name,
'invalid type',
param=param,
valid_types=valid_type_names,
)
return False
return True
return _on_passes_type_check
return _create_type_check_guard
def range_check(name, value, shape, error_type, errors):
failed = False
min_allowed = float('-inf')
if 'min' in shape.metadata:
min_allowed = shape.metadata['min']
if value < min_allowed:
failed = True
elif hasattr(shape, 'serialization'):
# Members that can be bound to the host have an implicit min of 1
if shape.serialization.get('hostLabel'):
min_allowed = 1
if value < min_allowed:
failed = True
if failed:
errors.report(name, error_type, param=value, min_allowed=min_allowed)
class ValidationErrors:
def __init__(self):
self._errors = []
def has_errors(self):
if self._errors:
return True
return False
def generate_report(self):
error_messages = []
for error in self._errors:
error_messages.append(self._format_error(error))
return '\n'.join(error_messages)
def _format_error(self, error):
error_type, name, additional = error
name = self._get_name(name)
if error_type == 'missing required field':
return (
f"Missing required parameter in {name}: "
f"\"{additional['required_name']}\""
)
elif error_type == 'unknown field':
unknown_param = additional['unknown_param']
valid_names = ', '.join(additional['valid_names'])
return (
f'Unknown parameter in {name}: "{unknown_param}", '
f'must be one of: {valid_names}'
)
elif error_type == 'invalid type':
param = additional['param']
param_type = type(param)
valid_types = ', '.join(additional['valid_types'])
return (
f'Invalid type for parameter {name}, value: {param}, '
f'type: {param_type}, valid types: {valid_types}'
)
elif error_type == 'invalid range':
param = additional['param']
min_allowed = additional['min_allowed']
return (
f'Invalid value for parameter {name}, value: {param}, '
f'valid min value: {min_allowed}'
)
elif error_type == 'invalid length':
param = additional['param']
min_allowed = additional['min_allowed']
return (
f'Invalid length for parameter {name}, value: {param}, '
f'valid min length: {min_allowed}'
)
elif error_type == 'unable to encode to json':
return 'Invalid parameter {} must be json serializable: {}'.format(
name,
additional['type_error'],
)
elif error_type == 'invalid type for document':
param = additional['param']
param_type = type(param)
valid_types = ', '.join(additional['valid_types'])
return (
f'Invalid type for document parameter {name}, value: {param}, '
f'type: {param_type}, valid types: {valid_types}'
)
elif error_type == 'more than one input':
members = ', '.join(additional['members'])
return (
f'Invalid number of parameters set for tagged union structure '
f'{name}. Can only set one of the following keys: '
f'{members}.'
)
elif error_type == 'empty input':
members = ', '.join(additional['members'])
return (
f'Must set one of the following keys for tagged union'
f'structure {name}: {members}.'
)
def _get_name(self, name):
if not name:
return 'input'
elif name.startswith('.'):
return name[1:]
else:
return name
def report(self, name, reason, **kwargs):
self._errors.append((reason, name, kwargs))
class ParamValidator:
"""Validates parameters against a shape model."""
def validate(self, params, shape):
"""Validate parameters against a shape model.
This method will validate the parameters against a provided shape model.
All errors will be collected before returning to the caller. This means
that this method will not stop at the first error, it will return all
possible errors.
:param params: User provided dict of parameters
:param shape: A shape model describing the expected input.
:return: A list of errors.
"""
errors = ValidationErrors()
self._validate(params, shape, errors, name='')
return errors
def _check_special_validation_cases(self, shape):
if is_json_value_header(shape):
return self._validate_jsonvalue_string
if shape.type_name == 'structure' and shape.is_document_type:
return self._validate_document
def _validate(self, params, shape, errors, name):
special_validator = self._check_special_validation_cases(shape)
if special_validator:
special_validator(params, shape, errors, name)
else:
getattr(self, f'_validate_{shape.type_name}')(
params, shape, errors, name
)
def _validate_jsonvalue_string(self, params, shape, errors, name):
# Check to see if a value marked as a jsonvalue can be dumped to
# a json string.
try:
json.dumps(params)
except (ValueError, TypeError) as e:
errors.report(name, 'unable to encode to json', type_error=e)
def _validate_document(self, params, shape, errors, name):
if params is None:
return
if isinstance(params, dict):
for key in params:
self._validate_document(params[key], shape, errors, key)
elif isinstance(params, list):
for index, entity in enumerate(params):
self._validate_document(
entity, shape, errors, f'{name}[{index}]'
)
elif not isinstance(params, ((str,), int, bool, float)):
valid_types = (str, int, bool, float, list, dict)
valid_type_names = [str(t) for t in valid_types]
errors.report(
name,
'invalid type for document',
param=params,
param_type=type(params),
valid_types=valid_type_names,
)
@type_check(valid_types=(dict,))
def _validate_structure(self, params, shape, errors, name):
if shape.is_tagged_union:
if len(params) == 0:
errors.report(name, 'empty input', members=shape.members)
elif len(params) > 1:
errors.report(
name, 'more than one input', members=shape.members
)
# Validate required fields.
for required_member in shape.metadata.get('required', []):
if required_member not in params:
errors.report(
name,
'missing required field',
required_name=required_member,
user_params=params,
)
members = shape.members
known_params = []
# Validate known params.
for param in params:
if param not in members:
errors.report(
name,
'unknown field',
unknown_param=param,
valid_names=list(members),
)
else:
known_params.append(param)
# Validate structure members.
for param in known_params:
self._validate(
params[param],
shape.members[param],
errors,
f'{name}.{param}',
)
@type_check(valid_types=(str,))
def _validate_string(self, param, shape, errors, name):
# Validate range. For a string, the min/max constraints
# are of the string length.
# Looks like:
# "WorkflowId":{
# "type":"string",
# "min":1,
# "max":256
# }
range_check(name, len(param), shape, 'invalid length', errors)
@type_check(valid_types=(list, tuple))
def _validate_list(self, param, shape, errors, name):
member_shape = shape.member
range_check(name, len(param), shape, 'invalid length', errors)
for i, item in enumerate(param):
self._validate(item, member_shape, errors, f'{name}[{i}]')
@type_check(valid_types=(dict,))
def _validate_map(self, param, shape, errors, name):
key_shape = shape.key
value_shape = shape.value
for key, value in param.items():
self._validate(key, key_shape, errors, f"{name} (key: {key})")
self._validate(value, value_shape, errors, f'{name}.{key}')
@type_check(valid_types=(int,))
def _validate_integer(self, param, shape, errors, name):
range_check(name, param, shape, 'invalid range', errors)
def _validate_blob(self, param, shape, errors, name):
if isinstance(param, (bytes, bytearray, str)):
return
elif hasattr(param, 'read'):
# File like objects are also allowed for blob types.
return
else:
errors.report(
name,
'invalid type',
param=param,
valid_types=[str(bytes), str(bytearray), 'file-like object'],
)
@type_check(valid_types=(bool,))
def _validate_boolean(self, param, shape, errors, name):
pass
@type_check(valid_types=(float, decimal.Decimal) + (int,))
def _validate_double(self, param, shape, errors, name):
range_check(name, param, shape, 'invalid range', errors)
_validate_float = _validate_double
@type_check(valid_types=(int,))
def _validate_long(self, param, shape, errors, name):
range_check(name, param, shape, 'invalid range', errors)
def _validate_timestamp(self, param, shape, errors, name):
# We don't use @type_check because datetimes are a bit
# more flexible. You can either provide a datetime
# object, or a string that parses to a datetime.
is_valid_type = self._type_check_datetime(param)
if not is_valid_type:
valid_type_names = [str(datetime), 'timestamp-string']
errors.report(
name, 'invalid type', param=param, valid_types=valid_type_names
)
def _type_check_datetime(self, value):
try:
parse_to_aware_datetime(value)
return True
except (TypeError, ValueError, AttributeError):
# Yes, dateutil can sometimes raise an AttributeError
# when parsing timestamps.
return False
class ParamValidationDecorator:
def __init__(self, param_validator, serializer):
self._param_validator = param_validator
self._serializer = serializer
def serialize_to_request(self, parameters, operation_model):
input_shape = operation_model.input_shape
if input_shape is not None:
report = self._param_validator.validate(
parameters, operation_model.input_shape
)
if report.has_errors():
raise ParamValidationError(report=report.generate_report())
return self._serializer.serialize_to_request(
parameters, operation_model
)