DataType Validate Event
Summary
The DataType Validate event defines validation rules that apply to all properties of a given data type. Instead of writing the same validation on every property that uses a data type, define it once on the data type and it automatically applies everywhere that data type is used.
When does it fire?
DataType validation fires during the property set pipeline, after DataType AutoCorrect and before property-level validation:
Set property → ReadOnly → DataType AutoCorrect → Property AutoCorrect
→ DataType Validate → Property Validate → Dirty → Changed
Syntax
Data type validation is defined in a business logic class that takes the data type's logic container:
[Logic]
public class MyDataTypeBL(MyDataType.Logic dataType)
{
[RegisterLogic]
public void Validate()
{
dataType.Validate()
.RejectIf(value => /* condition */)
.WithMessage("Error message");
}
}
Fluent API
| Step | Method | Description |
|---|---|---|
| Required | .RejectIf(condition) |
Condition that, when true, rejects the value |
| Required | .WithMessage(message) |
Error message shown when validation fails |
Scenarios
1. Validating email format
All properties using the Email data type must contain a valid email pattern.
[Logic]
public class EmailBL(DataTypes.Email.Logic dataType)
{
[RegisterLogic]
public void ValidateFormat()
{
dataType.Validate()
.RejectIf(value => !string.IsNullOrEmpty(value)
&& !value.Contains('@'))
.WithMessage("Invalid email format");
}
}
2. Ensuring decimal values are non-negative
Prices and costs should not be negative.
[Logic]
public class PositiveDecimalBL(DataTypes.PositiveDecimal.Logic dataType)
{
[RegisterLogic]
public void ValidateNonNegative()
{
dataType.Validate()
.RejectIf(value => value < 0)
.WithMessage("Currency values cannot be negative");
}
}
DataType Validate vs. Property Validate
| DataType Validate | Property Validate | |
|---|---|---|
| Scope | All properties of that data type | One specific property |
| Defined on | Data type logic class | Entity logic class |
| Use for | Universal type rules (Email, PositiveDecimal, etc.) | Business-specific rules |
| Order | Runs first | Runs after data type validation |
Notes
- DataType validation runs before property-level validation.
- If DataType validation fails, the property-level validation does not run.
- Define custom data types (see Data types) when you need reusable validation for a specific kind of data.