Cion namespace API Reference¶
Cion namespace
- class cion.Field[source]¶
A field in a Schema
- __init__(*, filters: list[Callable[[Any], Any]] | None = None, default: Any | None = None, nullable: bool = False, required: bool = False) None[source]¶
Create a field for use in a Schema
data is assumed to be the data passed to
Schema.validate()- Parameters:
filters (list[Callable[[Any], Any]] | None) – A list of filters that get passed against the value
default (Any | None) – The default value for the field, if it is not present in the data
nullable (bool) – Whether the value can be None When this is
Trueand the value is None, no filters are calledrequired (bool) – Whether or not the field is required to be in the data
- Return type:
None
- class cion.Options[source]¶
Schema Options
- __init__(*, extra: ExtraFields = ExtraFields.IGNORE, stop_on_error: bool = False) None[source]¶
Class for creating Schema options
- Parameters:
extra (ExtraFields) –
What to do when there are extra fields in the data
See the docs for :class:
cion.options.ExtraFieldsOptionstop_on_error (bool) –
Whether or not to continue after an error If
True, when an error is raised, stop immediately and throw an errorIf
False, when an error is raised, compile a list of errors and throw the error after validation is doneEssentially, this option dictates whether you want to receive any validated data on error, or wait to get all the valid data
- Return type:
None
- class cion.Schema[source]¶
Schema to validate data
- __init__(fields: dict[str, Field], options: Options | None = None) None[source]¶
Create schema instance
Initializes a schema instance that can be used to validate data
- Parameters:
fields (dict[str, Field]) – A dictionary of fields, with the keys being names and the values being an instance of
cion.Fieldoptions (Options | None) – Optional options to add extra functionality to the schema
- Return type:
None
Notes
__schema__is not allowed to be a field name, it is reserved for internal usage
- validate(data: dict[Any, Any]) dict[Any, Any][source]¶
Validate a dict according to the defined schema
Goes through every item in the data and applies constraints to it
Note
It is important to ensure that
datais an iterable that can have data removed. It is highly reccomended to convert it to a dict before calling this method.- Parameters:
data (dict[Any, Any]) – The data to be validated
- Return type:
dict[Any, Any]
- Returns
The validated and transformed data depending on the specified validators
- Raises:
ValidationError – When
self.stop_on_erroris true and a field in the data does not validate properlyValidationError – When
self.stop_on_erroris false, this will contain all the errors, if any
- Parameters:
data (dict[Any, Any])
- Return type:
dict[Any, Any]