A flexible, object-oriented Python input data parsing & validation library
Project description
Datalidator
Datalidator is a flexible, object-oriented Python library for parsing and validating untrusted input data.
One of its most prominent characteristics is that it is able to parse various kinds of input data: configuration files, user inputs, web API outputs etc.
NOTE: Some links in this README may not work if the file is not viewed through GitHub.
Features and characteristics
- supports Python 3.9 and above
- no dependencies (other than the Python standard library) are required
- object-oriented & polymorphic design – there are 3 main types of Datalidator objects:
- blueprints – parse untrusted input data to output data of a specific type, and then dispatch filters & validators
- filters – modify the parsed data in some way while retaining their data type
- validators – validate whether the parsed and filtered data meet certain conditions
- adheres to the rules of defensive programming
- extensive automated test coverage
- data-source-agnostic – the library may be used to parse configuration files, user inputs, web API outputs, ...
- environment-agnostic – the library does not access any external resources (such as the filesystem or network)
- thread-safe
- many built-in & ready-to-use Datalidator objects:
- for both primitive and non-primitive data types (e.g. strings, integers, lists, dictionaries, URLs, IP addresses, ...)
- 19 blueprints + 5 special blueprints
- 23 filters
- 31 validators
How to use this library?
Installation
Datalidator can be installed from PyPI using pip
:
python3 -m pip install --upgrade datalidator
Tutorial
This library ships with a tutorial series:
- The Basics
- Using Blueprints
- Using Filters
- Using Validators
- Lists and Dictionaries
- Object Blueprint
- Special Blueprints
- Tags
- Extending the Functionality
- Real Use Case Examples
Example
The following example shows how data from a simple hypothetical registration form could be parsed by this library:
from datalidator.blueprints.impl.ObjectBlueprint import ObjectBlueprint
from datalidator.blueprints.impl.StringBlueprint import StringBlueprint
from datalidator.blueprints.impl.IntegerBlueprint import IntegerBlueprint
from datalidator.blueprints.impl.BooleanBlueprint import BooleanBlueprint
from datalidator.blueprints.extras.ObjectModel import ObjectModel
from datalidator.blueprints.extras.OptionalItem import OptionalItem
from datalidator.filters.impl.StringStripFilter import StringStripFilter
from datalidator.validators.impl.SequenceMinimumLengthValidator import SequenceMinimumLengthValidator
from datalidator.validators.impl.NumberMaximumValueValidator import NumberMaximumValueValidator
from datalidator.validators.impl.NumberMinimumValueValidator import NumberMinimumValueValidator
from datalidator.validators.impl.StringMatchesRegexValidator import StringMatchesRegexValidator
class RegistrationFormModel(ObjectModel):
username = StringBlueprint(
filters=[StringStripFilter()],
validators=[StringMatchesRegexValidator(r'^[A-Za-z0-9_]{3,20}\Z')]
)
password = StringBlueprint(
filters=[StringStripFilter()],
validators=[SequenceMinimumLengthValidator(8)]
)
age = IntegerBlueprint(
validators=[NumberMinimumValueValidator(13), NumberMaximumValueValidator(100)],
)
tos_accepted = OptionalItem(wrapped_blueprint=BooleanBlueprint(), default_value=False) # "tos" = Terms of Service
blueprint = ObjectBlueprint(object_model=RegistrationFormModel)
blueprint.use({"username": "Joe_123", "password": "MyPassword", "age": "18\n", "tos_accepted": True})
# == RegistrationFormModel(username='Joe_123', password='MyPassword', age=18, tos_accepted=True)
For simpler examples with explanation, see the tutorial.
Security
Despite the non-negligible efforts made to reduce the risk of bugs being present in this library (the adherence to the rules of defensive programming, extensive automated test coverage), it can never be guaranteed that there are no bugs or even security vulnerabilities present. We all know what Murphy's law states, don't we?
As stated in the license, this library comes with ABSOLUTELY NO WARRANTY:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Licensing
This project is licensed under the 3-clause BSD license – see the LICENSE file.
Programmed by Vít Labuda.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file datalidator-1.0.3.tar.gz
.
File metadata
- Download URL: datalidator-1.0.3.tar.gz
- Upload date:
- Size: 55.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52760b9d41e8988734a104c0dc213cef7be3ccf6701caf8e43d5aa36aa2263c1 |
|
MD5 | 63a224caf603a6fa647010e07af504c4 |
|
BLAKE2b-256 | 48ca31d8c2a2ed837029e8a62289704a88dc5930b90e31b5ef835e6a97e784b5 |