Python3 Validation in another way
Project description
SmileValidation v1.2.10
Python3 validation with a different approach.
Validating those form elements is enough to make me feel hurt. Time to bring a new technique type.
What's new v1.2.x:
- Adding RuleSchema into Validation class
- Improving rule of validation method
- Fix bugs
Validation class
After adding elements to the collection, the core class includes a validation element that validates each element by calling IsValid().
from smilevalidation.Validation import Validation
# validition instance
v = Validation()
## integer validation
# add element with
v.addElement(
elementName= 'computer-quantity'
, elementValue= 2
, rule= v.rule.getInteger(
require= True
, maxValue= 5
, minValue= 1
, negative= False
)
)
## start validating
# true if every element is correct
if v.isValid():
Console.output(f'Everything is fine')
else:
Console.output(f'Error: {v.getErrorNumber()}')
Comparison
For reality form validation, it does not just need validation with the value, they need also the comparison such as confirm password form. So does it? It should build for this purpose via addMatchedElement method and addNotMatchedElement. Both will validate the matched value and not matched value.
v.addMatchedElement(
elementName1= 'm1'
, elementValue1='hello'
, elementName2='m2'
, elementValue2='hello2'
)
v.addNotMatchedElement(
elementName1= 'm2'
, elementValue1='hello'
, elementName2='m1'
, elementValue2='hello2'
)
Then the IsValid() method will execute these too.
Full example:
# sample with two elements
from smilevalidation.Validator import Validator
from smilevalidation.Rule import Rule
# validition instance
v = Validation()
# match element value
v.addMatchedElement(
elementName1= 'm1'
, elementValue1='hello'
, elementName2='m2'
, elementValue2='hello2'
)
v.addNotMatchedElement(
elementName1= 'm2'
, elementValue1='hello'
, elementName2='m1'
, elementValue2='hello2'
)
## start validating
# true if every element is correct
if v.isValid():
Console.output(f'Everything is fine')
else:
Console.output(f'Error: {v.getErrorNumber()}')
Rule Reusable
Extending or overriding the existing RuleSchema port
Example:
from Validation import Validation, RuleSchema
class Rule2(RuleSchema):
def getQuantityOne(self) -> dict:
"""
:return:
"""
return self.getInteger(
require= True
, maxValue= 5
, minValue= 1
, negative= False
)
# init
val = Validation()
# override the new rule
val.setRule(rule= Rule2())
getQualityOne method will present the new rule function
# Two elements with Rule2 class
from smilevalidation.Validation import Validation
# validition instance
v = Validation()
v.setRule(rule= Rule2())
## integer validation
# add element with
v.addElement(
elementName= 'computer-quantity'
, elementValue= 2
, rule= self.rule.getQuantityOne()
)
v.addElement(
elementName= 'tv-quantity'
, elementValue= 4
, rule= self.rule.getQuantityOne()
)
## start validating
# true if every element is correct
if v.isValid():
Console.output(f'Everything is fine')
else:
Console.output(f'Error: {v.getErrorNumber()}')
Full example
# validition instance
v = Validation()
## integer validation
# add element with
v.addElement(
elementName= 'computer-quantity'
, elementValue= 2
, rule= self.rule.getInteger(
require= True
, maxValue= 5
, minValue= 1
, negative= False
)
)
v.addElement(
elementName= 'tv-quantity'
, elementValue= 4
, rule= self.rule.getInteger(
require= True
, maxValue= 5
, minValue= 1
, negative= False
)
)
# match element value
v.addMatchedElement(
elementName1= 'm1'
, elementValue1='hello'
, elementName2='m2'
, elementValue2='hello2'
)
v.addNotMatchedElement(
elementName1= 'm2'
, elementValue1='hello'
, elementName2='m1'
, elementValue2='hello2'
)
## start validating
# true if every element is correct
if v.isValid():
Console.output(f'Everything is fine')
else:
Console.output(f'Error: {v.getErrorNumber()}')
It is available on PyPi store via https://pypi.org/project/SmileValidation/
In order to support my work, please donate to me through Buy me a Coffee
My unique slogan is:
a little developer in the big world \o/
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
Built Distribution
File details
Details for the file SmileValidation-1.2.10.tar.gz
.
File metadata
- Download URL: SmileValidation-1.2.10.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85b903026a095239c82e4454d170c95b512e0800583e4d730d2a52f8bad7cf53 |
|
MD5 | 391b3b3fa1dcc55a0d4ca30a103c069e |
|
BLAKE2b-256 | 1e3ce620582e1061bc1a2c30bb1228ca0209703d0a4aca2029e39fa06765a04b |
File details
Details for the file SmileValidation-1.2.10-py3-none-any.whl
.
File metadata
- Download URL: SmileValidation-1.2.10-py3-none-any.whl
- Upload date:
- Size: 26.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a81cb0014fff7c0a371cf8c5a9f6df42ef58866114ed009c39d4111b2ff44a86 |
|
MD5 | 62294bbd5f226bdee6dabf27a13a33c1 |
|
BLAKE2b-256 | 8e55bda7e03b38416cd61831c39d05967afc7645e98fc07b2cdc305c95163198 |