← 返回题库
中级

实现JSON Schema简化版验证

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    raw_requests_csv = open_url(____).read()
    import json
    USER_SCHEMA = {
        '____': '____',
        '____': ['____','____','____'],
        '____': {
            '____': {'____': '____', '____': ____},
            '____': {'____': '____'},
            '____': {'____': '____'},
            '____': {'____': '____', '____': ____, '____': ____},
        }
    }
    TYPE_MAP = {'____': str, '____': int, '____': float, '____': bool, '____': list, '____': dict}
    def validate_schema(____):
        errors = []
        for req in schema.get(____):
            if req not in data: errors.append(____)
        for field, spec in schema.get(____).items():
            val = data.get(____)
            if val is None: continue
            expected = TYPE_MAP.get(spec.get(____))
            if expected and not isinstance(____):
                errors.append(____)
            if isinstance(____) and '____' in spec and len(____) < spec['____']:
                errors.append(____)
            if isinstance(____):
                if '____' in spec and val < spec['____']: errors.append(____)
                if '____' in spec and val > spec['____']: errors.append(____)
        return errors
    bodies = [
        {'____':'____','____':'____','____':'____','____':____},
        {'____':'____','____':'____'},
        {'____':'____','____':'____','____':'____','____':____},
    ]
    for b in bodies:
        errs = validate_schema(____)
        print(____)

示例

输入
solve()
期望输出
data=['username', 'email', 'phone', 'age']: VALID
data=['email', 'phone']: ['missing required: username']
data=['username', 'email', 'phone', 'age']: ['username: minLength=3', 'age: > maximum']
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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