A popular .NET validation library developed by 'Jeremy Skinner' 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}")
You can also call to_string
on the ValidationResult
to combine all error messages into a single string. By default, the messages will be separated with new lines, but if you want to customize this behaviour you can pass a different separator character to to_string
.
results = validator.validate(customer)
allMessages:str = results.to_string("~"); # In this case, each message will be separated with a `~`
Note : if there are no validation errors, to_string()
will return an empty string.
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-1.0.0.tar.gz
.
File metadata
- Download URL: fluent_validation-1.0.0.tar.gz
- Upload date:
- Size: 32.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c578e50e6789a5f1bb84f8397c43da791e0a56a67567ba1cb7d413dd8763c0ab |
|
MD5 | c4dbddea4f0fcae24ba8d7d44cb2068a |
|
BLAKE2b-256 | b11d23f03600666c0efb37d38a8af5e822d230ed6c17f423d73855ca5dd85bcc |
File details
Details for the file fluent_validation-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: fluent_validation-1.0.0-py3-none-any.whl
- Upload date:
- Size: 51.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d7becc27ff9a37e266e6918a09cfef97d4cb88af9c3cbba725a2425ced4a4c3 |
|
MD5 | 0431e79fc4170d0d7d09d7cacc37710a |
|
BLAKE2b-256 | 902882f939ddea16556ebc86b9daa3daee324160a6b6b67201c8b3739e499d3a |