Let’s recap what we’ve achieved so far with our semantic types for validation. We’ve created a handful of types that work together to give a good model for capturing and processing object validation. The model starts with the base class ValidationResult and its subclasses:

  • SuccessResult handles the case when everything is fine
  • ErrorResult handles a single error
  • AggregateResult collects multiple other results together

The static class Validation is a factory, giving us a simple way to create validation results:

var nameError = Validation.Error("Boom!");
var fooValidation = Validation.ErrorWhen( foo == null, "Foo is missing");

With the operators +, & and &&, we can easily combine validation results together in a fairly declarative fashion. For example, from the motivation post at the start of this series, we can rebuild one of the examples:

public ValidationResult Validate()
{
    return Validation.ErrorWhen(
        string.IsNullOrEmpty(FullName),
        "Mandatory property 'FullName' not supplied.")
    + Validation.ErrorWhen(
        string.IsNullOrEmpty(FamilyName),
        "Mandatory property 'FamilyName' not supplied.")
    + Validation.ErrorWhen(
        string.IsNullOrEmpty(KnownAs),
        "Mandatory property 'KnownAs' not supplied.")
    + Validation.ErrorWhen(
        string.IsNullOrEmpty(SortKey),
        "Mandatory property 'SortKey' not supplied.");
}

But … no project survives scope creep …

Now that we have a solid foundation, what might we add that makes validation easier and more powerful?

Prior post in this series:
Equality of validation
Next post in this series:
Extending validation with warnings

Comments

blog comments powered by Disqus