Validation Result

IValidationSpecification<T> interface provide special structure with validation result, error message and types of visited Specification - SpecificationResult.
Below is description of each property with examples.

We start of Specification:

var spec = Specification
    .Length<Dictionary<int, string>>(3)
    .And()
    .Null(d => d.Comparer)
    .And()
    .All(Specification
        .LessThan<KeyValuePair<int, string>, int>(
            kv => kv.Key, 100));

spec.IsSatisfiedBy(new Dictionary<int, string>
{
    {1, "First"},
    {100, "Last"}
}, out var result);
result.ToString();
// Object length is not [3]
// Field 'Comparer' value is not valid
// Field 'Comparer': [Object is not null]
// One or more elements are not specified
// [1] Field 'Key' value is not valid
// [1] Field 'Key': [Object is greater than or equal to [100]]
  • OverallResult - overall result returned for current candidate.
    In example - false.
  • TotalSpecificationsCount - count of all executed Specifications in chain.
    In example - 8.
    Two Ands, Length, Null, All, LessThan and two properties Specifications (for 'Comparer' and 'Key').
  • FailedSpecificationsCount - count of failed Specifications (returned false).
    In example - 6.
    And Specifications are "neutral" - thats why 6 but not 8.
  • Errors - list with all failed Specifications errors. In example:
    • Object length is not [3]
    • Field 'Comparer' value is not valid
    • Field 'Comparer': [Object is not null]
    • One or more elements are not specified
    • [1] Field 'Key' value is not valid
    • [1] Field 'Key': [Object is greater than or equal to [100]]
  • FailedSpecifications - list of failed Specifications (FailedSpecification structure).
    • SpecificationType - type of failed Specification.
      In example, in sequence - LengthSpecification<Dictionary<int, string>>, PropertySpecification<Dictionary<int, string>, IEqualityComparer<int>, NullSpecification<IEqualityComparer<int>>, AllSpecification<Dictionary<int, string>, KeyValuePair<int, string>>, PropertySpecification<KeyValuePair<int, string>, int> and LessThanSpecification<int>.
    • Parameters - Dictionary<string, object> - parameters used during candidate verification.
      In example:
      • Length - { "Length", 3 }
      • Comparer Property - { "PropertySelector", d => d.Comparer }, { "PropertyName", "Comparer" }, { "PropertySpecification", NullSpecification<IEqualityComparer<int>> object }
      • Null - none
      • All - { "SpecificationForAll", PropertySpecification<KeyValuePair<int, string>, int> object }
      • Key proerty - { "PropertySelector", kv => kv.Key }, { "PropertyName", "Key" }, { "PropertySpecification", LessThanSpecification<int> object }
      • Less Than - { "LessThan", 100 }
    • Candidate - used candidate.
      In example:
      • Length - Dictionary<int, string> object
      • Comparer Property - Dictionary<int, string> object
      • Null - GenericEqualityComparer<int> object
      • All - Dictionary<int, string> object
      • Key proerty - { 100, "Last" }
      • Less Than - 100
    • Errors - list of errors generated by this Specification.
      In example:
      • Length - "Object length is not [3]"
      • Comparer Property - "Field 'Comparer' value is not valid"
      • Null - "Field 'Comparer': [Object is not null]"
      • All - "One or more elements are not specified"
      • Key proerty - "[1] Field 'Key' value is not valid"
      • Less Than - "[1] Field 'Key': [Object is greater than or equal to [100]]"
  • Trace - trace message. Excellent for debugging Specifications chain behavior from left to right.
    In example - "LengthSpecification<Dictionary<Int32,String>>+Failed And PropertySpecification<Dictionary<Int32,String>,IEqualityComparer>(NullSpecification<IEqualityComparer>+Failed)+Failed And AllSpecification<Dictionary<Int32,String>,KeyValuePair<Int32,String>>([0](PropertySpecification<KeyValuePair<Int32,String>,Int32> (LessThanSpecification)) And [1](PropertySpecification<KeyValuePair<Int32,String>,Int32> (LessThanSpecification+Failed)+Failed))+Failed"
GitHub