The ValidationOptions class contains properties that controls the flow of execution when a validation rule is broken or an unhandled exception occurs.
By default, Validatum will execute all validation functions in order and any exceptions will be added to the broken rules collection. Exceptions are not thrown unless explicitly set in ValidationOptions.
Indicates whether to stop validation when the first invalid rule occurs. Default is false.
When set to true, execution of the validator will immediately end with the ValidationResult containing a single item in the broken rules collection.
var validator = new ValidatorBuilder<Employee>()
.Required(e => e.FirstName)
.Email(e => e.Email)
.Range(e => e.Salary, 50000, 100000)
.LessThanOrEqual(e => e.Commenced, DateTime.Today) // this won't be executed
.Build();
// validation will stop on first error
var result = validator.Validate(
new Employee
{
FirstName = "Robbo",
Email = "rob@bo.xyz",
Salary = 40000, // this is invalid
Commenced = DateTime.Today // this is also invalid
},
new ValidationOptions
{
StopWhenInvalid = true
});
foreach (var rule in result.BrokenRules)
{
Console.WriteLine($"[{rule.Rule}] {rule.Key}: {rule.Message}");
}
// output - only the first validation error
// [Range] Salary: Value must be in range '50000' to '100000'.
Indicates whether to throw ValidationException when validation fails. Default is false.
When set to true, the validator will execute all functions and only throw an exception if validation has failed.
var validator = new ValidatorBuilder<Employee>()
.Required(e => e.FirstName)
.Email(e => e.Email)
.Range(e => e.Salary, 50000, 100000)
.LessThanOrEqual(e => e.Commenced, DateTime.Today, message: "Date cannot be in the future.")
.Build();
// validation will throw an exception
try
{
validator.Validate(
new Employee
{
FirstName = "Robbo",
Email = "rob@bo.xyz",
Salary = 40000, // this is invalid
Commenced = DateTime.Today // this is also invalid
},
new ValidationOptions
{
ThrowWhenInvalid = true
});
}
catch (ValidationException ex)
{
// broken rules can be accessed in the exception
foreach (var rule in ex.BrokenRules)
{
Console.WriteLine($"[{rule.Rule}] {rule.Key}: {rule.Message}");
}
}
// output
// [Range] Salary: Value must be in range '50000' to '100000'.
// [LessThanOrEqual] Commenced: Date cannot be in the future.
Indicates whether to add unhandled exceptions as broken rules when they occur. Default is true.
When set to false, any unhandled exception will be thrown, regardless of the value of ThrowWhenInvalid.
var validator = new ValidatorBuilder<string>()
.With(ctx =>
{
throw new InvalidOperationException("Error");
})
.Build();
// the exception will be added to broken rules collection and not thrown
var result = validator.Validate("validate",
new ValidationOptions
{
AddBrokenRuleForException = true // this is the default
});
foreach (var rule in result.BrokenRules)
{
Console.WriteLine($"[{rule.Rule}] {rule.Key}: {rule.Message}");
}
// output
// [InvalidOperationException] String: Error