DEV Community

Free Python Code
Free Python Code

Posted on

How to make validation using onelinevalidation library In Python

Hi πŸ™‚πŸ–

In this post, I will share with you how to use onelinevalidation library In Python.

What is onelinevalidation

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

install onelinevalidation

python pip install onelinevalidation
Enter fullscreen mode Exit fullscreen mode

Examples of how to use


from onelinevalidation import validate_form

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

Enter fullscreen mode Exit fullscreen mode

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#*'}
}

Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Result

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

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Result

{'errors': {'btc': 'Invalid btc address', 'amount': 'The minimum value must be at least $1.', 'md5': 'Invalid md5 value'}}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Result

{'good': {'btc': '3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69', 'amount': 1, 'md5': 'd41d8cd98f00b204e9800998ecf8427e'}}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)