Package: datalidator
DatalidatorDatalidatoris 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 outputsetc.NOTE: Some links in this README may not work if the file is not viewed throughGitHub.Features and characteristicssupportsPython 3.9 and aboveno dependencies(other than the Python standard library)are requiredobject-oriented & polymorphic design– there are 3 main types ofDatalidator objects:blueprints – parse untrusted input data to output data of a specific type, and then dispatch filters & validatorsfilters – modify the parsed data in some way while retaining their data typevalidators – validate whether the parsed and filtered data meet certain conditionsadheres to the rules ofdefensive programmingextensive automated test coveragedata-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-safemany built-in & ready-to-useDatalidator objects:for bothprimitiveandnon-primitivedata types (e.g. strings, integers, lists, dictionaries, URLs, IP addresses, ...)19 blueprints+5special blueprints23 filters31 validatorsHow to use this library?InstallationDatalidator can be installed fromPyPIusingpip:python3-mpipinstall--upgradedatalidatorTutorialThis library ships with atutorialseries:The BasicsUsing BlueprintsUsing FiltersUsing ValidatorsLists and DictionariesObject BlueprintSpecial BlueprintsTagsExtending the FunctionalityReal Use Case ExamplesExampleThe following example shows how data from a simple hypothetical registration form could be parsed by this library:fromdatalidator.blueprints.impl.ObjectBlueprintimportObjectBlueprintfromdatalidator.blueprints.impl.StringBlueprintimportStringBlueprintfromdatalidator.blueprints.impl.IntegerBlueprintimportIntegerBlueprintfromdatalidator.blueprints.impl.BooleanBlueprintimportBooleanBlueprintfromdatalidator.blueprints.extras.ObjectModelimportObjectModelfromdatalidator.blueprints.extras.OptionalItemimportOptionalItemfromdatalidator.filters.impl.StringStripFilterimportStringStripFilterfromdatalidator.validators.impl.SequenceMinimumLengthValidatorimportSequenceMinimumLengthValidatorfromdatalidator.validators.impl.NumberMaximumValueValidatorimportNumberMaximumValueValidatorfromdatalidator.validators.impl.NumberMinimumValueValidatorimportNumberMinimumValueValidatorfromdatalidator.validators.impl.StringMatchesRegexValidatorimportStringMatchesRegexValidatorclassRegistrationFormModel(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 Serviceblueprint=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 thetutorial.SecurityDespite 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 thelicense, 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.LicensingThis project is licensed under the3-clause BSD license– see theLICENSEfile.Programmed byVít Labuda.