Load the schema from a given record.

    Args:
        data (dict): record data.
        schema (Union[dict, str]): schema to validate against.

    Returns:
        dict: the loaded schema.

    Raises:
        SchemaNotFound: if the given schema was not found.
        SchemaKeyNotFound: if ``schema`` is ``None`` and no ``$schema`` key was
            found in ``data``.
        jsonschema.SchemaError: if the schema is invalid.

def _load_schema_for_record(data, schema=None):
    """Load the schema from a given record.

    Args:
        data (dict): record data.
        schema (Union[dict, str]): schema to validate against.

    Returns:
        dict: the loaded schema.

    Raises:
        SchemaNotFound: if the given schema was not found.
        SchemaKeyNotFound: if ``schema`` is ``None`` and no ``$schema`` key was
            found in ``data``.
        jsonschema.SchemaError: if the schema is invalid.
    """
    if schema is None:
        if '$schema' not in data:
            raise SchemaKeyNotFound(data=data)
        schema = data['$schema']

    if isinstance(schema, six.string_types):
        schema = load_schema(schema_name=schema)
    return schema