pyramid_jsonapi.metadata.JSONSchema package

Module contents

JSONSchema metadata plugin.

This plugin provides JSONSchema schemas and validation.

This module provides 3 metadata views:

  • JSONSchema - The full JSONSchema for JSONAPI (as provided by jsonapi.org)

  • JSONSchema/endpoint/{endpoint} - JSONSchema for a specific endpoint, with attributes defined.

  • JSONSchema/resource/{resource} - JSONSchema of just the attributes for a resource.

Configuration

This plugin uses alchemyjsonschema to provide mapping between pyramid model and JSONSchema types.

alchemyjsonschema provides a default_column_to_schema dictionary, which maps column types to jsonschema types. If you wish to extend this, you can do so by importing this module and modifying this constant prior to importing pyramid_jsonapi. For example, to extend the default mapping to include uuid types:

import alchemyjsonschema

alchemyjsonschema.default_column_to_schema.update(
    {
        sqlalchemy_utils.types.uuid.UUIDType: "string"
    }
)

jsonapi = pyramid_jsonapi.PyramidJSONAPI(config, models)

Endpoint View

The endpoint view expects the path to include the endpoint in question, and 3 query parameters must be provided:

  • method - http method (GET, POST etc)

  • direction - ‘request’ or ‘response’

  • code - http status code (if direction is ‘response’)

For example:

https://localhost:6543/metadata/JSONSchema/endpoint/people?method=get&direction=response&code=200

Will return the schema that matches a valid response (200 OK) to a GET to /api/people

class pyramid_jsonapi.metadata.JSONSchema.JSONSchema(api)[source]

Bases: object

Metadata plugin to generate and validate JSONSchema for sqlalchemy, using alchemyjsonschema to map sqlalchemy types.

build_definitions()[source]

Build data and attribute references for all endpoints, and updates the ‘global’ schema.

endpoint_schema(endpoint, method, direction, code)[source]

Generate a full schema for an endpoint.

Parameters
  • endpoint (string) – Endpoint name

  • method (string) – http method (defaults to ‘get’)

  • direction (string) – request or response (defaults to response)

  • code (string) – http status code

Returns

JSONSchema (dict)

endpoint_schema_view(request)[source]

Pyramid view for endpoint_schema.

Parameters

request – Pyramid Request object.

Returns

Results of endpoint_schema() method call.

Raises

HTTPNotFound error for unknown endpoints.

Takes 1 path parameert:
  • ‘endpoint’

Takes 3 optional query parameters:
  • ‘method’: http method (defaults to get)

  • ‘direction’: request or response (defaults to response)

  • ‘code’: http status code

load_schema()[source]

Load the JSONAPI jsonschema from file.

Reads ‘pyramid_jsonapi.schema_file’ from config, or defaults to one provided with the package.

resource_attributes(endpoint)[source]

Return jsonschema attributes for a specific resource.

Parameters

endpoint (str) – endpoint to obtain schema for.

Returns

Dictionary containing jsonschema attributes for the endpoint.

resource_attributes_view(request)[source]

Call resource() via a pyramid view.

Parameters

request – Pyramid Request object.

Returns

Results of resource_attributes() method call.

Raises

HTTPNotFound error for unknown endpoints.

template(request=None)[source]

Return the JSONAPI jsonschema dict (as a pyramid view).

Parameters

request (optional) – Pyramid Request object.

Returns

JSONAPI schema document.

validate(json_body, method='get')[source]

Validate schema against jsonschema.