Data Validation and normalisation
The delfick_project.norms
package provides helpers for validating and
normalising data.
Everything boils down to objects that have a normalise(meta, val)
method
that takes in a meta
object describing the root of our data and where in the
data we are, and the val
which is the current value to validate and normalise.
Normalisation is where we take the data and modify it to meet the shape we want.
An example of this is:
from delfick_project.norms import sb, Meta, BadSpecValue
spec = sb.listof(sb.string_spec())
normalised = spec.normalise(Meta.empty(), "a_string")
assert normalised == ["a_string"]
normalised = spec.normalise(Meta.empty(), ["one", "two"])
assert normalised == ["one", "two"]
with assertRaises(BadSpecValue, "Expected a string", got=bool, meta=meta.indexed_at(0)):
spec.normalise(Meta.empty(), True)
Included in this package is a class for creating objects from dictionaries of data and validation objects that allow you to declare styles of validation on your data.
- Default normalise helpers
NotSpecified
apply_validators()
Spec
- pass_through_spec
- always_same_spec
- dictionary_spec
- dictof
- tupleof
- listof
- set_options
- defaulted
- required
- boolean
- directory_spec
- filename_spec
- file_spec
- string_spec
- integer_spec
- float_spec
- string_or_int_as_string_spec
- valid_string_spec
- integer_choice_spec
- string_choice_spec
- create_spec
- or_spec
- match_spec
- and_spec
- optional_spec
- dict_from_bool_spec
- formatted
- many_format
- overridden
- any_spec
- container_spec
- delayed
- typed
- has
- tuple_spec
- none_spec
- many_item_formatted_spec
- Validator helpers
- The Meta object
- The dictobj class
- Errors