Validation
Last updated
Last updated
Structr.Validation package provides functionality for building strongly-typed validation.
Validation package is available on NuGet.
Configure validation services:
AddValidation()
extension method performs registration of validation provider service and validators implementing IValidator
or inherited from Validator
class.
Parameter name | Parameter type | Description |
---|---|---|
Additionally configure IValidationProvider
service by specifying it's type and lifetime used ValidationServiceOptions
.
Property name | Property type | Description |
---|---|---|
Validation objects can be Entities, DTO's, Commands, Queries and many others typed objects.
The basic usage is:
The last step is to inject IValidationProvider
service and use it:
Recommendation: For validating Commands/Queries use IOperationFilter
instead of calling _validationProvider.ValidateAndThrowAsync()
in each command/query. See more details about Operations.
IValidationProvider
methods:
ValidationFailure
represents a single validation error.
ValidationFailure
properties:
ValidationResult
represents all failures that occur during validation execution.
ValidationResult
properties:
ValidationResult
class inherits from IEnumerable<ValidationFailure>
and allow you to use iteration:
Thrown ValidationException
class has following main properties:
Method name | Return type | Description |
---|---|---|
Property name | Property type | Description |
---|---|---|
Property name | Property type | Description |
---|---|---|
Property name | Property type | Description |
---|---|---|
assembliesToScan
params Assembly[]
List of assemblies to search validators.
configureOptions
Action<ValidationServiceOptions>
Options to be used by validation service.
ProviderServiceType
Type
Changes standard implementation of IValidationProvider
to specified one, default is typeof(ValidationProvider)
.
ProviderServiceLifetime
ServiceLifetime
Specifies the lifetime of an IValidationProvider
service, default is Transient
.
ValidateAsync
ValidationResult
Asynchronously validates the object and returns the ValidationResult
.
ValidateAndThrowAsync
-
Asynchronously validates the object and throws ValidationException
if validation result has failures.
ParameterName
string
The name of the parameter that caused the failure.
ActualValue
object
The value of the parameter that caused the failure.
Message
string
The message that describes the failure.
Code
string
The optional failure code.
Level
ValidationFailureLevel
The optional level of failure - Error
, Warning
or Info
. Default value is Error
.
IsValid
bool
Returns true
if the validation result has not failures, otherwise false
.
ValidationResult
ValidationResult
The result of validation.
Message
string
The messages of all failures joined into one string.