A popular .NET validation library for building strongly-typed validation rules, rewritten in python
Project description
Creating your first validator
To define a set of validation rules for a particular object, you will need to create a class that inherits from AbstractValidator[T]
, where T
is the type of class that you wish to validate.
For example, imagine that you have a Customer class:
from dataclasses import dataclass
@dataclass
class Customer:
Id:int = None
Surname:str = None
Forename:str = None
Discount:float = None
Address:str = None
You would define a set of validation rules for this class by inheriting from AbstractValidator[Customer]
:
from fluent_validation import AbstractValidator
class CustomerValidator(AbstractValidator[Customer]):
...
The validation rules themselves should be defined in the validator class's constructor.
To specify a validation rule for a particular property, call the rule_for
method, passing a lambda expression
that indicates the property that you wish to validate. For example, to ensure that the Surname
property is not null,
the validator class would look like this:
from fluent_validation import AbstractValidator
class CustomerValidator(AbstractValidator[Customer]):
def __init__(self)-> None:
super().__init__()
self.rule_for(lambda customer: customer.Surname).not_null()
To run the validator, instantiate the validator object and call the validate
method, passing in the object to validate.
customer = Customer()
validator = CustomerValidator()
result = validator.validate(customer)
The validate
method returns a ValidationResult
object. This contains two properties:
is_valid
- a boolean that says whether the validation succeeded.errors
- a collection ofValidationFailure
objects containing details about any validation failures.
The following code would write any validation failures to the console:
customer = Customer()
validator = CustomerValidator()
results = validator.validate(customer)
if not results.is_valid:
for failure in results.errors:
print(f"Property {failure.PropertyName} failed validation. Error was: {failure.ErrorMessage}")
Chaining validators
You can chain multiple validators together for the same property:
from fluent_validation import AbstractValidator
CustomerValidator(AbstractValidator[Customer]):
def __init__(self)-> None:
super().__init__()
rule_for(lambda customer: customer.Surname).not_null().not_equal("foo")
This would ensure that the surname is not null and is not equal to the string 'foo'.
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 fluent_validation-0.1.0.tar.gz
.
File metadata
- Download URL: fluent_validation-0.1.0.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03330feb9ba02ac701c691a7b36f39d05e27f072dafc6cb42f149051921949c1 |
|
MD5 | 6208fc068d680f4496e9b88ea5737623 |
|
BLAKE2b-256 | 45e8f577e3691d6940e0712a6d6345011bd08dc4f5008593947d66159f56f25e |
File details
Details for the file fluent_validation-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fluent_validation-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8663d72f0fca3623905d2a692c8d9bb2c788ccf18acd1630fc6d4d638646d606 |
|
MD5 | c6fe6269b63e1fac330361d0a6d1f776 |
|
BLAKE2b-256 | b95763ccf44694ed8644ac9b6467c9d5e60d81b3e9e1f5bd38af4879a806857b |