jsonschema.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:deb-python-falcon 作者: openstack 项目源码 文件源码
def validate(schema):
    """Decorator that validates ``req.media`` using JSON Schema

    Args:
        schema (dict): A dictionary that follows the JSON Schema specification.
            See `json-schema.org <http://json-schema.org/>`_ for more
            information on defining a compatible dictionary.

    Example:
        .. code:: python

            from falcon.media.validators import jsonschema

            # -- snip --

            @jsonschema.validate(my_post_schema)
            def on_post(self, req, resp):

            # -- snip --

    Note:
        This validator requires the ``jsonschema`` library available via
        PyPI. The library also requires Python 2.7+.
    """
    def decorator(func):
        def wrapper(self, req, resp, *args, **kwargs):
            try:
                jsonschema.validate(req.media, schema)
            except jsonschema.ValidationError as e:
                raise falcon.HTTPBadRequest(
                    'Failed data validation',
                    description=e.message
                )

            return func(self, req, resp, *args, **kwargs)
        return wrapper
    return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号