Skip to main content

onelinevalidation It is a simple library in the Python language that performs validate in an easy and simple way

Project description

onelinevalidation

Now the validation is easy. 😎

onelinevalidation It is a simple library in the Python language that performs validation in an easy and simple way.

Support Me

ko-fi

Installation

  • From PyPi
python pip install onelinevalidation

Examples of how to use

from onelinevalidation import validate_form

userData = {"username": "amr123", "email": "amr.@aol.com", "password": "123Ab#"}
print(validate_form(userData))

Result

{'error': {'username': 'Invalid username should be like this abc_123 or abc. abc', 'email': 'This email address is not valid', 'password': 'The password length must be at least 8 uppercase, lowercase letters, numbers, symbols like @aA123#*'}
}

Create custom validate

If you want more control use custom_validate

from onelinevalidation import custom_validate


pattrens = [
	"[a-zA-Z]+[_.]+[a-zA-Z0-9]+", 
	"[a-zA-Z0-9_-]+[@](aol|gmail|yahoo|outlook)+(.com)+",
	"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
]


messages = [
	"Invalid username should be like this abc_123 or abc. abc",
	"This email address is not valid",
	"The password length must be at least 8 uppercase, lowercase letters, numbers, symbols like @aA123#*"
]


print(custom_validate(userData, pattrens, messages))

Result

{'good': {'username': 'amr_123', 'email': 'amr@aol.com', 'password': '123---Ab#'}}

Do you not prefer dealing with the regex 🙃?

Now in the new version 🥳🎉, you can perform validation using callback functions. From the validators library

from onelinevalidation import validate_data_with_callbacks
import validators


user_data = {
    'btc': '00000000000000000000000',
    'amount': 0.5,
    'md5': '0000'
}

messages = [
    'Invalid btc address',
    'The minimum value must be at least $1.',
    'Invalid md5 value'
]

from functools import partial

callbacks = [
    validators.btc_address,
    {'func': partial(validators.between, min_val = 1, max_val = 100)},
    validators.md5
]

result = validate_data_with_callbacks(user_data, callbacks, messages)
print(result)

Result

{'errors': {'btc': 'Invalid btc address', 'amount': 'The minimum value must be at least $1.', 'md5': 'Invalid md5 value'}}
from onelinevalidation import validate_data_with_callbacks
import validators


user_data = {
    'btc': '3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69',
    'amount': 1,
    'md5': 'd41d8cd98f00b204e9800998ecf8427e'
}

messages = [
    'Invalid btc address',
    'The minimum value must be at least $1.',
    'Invalid md5 value'
]

from functools import partial

callbacks = [
    validators.btc_address,
    {'func': partial(validators.between, min_val = 1, max_val = 100)},
    validators.md5
]

result = validate_data_with_callbacks(user_data, callbacks, messages)
print(result)

Result

{'good': {'btc': '3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69', 'amount': 1, 'md5': 'd41d8cd98f00b204e9800998ecf8427e'}}

Note

In default sanitize = True in validate_form and other functions This is to prevent a Cross-site Scripting vulnerability

For more info : https://owasp.org/www-community/attacks/xss/

I want to thank Gemini 🙂❤ because he helped me develop this library.

Credits

The validators library was used to validate the input data in the onelinevalidation library.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onelinevalidation-0.4.4.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file onelinevalidation-0.4.4.tar.gz.

File metadata

  • Download URL: onelinevalidation-0.4.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for onelinevalidation-0.4.4.tar.gz
Algorithm Hash digest
SHA256 7031d62febbdac5b17cc4142de7a1ee603a28730fd1b45130d1dfb22147822ff
MD5 be0562f7a10a17d44ac6b59df43f4813
BLAKE2b-256 e9d1e7b7eddb0a2a670cb72bba879f1ebbaffe22c62cb196948ca51b33f0b0e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page