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¶
A wrapper around a set of validation rules which can be used to select the appropriate validator |
Functions¶
|
Validate that the given string is an absolute URI |
|
Closure that returns a validation function that checks that a list of values contains at least one |
|
Closure that returns a validation function that checks the provided values contain the required value |
|
Closure that returns a validation function that checks that the value is one of the given values |
|
Validate that the given value is of the correct type for the object. The exact behaviour of this function |
|
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 atupleof strings (the property name and the namespace for the property name).If a
contextis provided, then if the top level property is being validated, and it appears inside a field present in thecontextthen thedefaultvalidator at the top level is overridden by thedefaultvalidator in thecontext.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
TYPEproperty should be validated withvalidate.type_checkerby default. But if we are looking at thatTYPEproperty inside anACTORobject, then instead we should usevalidate.one_of.When the
get()method is called, thecontextparameter 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:
Trueif 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_TYPESattribute which is not an empty list, then the value must be one of the types in that list
- If the object has an
If the object has a
TYPEattribute, then the value must be, or contain, that typeIn all other cases, type validation will succeed
- Parameters:
obj – the notify object being validated
value – the type being validated
- Returns:
Trueif 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:
Trueif the URI is valid, otherwise ValueError is raised