"""JSON Draft3 Schema Validators. A collection of json_schema.Draft3Validator classes loaded with the different endpoint validation schemas for our API """ import re from jsonschema import Draft3Validator from artist import config def raml_header_to_json_schema(raml_headers): """Convert headers to JSON Draft 3 schema. Takes RAML specs for headers for an endpoint and converts to JSON Draft 3 schema to use with our validator NOTE: we have to filter out props with None as the value :( Args: raml_headers (collections.OrderedDict): the headers part of a RAML endpoint descriptor. Returns: dict: the dict representing JSON validation schema snippet. """ properties = {} for item in raml_headers: raw_props = raml_headers[item] props = {} for prop in raw_props.__dict__: if raw_props.__dict__[prop] is not None: props[prop] = raw_props.__dict__[prop] properties[item] = props schema = { '$schema': 'http://json-schema.org/draft-03/schema', 'type': 'object', 'required': True, 'properties': properties } return schema def get_method_spec(resource_string, method): """Extract the section of the RAML spec for the given resource and method. Args: resource_string (str): resource name in the RAML spec. method (str): method name in the RAML spec. Returns: pyraml.entities.RamlMethod: spec for the given resource and method. """ method_spec = config.API_DEFINITION resource_regex = r'(/{?\w+}?)' resources = re.findall(resource_regex, resource_string) for resource in resources: method_spec = method_spec.resources[resource] return method_spec.methods[method] def query_args_validator(resource, method): """Construct a JSON args validator. JSON validator is based on the query parameters defined in the RAML spec for a particular resource and method. Args: resource (str): resource name in the RAML spec. method (str): method name in the RAML spec. Returns: Draft3Validator: schema validator object for query args """ method_spec = get_method_spec(resource, method) return Draft3Validator( raml_header_to_json_schema(method_spec.queryParameters)) def headers_validator(resource, method): """Construct a JSON header validator. JSON validator is based on the query parameters defined in the RAML spec for a particular resource and method. Args: resource (str): resource name in the RAML spec. method (str): method name in the RAML spec. Returns: Draft3Validator: schema validator object for headers """ method_spec = get_method_spec(resource, method) return Draft3Validator( raml_header_to_json_schema(method_spec.headers)) def body_validator(resource, method): """Construct a JSON body validator. JSON Validator is based on the the request body schema defined in the RAML spec for a particular resource and method (method will generally be POST or PUT). Args: resource (str): resource name in the RAML spec. method (str): method name in the RAML spec. Returns: Draft3Validator: schema validator object for request body """ method_spec = get_method_spec(resource, method) return Draft3Validator(method_spec.body['application/json'].schema)