Class ValidationOptions
A model for options which affect how a validator should behave whilst it is performing its validation.
Inheritance
Inherited Members
Namespace: CSF.Validation
Assembly: CSF.Validation.Abstractions.dll
Syntax
public class ValidationOptions
Remarks
Specify validation options to customise how the validator behaves as it is performing validation.
There are two opportunities to do this; default validation options may be provided at the point of adding the validation framework to
dependency injection, with the UseValidationFramework()
method.
Options may also be specified directly when using one of the
ValidateAsync(Object, ValidationOptions, CancellationToken) or
ValidateAsync(TValidated, ValidationOptions, CancellationToken)
methods.
In this class, every property is nullable. Properties which are set to non-null values are considered to be specified and those which are null are considered to be unspecified.
When performing validation, the validation options are resolved using an implementation of IGetsResolvedValidationOptions. That service will get an effective value for each validation option represented by properties of this class, using the following order of precedence:
- If specified in the
ValidateAsync()
method then that specified value is used. - If specified as a default value in
UseValidationFramework()
then that default value is used. - If the options property is unspecified in both of the above then a hard-coded default value is used. See documentation for the corresponding properties of ResolvedValidationOptions for information about those hard-coded defaults.
Thus, options which are specified directly when performing validation will override those specified in default options.
Properties
| Improve this Doc View SourceAccessorExceptionBehaviour
Gets or sets a value which indicates the default behaviour should a value-accessor raise an exception during validation.
Declaration
public ValueAccessExceptionBehaviour? AccessorExceptionBehaviour { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<ValueAccessExceptionBehaviour> |
Remarks
The validator automatically catches exceptions when executing validation rules and converts each of these into a rule result which has an outcome of Errored. This option configures what the validator should do by default when accessing a value to be validated throws an exception.
Please note that the behaviour specified in this validation option can be overridden by individual values in the validation manifest. The property AccessorExceptionBehaviour, where set to a non-null value, will override the behaviour specified upon this options property.
See Also
EnableMessageGeneration
Gets or sets a value which determines whether or not the validator should generate validation feedback messages, such as for rules which fail validation.
Declaration
public bool? EnableMessageGeneration { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Boolean> |
Remarks
An optional piece of functionality within this validation framework is the generation of human-readable feedback messages relating to validation, typically for failures or errors. By default this functionality is disabled and the logic to generate messages is skipped, providing a performance boost when it is not going to be used.
Setting this option value to true enables this functionality and means that every validation rule result has the opportunity to have a Message generated with feedback advising the user what to do about the validation failure. If thie functionality is not enabled then that message property will be left at its unset/default value of null.
For more information about validation feedback message generation, you are encouraged to read the corresponding documentation.
See Also
| Improve this Doc View SourceEnableRuleParallelization
Gets or sets a value indicating whether or not rules are permitted to be executed/evaluated in parallel. This will offer a modest performance improvement, particularly if your rules perform a lot of CPU-bound work.
Declaration
public bool? EnableRuleParallelization { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Boolean> |
Remarks
Running rules in parallel requires both of the following:
- This configuration option must be set to true (this is the default setting).
- Individual rule classes must be decorated with the ParallelizableAttribute to be eligible for parallel execution.
For rules which are executed in parallel, the default .NET Task Parallel Library is used. More can be read about this at https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming
See Also
| Improve this Doc View SourceInstrumentRuleExecution
Gets or sets a value that indicates whether or not the validaiton rule process should record and add instrumentation data to the results.
Declaration
public bool? InstrumentRuleExecution { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Boolean> |
Remarks
Developers wishing to benchmark or profile validation rules may find this useful. When this option is set to true, additional information is added to the validation results, including information about the length of time spent executing rules and/or generating validation feedback messages.
Because the act of gathering this information causes some performance degradation, this option defaults to false.
See Also
| Improve this Doc View SourceRuleThrowingBehaviour
Gets or sets the exception-throwing behaviour for validation rules.
Declaration
public RuleThrowingBehaviour? RuleThrowingBehaviour { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<RuleThrowingBehaviour> |
Remarks
When a validator - an IValidator or an IValidator<TValidated> - completes validation it might throw an exception, governed by this option setting. If this is set to OnError then the validator will throw if any result indicates Errored. If this is set to OnFailure then the validator will throw if any result indicates either Failed or Errored.
See Also
| Improve this Doc View SourceTreatMessageGenerationErrorsAsRuleErrors
Gets or sets a value that determines whether or not the validator should treat errors encountered whilst generating validation failure messages as rule errors.
Declaration
public bool? TreatMessageGenerationErrorsAsRuleErrors { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Boolean> |
Remarks
Firstly, this option is irrelevant if EnableMessageGeneration is not true.
When this option is not true, then any errors encountered by the validation framework whilst generating validation failure messages are ignored. In this case, if a message was to be generated for a rule result but an error meant that it could not be then the Message property of the validation rule result is left null but the validation will otherwise complete normally.
If this option is set to true then if an error occurs whilst generating a message for a validation rule then that rule will have its outcome set to Errored and the exception will be stored within the Exception property. This could then cause the overall validation process to raise an exception, depending upon the setting of the RuleThrowingBehaviour option.