def json_schema_validator(request_schema=None, response_schema=None):
    """
    Validate your request & response payloads against a JSONSchema.

    *NOTE: depends on the* `jsonschema <https://github.com/Julian/jsonschema>`_
    *package. If you're using* `serverless-python-requirements <https://github.com/UnitedIncome/serverless-python-requirements>`_
    *you're all set. If you cURLed* ``lambda_decorators.py`` *you'll have to
    install it manually in your service's root directory.*

    Usage::

      >>> from jsonschema import ValidationError
      >>> from lambda_decorators import json_schema_validator
      >>> @json_schema_validator(request_schema={
      ... 'type': 'object', 'properties': {'price': {'type': 'number'}}})
      ... def handler(event, context):
      ...     return event['price']
      >>> handler({'price': 'bar'}, object())
      {'statusCode': 400, 'body': "RequestValidationError: 'bar' is not of type 'number'"}
      >>> @json_schema_validator(response_schema={
      ... 'type': 'object', 'properties': {'price': {'type': 'number'}}})
      ... def handler(event, context):
      ...     return {'price': 'bar'}
      >>> handler({}, object())
      {'statusCode': 500, 'body': "ResponseValidationError: 'bar' is not of type 'number'"}
    """
    def wrapper_wrapper(handler):
        @wraps(handler)
        def wrapper(event, context):
            if request_schema is not None:
                if jsonschema is None:
                    logger.error('jsonschema is not installed, skipping request validation')
                else:
                    try:
                        jsonschema.validate(event, request_schema)
                    except jsonschema.ValidationError as exception:
                        return {'statusCode': 400,
                                'body': 'RequestValidationError: {}'.format(
                                    exception.message)}
            response = handler(event, context)
            if response_schema is not None:
                if jsonschema is None:
                    logger.error('jsonschema is not installed, skipping response validation')
                else:
                    try:
                        jsonschema.validate(response, response_schema)
                    except jsonschema.ValidationError as exception:
                        return {'statusCode': 500,
                                'body': 'ResponseValidationError: {}'.format(
                                    exception.message)}
            return response

        return wrapper

    return wrapper_wrapper
def json_schema_validator ( request_schema = None , response_schema = None ) : def wrapper_wrapper ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : if request_schema is not None : if jsonschema is None : logger . error ( 'jsonschema is not installed, skipping request validation' ) else : try : jsonschema . validate ( event , request_schema ) except jsonschema . ValidationError as exception : return { 'statusCode' : 400 , 'body' : 'RequestValidationError: {}' . format ( exception . message ) } response = handler ( event , context ) if response_schema is not None : if jsonschema is None : logger . error ( 'jsonschema is not installed, skipping response validation' ) else : try : jsonschema . validate ( response , response_schema ) except jsonschema . ValidationError as exception : return { 'statusCode' : 500 , 'body' : 'ResponseValidationError: {}' . format ( exception . message ) } return response return wrapper return wrapper_wrapper
Validate your request & response payloads against a JSONSchema.

    *NOTE: depends on the* `jsonschema <https://github.com/Julian/jsonschema>`_
    *package. If you're using* `serverless-python-requirements <https://github.com/UnitedIncome/serverless-python-requirements>`_
    *you're all set. If you cURLed* ``lambda_decorators.py`` *you'll have to
    install it manually in your service's root directory.*

    Usage::

      >>> from jsonschema import ValidationError
      >>> from lambda_decorators import json_schema_validator
      >>> @json_schema_validator(request_schema={
      ... 'type': 'object', 'properties': {'price': {'type': 'number'}}})
      ... def handler(event, context):
      ...     return event['price']
      >>> handler({'price': 'bar'}, object())
      {'statusCode': 400, 'body': "RequestValidationError: 'bar' is not of type 'number'"}
      >>> @json_schema_validator(response_schema={
      ... 'type': 'object', 'properties': {'price': {'type': 'number'}}})
      ... def handler(event, context):
      ...     return {'price': 'bar'}
      >>> handler({}, object())
      {'statusCode': 500, 'body': "ResponseValidationError: 'bar' is not of type 'number'"}