<commit_before># -*- coding: utf-8 -*-
"""
    flask_jsonschema
    ~~~~~~~~~~~~~~~~

    flask_jsonschema
"""

import os

from functools import wraps

try:
    import simplejson as json
except ImportError:
    import json

from flask import current_app, request
from jsonschema import ValidationError, validate


class _JsonSchema(object):
    def __init__(self, schemas):
        self._schemas = schemas

    def get_schema(self, path):
        rv = self._schemas[path[0]]
        for p in path[1:]:
            rv = rv[p]
        return rv


class JsonSchema(object):
    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self._state = self.init_app(app)

    def init_app(self, app):
        default_dir = os.path.join(app.root_path, 'jsonschema')
        schema_dir = app.config.get('JSONSCHEMA_DIR', default_dir)
        schemas = {}
        for fn in os.listdir(schema_dir):
            key = fn.split('.')[0]
            fn = os.path.join(schema_dir, fn)
            if os.path.isdir(fn) or not fn.endswith('.json'):
                continue
            with open(fn) as f:
                schemas[key] = json.load(f)
        state = _JsonSchema(schemas)
        app.extensions['jsonschema'] = state
        return state

    def validate(self, *path):
        def wrapper(fn):
            @wraps(fn)
            def decorated(*args, **kwargs):
                schema = current_app.extensions['jsonschema'].get_schema(path)
                validate(request.json, schema)
                return fn(*args, **kwargs)
            return decorated
        return wrapper

    def __getattr__(self, name):
        return getattr(self._state, name, None)<commit_msg>OpenAPI Spec based JSON schema validation.<commit_after># -*- coding: utf-8 -*-
"""
    flask_jsonschema
    ~~~~~~~~~~~~~~~~

    flask_jsonschema
"""

import os

from functools import wraps

try:
    import simplejson as json
except ImportError:
    import json

from flask import current_app, request
from jsonschema import ValidationError, validate


class _JsonSchema(object):
    def __init__(self, schemas):
        self._schemas = schemas

    def get_schema(self, schema_file, schema_path=None):
        try:
            return self._schemas[schema_file][schema_path]
        except KeyError:
            raise ValidationError("Matching schema not found")

    def get_oas_schema(self, schema_file, uri_path, method):
        for parameter in self._schemas[schema_file]['paths'][uri_path][method]["parameters"]:
            if parameter.get('in', '') == 'body':
                parameter['schema']['definitions'] = self._schemas[schema_file]['definitions']
                return parameter['schema']
        raise ValidationError("Matching schema not found")


class JsonSchema(object):
    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self._state = self.init_app(app)

    def init_app(self, app):
        default_dir = os.path.join(app.root_path, 'jsonschema')
        schema_dir = app.config.get('JSONSCHEMA_DIR', default_dir)
        schemas = {}
        for fn in os.listdir(schema_dir):
            key = fn.split('.')[0]
            fn = os.path.join(schema_dir, fn)
            if os.path.isdir(fn) or not fn.endswith('.json'):
                continue
            with open(fn) as f:
                schemas[key] = json.load(f)
        state = _JsonSchema(schemas)
        app.extensions['jsonschema'] = state
        return state

    def validate(self, *path):
        """
        Backwards compatibility
        """
        return validate_request(*path)

    def __getattr__(self, name):
        return getattr(self._state, name, None)


def validate_request(*path):
    """
    Validate request body's JSON against JSON schema document

    Args:
        json_file (string): file name of the OAS json file, without .json suffix
        path      (string): Name of nested jsonschema object in file

    Example:
        @app.route('/foo/<param>/bar', methods=['POST'])
        @validate_request_json('schema', 'foo-post')
        def foo(param):
            ...
    """
    def wrapper(fn):
        @wraps(fn)
        def decorated(*args, **kwargs):
            schema = current_app.extensions['jsonschema'].get_schema(*path)
            validate(request.json, schema)
            return fn(*args, **kwargs)
        return decorated
    return wrapper

def validate_request_oas(*path):
    """
    Validate request body's JSON against JSON schema in OpenAPI Specification

    Args:
        json_file (string): file name of the OAS json file, without .json suffix
        path      (string): OAS style application path http://goo.gl/2FHaAw
        method    (string): OAS style method (get/post..) http://goo.gl/P7LNCE

    Example:
        @app.route('/foo/<param>/bar', methods=['POST'])
        @validate_request_oas('oas', '/foo/{param}/bar', "post")
        def foo(param):
            ...
    """
    def wrapper(fn):
        @wraps(fn)
        def decorated(*args, **kwargs):
            schema = current_app.extensions['jsonschema'].get_oas_schema(*path)
            validate(request.json, schema)
            return fn(*args, **kwargs)
        return decorated
    return wrapper
{'fraction_non_alphanumeric': 0.11958997722095673, 'fraction_numerical': 0.0015186028853454822, 'mean_word_length': 2.3096733668341707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7348570', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1510, 'n_words': 389}