coarnotify.validate

This module provides a set of validation functions that can be used to validate properties on objects. It also contains a Validator class which is used to wrap the protocol-wide validation rules which are shared across all objects.

Classes

Validator

A wrapper around a set of validation rules which can be used to select the appropriate validator

Functions

absolute_uri(→ bool)

Validate that the given string is an absolute URI

at_least_one_of(→ Callable)

Closure that returns a validation function that checks that a list of values contains at least one

contains(→ Callable)

Closure that returns a validation function that checks the provided values contain the required value

one_of(→ Callable)

Closure that returns a validation function that checks that the value is one of the given values

type_checker(obj, value)

Validate that the given value is of the correct type for the object. The exact behaviour of this function

url(→ bool)

Validate that the given string is an absolute HTTP URI (i.e. a URL)

Module Contents

class coarnotify.validate.Validator(rules: dict)

A wrapper around a set of validation rules which can be used to select the appropriate validator in a given context.

The validation rules are structured as follows:

{
    "<property>": {
        "default": default_validator_function
        "context": {
            "<context>": {
                "default": default_validator_function
            }
        }
    }
}

Here the <property> key is the name of the property being validated, which may be a string (the property name) or a tuple of strings (the property name and the namespace for the property name).

If a context is provided, then if the top level property is being validated, and it appears inside a field present in the context then the default validator at the top level is overridden by the default validator in the context.

For example, consider the following rules:

{
    Properties.TYPE: {
        "default": validate.type_checker,
        "context": {
            Properties.ACTOR: {
                "default": validate.one_of([
                    ActivityStreamsTypes.SERVICE,
                    ActivityStreamsTypes.APPLICATION
                ])
            }
        }
    }
}

This tells us that the TYPE property should be validated with validate.type_checker by default. But if we are looking at that TYPE property inside an ACTOR object, then instead we should use validate.one_of.

When the get() method is called, the context parameter can be used to specify the context in which the property is being validated.

Parameters:

rules – The rules to use for validation

add_rules(rules)
get(property: str | Tuple[str, str], context: str | Tuple[str, str] = None) Callable

Get the validation function for the given property in the given context

Parameters:
  • property – the property to get the validation function for

  • context – the context in which the property is being validated

Returns:

a function which can be used to validate the property

rules()

The ruleset for this validator

coarnotify.validate.absolute_uri(obj, uri: str) bool

Validate that the given string is an absolute URI

Parameters:
  • obj – The Notify object to which the property being validated belongs.

  • uri – The string that claims to be an absolute URI

Returns:

True if the URI is valid, otherwise ValueError is raised

coarnotify.validate.at_least_one_of(values: List[str]) Callable

Closure that returns a validation function that checks that a list of values contains at least one of the given values

Parameters:

values – The list of values to choose from. When the returned function is run, the values (plural) passed to it must contain at least one of these values

Returns:

a validation function

coarnotify.validate.contains(value: str) Callable

Closure that returns a validation function that checks the provided values contain the required value

Parameters:

value – The value that must be present. When the returned function is run, the value(s) passed to it must contain this value

Returns:

a validation function

coarnotify.validate.one_of(values: List[str]) Callable

Closure that returns a validation function that checks that the value is one of the given values

Parameters:

values – The list of values to choose from. When the returned function is run, the value passed to it must be one of these values

Returns:

a validation function

coarnotify.validate.type_checker(obj, value)

Validate that the given value is of the correct type for the object. The exact behaviour of this function depends on the object provided:

  • If the object has an ALLOWED_TYPES attribute which is not an empty list, then the value must be one of

    the types in that list

  • If the object has a TYPE attribute, then the value must be, or contain, that type

  • In all other cases, type validation will succeed

Parameters:
  • obj – the notify object being validated

  • value – the type being validated

Returns:

True if the type is valid, otherwise ValueError is raised

coarnotify.validate.url(obj, url: str) bool

Validate that the given string is an absolute HTTP URI (i.e. a URL)

Parameters:
  • obj – The Notify object to which the property being validated belongs.

  • uri – The string that claims to be an HTTP URI

Returns:

True if the URI is valid, otherwise ValueError is raised