Package: garlic-validator
Python Validator (garlic_validator)Cerberus and validator-collection based custom validator package (garlic_validator) for python projects.FeaturesValidator-collection based validator -https://pypi.org/project/validator-collectionCerberus schema validation -https://pypi.org/project/CerberusPydantic validation -https://pypi.org/project/pydanticCustom validator moduleis_empty, is_numpy, is_tensor, is_float, is_truthy, is_falsy, is_bool, is_attr_emptyInstallation1. PrerequisitesPython (>= v3.7)PyPi (>= v21)2. Install garlic-validatorA. [RECOMMENDED] PyPi install# Install or upgrade garlic-validator package:pipinstall--upgradegarlic-validator# To uninstall package:pipuninstall-ygarlic-validatorB. Manually add to PYTHONPATH (Recommended for development)# Clone repository by git:gitclonehttps://github.com/bybatkhuu/python_validator.gitgarlic_validatorcdgarlic_validator# Install python dependencies:pipinstall--upgradepip
catrequirements.txt|xargs-n1-L1pipinstall--no-cache-dir# Add current path to PYTHONPATH:exportPYTHONPATH="${PWD}:${PYTHONPATH}"C. Manually compile and setup (Not recommended)# Clone repository by git:gitclonehttps://github.com/bybatkhuu/python_validator.gitgarlic_validatorcdgarlic_validator# Building python package:pipinstall--upgradepipsetuptoolswheel
pythonsetup.pybuild# Install python dependencies with built package to current python environment:pythonsetup.pyinstall--recordinstalled_files.txt# To remove only installed garlic-validator package:head-n1installed_files.txt|xargsrm-vrf# Or to remove all installed files and packages:catinstalled_files.txt|xargsrm-vrfUsage/Examplesgarlic-validatorandvalidator-collection:importnumpyasnpfromgarlic_validatorimportvalidators,checkers,errorstry:email_address=validators.email('test@domain.dev')# The value of email_address will now be "test@domain.dev"email_address=validators.email('this-is-an-invalid-email')# Will raise a ValueErroremail_address=validators.email(None)# Will raise an EmptyValueErrorexcepterrors.EmptyValueError:# Handling logic goes hereprint('Email address is empty')excepterrors.InvalidEmailError:# More handlign logic goes hereprint('Invalid email address')email_address=validators.email(None,allow_empty=True)print(email_address)# The value of email_address will now be Noneemail_address=validators.email('',allow_empty=True)print(email_address)# The value of email_address will now be Noneis_email_address=checkers.is_email('test@domain.dev')print(is_email_address)# The value of is_email_address will now be Trueis_email_address=checkers.is_email('this-is-an-invalid-email')print(is_email_address)# The value of is_email_address will now be Falseis_email_address=checkers.is_email(None)print(is_email_address)# The value of is_email_address will now be False## Custom validators:## is_empty(val, trim_str=False)is_empty=checkers.is_empty(None)# Trueis_empty=checkers.is_empty('')# Trueis_empty=checkers.is_empty('    ')# Falseis_empty=checkers.is_empty('    ',trim_str=True)# Trueis_empty=checkers.is_empty([])# Trueis_empty=checkers.is_empty({})# Trueis_empty=checkers.is_empty(())# Trueis_empty=checkers.is_empty(set())# Trueis_empty=checkers.is_empty(range(0))# Trueis_empty=checkers.is_empty(np.array([]))# True## is_numpy(val)is_numpy=checkers.is_numpy(np.array([]))# Trueis_numpy=checkers.is_numpy(np.array([1,2,3]))# Trueis_numpy=checkers.is_numpy(None)# False## is_float(val)is_float=checkers.is_float(1)# Trueis_float=checkers.is_float(-1.1123)# Trueis_float=checkers.is_float(1e+123)# Trueis_float=checkers.is_float('0123.000')# Trueis_float=checkers.is_float('1e+12')# Trueis_float=checkers.is_float('2002_12')# False## is_truthy(val)is_truthy=checkers.is_truthy(True)# Trueis_truthy=checkers.is_truthy(1)# Trueis_truthy=checkers.is_truthy('1')# Trueis_truthy=checkers.is_truthy('1.0')# Trueis_truthy=checkers.is_truthy('TRUE')# Trueis_truthy=checkers.is_truthy('True')# Trueis_truthy=checkers.is_truthy('true')# Trueis_truthy=checkers.is_truthy('YES')# Trueis_truthy=checkers.is_truthy('Yes')# Trueis_truthy=checkers.is_truthy('yes')# Trueis_truthy=checkers.is_truthy('Y')# Trueis_truthy=checkers.is_truthy('y')# Trueis_truthy=checkers.is_truthy(1.1)# Falseis_truthy=checkers.is_truthy([1])# Falseis_truthy=checkers.is_truthy(False)# False## is_falsy(val)is_falsy=checkers.is_falsy(False)# Trueis_falsy=checkers.is_falsy(0)# Trueis_falsy=checkers.is_falsy('0')# Trueis_falsy=checkers.is_falsy('0.0')# Trueis_falsy=checkers.is_falsy('FALSE')# Trueis_falsy=checkers.is_falsy('False')# Trueis_falsy=checkers.is_falsy('false')# Trueis_falsy=checkers.is_falsy('NO')# Trueis_falsy=checkers.is_falsy('No')# Trueis_falsy=checkers.is_falsy('no')# Trueis_falsy=checkers.is_falsy('N')# Trueis_falsy=checkers.is_falsy('n')# Trueis_falsy=checkers.is_falsy(2)# Falseis_falsy=checkers.is_falsy('a')# Falseis_falsy=checkers.is_falsy(True)# False## is_bool(val, coerce_value=False)is_bool=checkers.is_bool(True)# Trueis_bool=checkers.is_bool(False)# Trueis_bool=checkers.is_bool(1)# Falseis_bool=checkers.is_bool('1',coerce_value=True)# Trueis_bool=checkers.is_bool('NO',coerce_value=True)# TrueCerberus:fromcerberusimportValidatorv=Validator({'name':{'type':'string'}})print(v.validate({'name':'john doe'}))# Truev.schema={'amount':{'type':'integer'}}print(v.validate({'amount':'1'}))# Falseprint(v.errors)# {'amount': ['must be of integer type']}v.schema={'amount':{'type':'integer','coerce':int}}print(v.validate({'amount':'1'}))# Trueprint(v.document)# {'amount': 1}to_bool=lambdav:v.lower()in('true','1')v.schema={'flag':{'type':'boolean','coerce':(str,to_bool)}}print(v.validate({'flag':'true'}))# Trueprint(v.document)# {'flag': True}pydantic:frompydanticimportvalidate_arguments,ValidationError@validate_argumentsdefrepeat(s:str,count:int,*,separator:bytes=b'')->bytes:b=s.encode()returnseparator.join(bfor_inrange(count))a=repeat('hello',3)print(a)#> b'hellohellohello'b=repeat('x','4',separator=' ')print(b)#> b'x x x x'try:c=repeat('hello','wrong')exceptValidationErrorasexc:print(exc)"""1 validation error for Repeatcountvalue is not a valid integer (type=type_error.integer)"""Running TestsTo run tests, run the following command:pytestReferenceshttps://validator-collection.readthedocs.io/en/latest/index.htmlhttps://github.com/insightindustry/validator-collectionhttps://pydantic-docs.helpmanual.iohttps://github.com/samuelcolvin/pydantichttps://docs.python-cerberus.org/en/stablehttps://github.com/pyeve/cerberus