Load and Validate Schemas

Module to load and validate GCP-related JSON schemas.

Schema file discovery is based on any JSON file in the gordon_gcp/schema/schemas/ directory.

For more information on this plugin’s schemas, see Schemas for Event Consuming.

Warning

The following documentation may change since the calling/using of the this module is not yet incorporated into the plugin logic.

Schemas are loaded once at service startup. A JSON message/object is validated against a given loaded schema upon receipt of message/object. The schema to validate against is determined by the topic from which the PubSub message is pulled.

To use:

>>> from gordon_gcp.schema import validate
>>> validator = validate.MessageValidator()
>>> validator.schemas
{
    'event': {
        'title': 'Generic Event Message',
        'type': 'object',
        'required': ...},
    'audit-log': {
        'title': 'Google Audit Log Message',
        'type': 'object',
        'required': ...}
}
>>> example_message = {'foo': 'bar'}
>>> validator.validate(example_message, 'event')
class gordon_gcp.schema.validate.MessageValidator[source]

Load packaged JSON schemas and validate a given JSON message.

schemas

dict – schema name based on filename mapped to its loaded JSON schema.

Raises:GCPGordonError – if unable to find or load schemas.
validate(message, schema_name)[source]

Validate a message given a schema.

Parameters:
  • message (dict) – Loaded JSON of pulled message from Google PubSub.
  • schema_name (str) – Name of schema to validate message against. schema_name will be used to look up schema from MessageValidator.schemas dict
Raises:
  • InvalidMessageError – if message is invalid against the given schema.
  • InvalidMessageError – if given schema name can not be found.