← 返回题库
中级

实现嵌套对象验证

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    raw_requests_csv = open_url(____).read()
    import json
    ORDER_SCHEMA = {
        '____': {'____': int, '____': True, '____': ____},
        '____': {'____': list, '____': True, '____': ____},
        '____': {'____': str, '____': True},
    }
    ITEM_SCHEMA = {
        '____': {'____': str, '____': True},
        '____': {'____': int, '____': True, '____': ____},
    }
    def validate_obj(____):
        errors = []
        for field, rules in schema.items():
            val = obj.get(____)
            if rules.get(____) and val is None:
                errors.append(____); continue
            if val is not None and not isinstance(____):
                errors.append(____); continue
            if isinstance(____) and '____' in rules and val < rules['____']:
                errors.append(____)
            if isinstance(____) and '____' in rules and len(____) < rules['____']:
                errors.append(____)
        return errors
    orders = [
        {'____':____,'____':[{'____':'____','____':____}],'____':'____'},
        {'____':____,'____':[],'____':'____'},
        {'____':____,'____':[{'____':'____'}]},
    ]
    for i, order in enumerate(____):
        errs = validate_obj(____)
        for item in order.get(____):
            errs += validate_obj(____)
        print(____)

示例

输入
solve()
期望输出
order[1]: VALID
order[2]: INVALID: ['user_id: < min=1', 'items: too short']
order[3]: INVALID: ['idempotency_key: required', 'items[0].qty: required']
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码