6
\$\begingroup\$

Quite some time ago I have created the Simple object validator (see also self-answer). The more I used it the more I thougt its API could be better so I have heavily refactored it and would like to you take another look at the new version.

Requirements

I'd like my validator to be:

  • intuitive
  • easy to use
  • extendable
  • testable
  • helpful by providing precise error messages
  • immutable so that predefined rules cannot be manipulated

In order to meet these criteria I have removed a couple of classes and built it on top of System.Collections.Immutable. Usually, you should not notice that and be able to just use the provided extensions.

How it works

You start with an empty collection of rules for the specified type and use one of the Add extensions to add validation rules. There are two types of them:

  • Require - which means it cannot continue when this fails (e.g. something is null)
  • Ensure - the validator can continue with the next rule

Validation rules are compiled from expressions and use two parameters:

  • T - the object being validated
  • TContext - optional context with additional data

Expressions are also used for generating error messages that are prettyfied with an expression visitor that replaces ugly closure classes with pretty type names like <param:Person>.FirstName.

The main extensibility point of this framework are the two properties Require and Ensure that return a builder that lets the user chain extensions such as True, False, NotNull etc.

There is no classic validator but an extension (ValidateWith), for an IImutableList<> that executes the rules. It returns a tuple with the object being validated and a lookup with results. Its key is bool where true returns successul rules and false failed ones. When the execution should be interrupted because of validation errors, the user can chain the ThrowIfValidationFailed extension.

With the currently available APIs it's also possible to create shortcuts to reduce the verbosity. See the Simplified test below. I think it still could be better.

In general, a set of rules would be a static field. It's supposed to be build once and reused many times as compiling expressions might otherwise become a bottleneck.

Example

These tests show it in action:

public class ValidationTest
{
    private static readonly Person Tester = new Person
    {
        FirstName = "Cookie",
        LastName = "Monster",
        Address = new Address
        {
            Street = "Sesame Street"
        }
    };

    [Fact]
    public void Can_validate_rules()
    {
        var rules =
            ValidationRuleCollection
                .For<Person>()
                .Add(x =>
                    ValidationRule
                        .Require
                        .NotNull(x))
                .Add(x =>
                    ValidationRule
                        .Require
                        .NotNull(() => x.FirstName))
                .Add(x =>
                    ValidationRule
                        .Ensure
                        .True(() => x.FirstName.Length > 3))
                .Add(x =>
                    ValidationRule
                        .Require
                        .NotNull(() => x.Address))
                .Add(x =>
                    ValidationRule
                        .Ensure
                        .False(() => x.Address.Street.Length > 100));

        var (person, results) = Tester.ValidateWith(rules);

        Assert.Equal(5, results[true].Count());
        Assert.Equal(0, results[false].Count());

        Tester.ValidateWith(rules).ThrowIfValidationFailed();
    }

    [Fact]
    public void Can_throw_if_validation_failed()
    {
        var rules =
            ValidationRuleCollection
                .For<Person>()
                .Add(x =>
                    ValidationRule
                        .Require
                        .NotNull(x))
                .Add(x =>
                    ValidationRule
                        .Require
                        .NotNull(() => x.FirstName))
                .Add(x =>
                    ValidationRule
                        .Ensure
                        .True(() => x.FirstName.Length > 3));

        var (person, results) = default(Person).ValidateWith(rules);

        Assert.Equal(0, results[true].Count());
        Assert.Equal(1, results[false].Count());
        Assert.ThrowsAny<DynamicException>(() => default(Person).ValidateWith(rules).ThrowIfValidationFailed());
    }

    [Fact]
    public void Simplified()
    {
        var rules =
            ValidationRuleCollection
                .For<Person>()
                .Require((b, x) => b.NotNull(() => x))
                .Ensure((b, x) => b.NotNull(() => x.FirstName))
                .Ensure((b, x) => b.True(() => x.FirstName.Length > 3));

        var (person, results) = default(Person).ValidateWith(rules);

        Assert.Equal(0, results[true].Count());
        Assert.Equal(1, results[false].Count());
        Assert.ThrowsAny<DynamicException>(() => default(Person).ValidateWith(rules).ThrowIfValidationFailed());
    }

    private class Person
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public Address Address { get; set; }
    }

    private class Address
    {
        public string Street { get; set; }
    }
}

Code

ValidationRuleCollection and convenience extensions for working with immutable collections so that I don't have to create my own immutables.

public static class ValidationRuleCollection
{
    public static IImmutableList<IValidationRule<T, TContext>> For<T, TContext>() => ImmutableList<IValidationRule<T, TContext>>.Empty;

    public static IImmutableList<IValidationRule<T, object>> For<T>() => ImmutableList<IValidationRule<T, object>>.Empty;
}

public static class ValidationRuleCollectionExtensions
{
    public static IImmutableList<IValidationRule<T, TContext>> Add<T, TContext>(this IImmutableList<IValidationRule<T, TContext>> rules, Func<T, TContext, ValidationRuleBuilder> builder)
    {
        return rules.Add(builder(default, default).Build<T, TContext>());
    }

    public static IImmutableList<IValidationRule<T, object>> Add<T>(this IImmutableList<IValidationRule<T, object>> rules, Func<T, ValidationRuleBuilder> builder)
    {
        return rules.Add(builder(default).Build<T, object>());
    }

    public static IImmutableList<IValidationRule<T, object>> Require<T>(this IImmutableList<IValidationRule<T, object>> rules, Func<ValidationRuleBuilder, T, ValidationRuleBuilder> builder)
    {

        return rules.Add(builder(ValidationRule.Require, default).Build<T, object>());
    }

    public static IImmutableList<IValidationRule<T, object>> Ensure<T>(this IImmutableList<IValidationRule<T, object>> rules, Func<ValidationRuleBuilder, T, ValidationRuleBuilder> builder)
    {

        return rules.Add(builder(ValidationRule.Ensure, default).Build<T, object>());
    }


    public static (T Value, ILookup<bool, IValidationResult<T>> Results) ValidateWith<T, TContext>(this T obj, IImmutableList<IValidationRule<T, TContext>> rules, TContext context)
    {
        return
        (
            obj,
            rules
                .Evaluate(obj, context)
                .ToLookup(r => r.Success)
        );
    }

    public static (T Value, ILookup<bool, IValidationResult<T>> Results) ValidateWith<T>(this T obj, IImmutableList<IValidationRule<T, object>> rules)
    {
        return obj.ValidateWith(rules, default);
    }

    private static IEnumerable<IValidationResult<T>> Evaluate<T, TContext>(this IImmutableList<IValidationRule<T, TContext>> rules, T obj, TContext context)
    {
        var result = default(IValidationResult<T>);
        foreach (var rule in rules)
        {
            yield return result = rule.Evaluate(obj, context);
            if (!result.Success && rule.Option == ValidationRuleOption.Require) yield break;
        }
    }
}

ValidationRule, its callbacks and helpers.

public delegate bool ValidationPredicate<in T, in TContext>(T obj, TContext context);

public delegate string MessageCallback<in T, in TContext>(T obj, TContext context);

public interface IValidationRule<T, in TContext>
{
    ValidationRuleOption Option { get; }

    IValidationResult<T> Evaluate([CanBeNull] T obj, TContext context);
}

public enum ValidationRuleOption
{
    Ensure,
    Require
}

internal class ValidationRule<T, TContext> : IValidationRule<T, TContext>
{
    private readonly ValidationPredicate<T, TContext> _predicate;
    private readonly MessageCallback<T, TContext> _message;
    private readonly string _expressionString;

    public ValidationRule
    (
        [NotNull] Expression<ValidationPredicate<T, TContext>> predicate,
        [NotNull] Expression<MessageCallback<T, TContext>> message,
        [NotNull] ValidationRuleOption option
    )
    {
        if (predicate == null) throw new ArgumentNullException(nameof(predicate));

        _predicate = predicate.Compile();
        _message = message.Compile();
        _expressionString = ValidationParameterPrettifier.Prettify<T>(predicate).ToString();
        Option = option;
    }

    public ValidationRuleOption Option { get; }

    public IValidationResult<T> Evaluate(T obj, TContext context)
    {
        return new ValidationResult<T>(ToString(), _predicate(obj, context), _message(obj, context));
    }

    public override string ToString() => _expressionString;

    public static implicit operator string(ValidationRule<T, TContext> rule) => rule?.ToString();
}

public static class ValidationRule
{
    public static ValidationRuleBuilder Ensure => new ValidationRuleBuilder(ValidationRuleOption.Ensure);

    public static ValidationRuleBuilder Require => new ValidationRuleBuilder(ValidationRuleOption.Require);
}

ValidtionBuilder...

public class ValidationRuleBuilder
{
    private readonly ValidationRuleOption _option;

    private LambdaExpression _predicate;
    private LambdaExpression _message;

    public ValidationRuleBuilder(ValidationRuleOption option)
    {
        _option = option;
    }

    public ValidationRuleBuilder Predicate(LambdaExpression expression)
    {
        _predicate = expression;
        return this;
    }

    public ValidationRuleBuilder Message(Expression<Func<string>> message)
    {
        _message = message;
        return this;
    }

    [NotNull]
    public IValidationRule<T, TContext> Build<T, TContext>()
    {
        if (_predicate is null || _message is null) throw new InvalidOperationException("Validation-rule requires you to set rule and message first.");

        var parameters = new[]
        {
            _predicate.Parameters.ElementAtOrDefault(0) ?? ValidationParameterPrettifier.CreatePrettyParameter<T>(),
            _predicate.Parameters.ElementAtOrDefault(1) ?? ValidationParameterPrettifier.CreatePrettyParameter<TContext>()
        };

        var expressionWithParameter = parameters.Aggregate(_predicate.Body, ValidationParameterInjector.InjectParameter);
        var predicate = Expression.Lambda<ValidationPredicate<T, TContext>>(expressionWithParameter, parameters);

        var messageWithParameter = parameters.Aggregate(_message.Body, ValidationParameterInjector.InjectParameter);
        var message = Expression.Lambda<MessageCallback<T, TContext>>(messageWithParameter, parameters);

        return new ValidationRule<T, TContext>(predicate, message, _option);
    }
}

...and its extensions.

using static ValidationExpressionFactory;

public static class ValidationRuleBuilderExtension
{
    public static ValidationRuleBuilder True(this ValidationRuleBuilder builder, Expression<Func<bool>> expression)
    {
        return
            builder
                .Predicate(expression)
                .Message(() => "The specified expression must be 'true'.");
    }

    public static ValidationRuleBuilder Null<TMember>(this ValidationRuleBuilder builder, Expression<Func<TMember>> expression)
    {
        return
            builder
                .Predicate(ReferenceEqualNull(expression))
                .Message(() => $"{typeof(TMember).ToPrettyString(false)} must be null.");
    }

    public static ValidationRuleBuilder Null<T>(this ValidationRuleBuilder builder, T value)
    {
        return
            builder
                .Predicate(ReferenceEqualNull<T>())
                .Message(() => $"{typeof(T).ToPrettyString(false)} must be null.");
    }

    public static ValidationRuleBuilder False(this ValidationRuleBuilder builder, Expression<Func<bool>> expression)
    {
        return
            builder
                .Predicate(Negate(expression))
                .Message(() => "The specified expression must be 'false'.");
    }

    public static ValidationRuleBuilder NotNull<TMember>(this ValidationRuleBuilder builder, Expression<Func<TMember>> expression)
    {
        return
            builder
                .Predicate(Negate(ReferenceEqualNull(expression)))
                .Message(() => $"{typeof(TMember).ToPrettyString(false)} must not be null.");
    }

    public static ValidationRuleBuilder NotNull<T>(this ValidationRuleBuilder builder, T value)
    {
        return
            builder
                .Predicate(Negate(ReferenceEqualNull<T>()))
                .Message(() => $"{typeof(T).ToPrettyString(false)} must not be null.");
    }
}

ValidationResult with its extensions

using static ValidationResult;

// ReSharper disable once UnusedTypeParameter - T is required for chaining extensions.
public interface IValidationResult<T>
{
    string Expression { get; }

    bool Success { get; }

    string Message { get; }
}

internal static class ValidationResult
{
    public static readonly IDictionary<bool, string> Strings = new Dictionary<bool, string>
    {
        [true] = "Success",
        [false] = "Failed"
    };
}

internal class ValidationResult<T> : IValidationResult<T>
{
    public ValidationResult([NotNull] string expression, bool success, [NotNull] string message)
    {
        Expression = expression;
        Success = success;
        Message = message;
    }

    public string Expression { get; }

    public bool Success { get; }

    public string Message { get; }        

    public override string ToString() => $"{Strings[Success]} | {Message} | {Expression}";

    public static implicit operator bool(ValidationResult<T> result) => result.Success;
}

public static class ValidationResultExtensions
{
    /// <summary>
    /// Throws validation-exception when validation failed.
    /// </summary>
    public static T ThrowIfValidationFailed<T>(this (T Value, ILookup<bool, IValidationResult<T>> Results) lookup)
    {
        return
            lookup.Results[false].Any()
                ? throw DynamicException.Create
                (
                    $"{typeof(T).ToPrettyString()}Validation",
                    $"Object does not meet one or more requirements.{Environment.NewLine}{Environment.NewLine}" +
                    $"{lookup.Results[false].Select(Func.ToString).Join(Environment.NewLine)}"
                )
                : default(T);
    }
}

Helpers

To check wheter a type is a closure, I use this extension:

internal static class TypeExtensions
{
    public static bool IsClosure(this Type type)
    {
        return
            type.Name.StartsWith("<>c__DisplayClass") &&
            type.IsDefined(typeof(CompilerGeneratedAttribute));
    }
}

And a couple more for creating expressions:

internal static class ValidationExpressionFactory
{
    public static LambdaExpression ReferenceEqualNull<T>()
    {
        return ReferenceEqualNull<T>(Expression.Parameter(typeof(T)));
    }

    public static LambdaExpression ReferenceEqualNull<T>(Expression<Func<T>> expression)
    {
        // x => object.ReferenceEqual(x.Member, null)

        // This is tricky because the original expression is () => (<>c__DisplayClass).x.y.z
        // We first need to the closure and inject out parameter there.
        var member = ValidationClosureSearch.FindParameter(expression);
        var parameter = Expression.Parameter(member.Type);
        var expressionWithParameter = ValidationParameterInjector.InjectParameter(expression.Body, parameter);
        return ReferenceEqualNull<T>(parameter, expressionWithParameter);
    }

    private static LambdaExpression ReferenceEqualNull<T>(ParameterExpression parameter, Expression value = default)
    {
        // x => object.ReferenceEqual(x, null)
        return
            Expression.Lambda(
                Expression.ReferenceEqual(
                    value ?? parameter,
                    Expression.Constant(default(T))),
                parameter
            );
    }

    public static LambdaExpression Negate(LambdaExpression expression)
    {
        // !x
        return
            Expression.Lambda(
                Expression.Not(expression.Body),
                expression.Parameters
            );
    }
}

Expression visitors

With this one I search for closures to replace them with a parameter as validation expression don't have them, e.g: .NotNull(() => x.FirstName))

/// <summary>
/// Searches for the member of the closure class.
/// </summary>
internal class ValidationClosureSearch : ExpressionVisitor
{
    private MemberExpression _closure;

    public static MemberExpression FindParameter(Expression expression)
    {
        var parameterSearch = new ValidationClosureSearch();
        parameterSearch.Visit(expression);
        return parameterSearch._closure;
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.Expression.Type.IsClosure())
        {
            _closure = node;
        }

        return base.VisitMember(node);
    }
}

Once I've found it, I use this one to replace that closures with actual parameters:

/// <summary>
/// Injects the specified parameter to replace the closure.
/// </summary>
public class ValidationParameterInjector : ExpressionVisitor
{
    private readonly ParameterExpression _parameter;

    private ValidationParameterInjector(ParameterExpression parameter) => _parameter = parameter;

    public static Expression InjectParameter(Expression expression, ParameterExpression parameter)
    {
        return new ValidationParameterInjector(parameter).Visit(expression is LambdaExpression lambda ? lambda.Body : expression);
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        var isClosure =
            node.Type == _parameter.Type &&
            node.Expression.Type.IsClosure();

        return
            isClosure
                ? _parameter
                : base.VisitMember(node);
    }
}

The last one is used to prettify validation expressions for display by injecting good looking type names.

  • before: "Param_0.FirstName"
  • after: "<param:Person>.FirstName>"
// We don't want to show the exact same expression as the condition
// because there are variables and closures that don't look pretty.
// We replace them with more friendly names.
internal class ValidationParameterPrettifier : ExpressionVisitor
{
    private readonly ParameterExpression _originalParameter;
    private readonly ParameterExpression _prettyParameter;

    private ValidationParameterPrettifier(ParameterExpression originalParameter, ParameterExpression prettyParameter)
    {
        _originalParameter = originalParameter;
        _prettyParameter = prettyParameter;
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        return node.Equals(_originalParameter) ? _prettyParameter : base.VisitParameter(node);
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        // Extract member name from closures.
        return
            node.Expression is ConstantExpression
                ? Expression.Parameter(node.Type, node.Member.Name)
                : base.VisitMember(node);
    }

    protected override Expression VisitUnary(UnaryExpression node)
    {
        // Remove type conversion, this is change (Convert(<T>) != null) to (<T> != null)
        return
            node.Operand.Type == _originalParameter.Type
                ? Expression.Parameter(node.Operand.Type, _prettyParameter.Name)
                : base.VisitUnary(node);
    }

    public static Expression Prettify<T>([NotNull] LambdaExpression expression)
    {
        if (expression == null) throw new ArgumentNullException(nameof(expression));

        return
            expression
                .Parameters
                .Aggregate(expression.Body, (e, p) => new ValidationParameterPrettifier(expression.Parameters[0], CreatePrettyParameter<T>()).Visit(expression.Body));
    }

    public static ParameterExpression CreatePrettyParameter<T>()
    {
        return Expression.Parameter(typeof(T), $"<param:{typeof(T).ToPrettyString()}>");
    }
}

That's it.


Questions

  • would you say it meets my own requirements?
  • would you say any requirements or features are missing?
  • is there anything else I can improve?
\$\endgroup\$
5
  • \$\begingroup\$ Current commit: Flawless (it's how I call it) \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 17:41
  • \$\begingroup\$ Which use cases you see for this API? End-user validation or easy validation logic for application and API developers? \$\endgroup\$
    – dfhwze
    Jun 22, 2019 at 17:57
  • \$\begingroup\$ @dfhwze for now just application and API developers (as there is no localization for messages). \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 17:58
  • \$\begingroup\$ But you would like this to be a framework for end-users eventually, right? \$\endgroup\$
    – dfhwze
    Jun 22, 2019 at 17:59
  • \$\begingroup\$ @dfhwze that'd be cool... \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 18:01

3 Answers 3

5
\$\begingroup\$

I like the idea, but I'm in line with dfhwze meaning it's a little too verbose and complicated to follow, especially when unable to debug.

I would prefer a more simple pattern like the one dfhwze suggests:

  var result =
    Tester // the person
    .Validate()
    .NotNull(p => p.LastName, "LastName is Null")
    .IsTrue(p => p.FirstName.Length > 3, "FirstName is too short")
    .Match(p => p.Address.Street, @"^Sesa(m|n)e Street$", "Street name is invalid");

  Console.WriteLine(result);

This can be implemented in a lightweight way like the below, where I use a Railway Orientend Programming-ish pattern:

  public abstract class ValidateResult<T>
  {
    public ValidateResult(T source)
    {
      Source = source;
    }

    public T Source { get; }
  }

  public class Success<T> : ValidateResult<T>
  {
    public Success(T source) : base(source)
    {
    }

    public override string ToString()
    {
      return "Everything is OK";
    }
  }

  public class Failure<T> : ValidateResult<T>
  {
    public Failure(T source, string message) : base(source)
    {
      Message = message;
    }

    public string Message { get; }

    public override string ToString()
    {
      return $"Error: {Message}";
    }
  }

  public static class Validation
  {
    public static ValidateResult<T> Validate<T>(this T source)
    {
      return new Success<T>(source);
    }

    private static ValidateResult<T> Validate<T>(this ValidateResult<T> result, Predicate<T> predicate, string errorMessage)
    {
      if (result is Success<T> success)
      {
        if (!predicate(success.Source))
          return new Failure<T>(success.Source, errorMessage);
      }

      return result;
    }

    public static ValidateResult<T> NotNull<T, TMember>(this ValidateResult<T> result, Expression<Func<T, TMember>> expression, string errorMessage)
    {
      var getter = expression.Compile();
      Predicate<T> predicate = source => getter(source) != null;
      return Validate(result, predicate, errorMessage);
    }

    public static ValidateResult<T> IsTrue<T>(this ValidateResult<T> result, Expression<Func<T, bool>> expression, string errorMessage)
    {
      var predicate = new Predicate<T>(expression.Compile());
      return Validate(result, predicate, errorMessage);
    }

    public static ValidateResult<T> Match<T>(this ValidateResult<T> result, Expression<Func<T, string>> expression, string pattern, string errorMessage)
    {
      var getter = expression.Compile();
      Predicate<T> predicate = source => Regex.IsMatch(getter(source), pattern);
      return Validate(result, predicate, errorMessage);
    }
  }

The idea of the ROP pattern is that the first failure stops any further validation, but without throwing or any other error handling mechanism. You end up in the same place as if everything were OK, and can evaluate the result in one place. If you want to collect all possible failures, you can easily extent ValidateResult<T> with a collection of ValidateResult<T>s and then validate through the chain no matter what each result is.

IMO it's easy to follow, maintain and extent - for instance with the ability to be able to distinguish between degrees of failure. You could for instance implement a Warning<T> : ValdiateResult<T>.


Update

As t3chb0t (kindly I believe) emphasizes in his comment, I missed that he wants to have predefined validation rules. The above pattern can easily accommodate that requirement:

  public class Validator<T>
  {
    List<Func<ValidateResult<T>, ValidateResult<T>>> m_rules = new List<Func<ValidateResult<T>, ValidateResult<T>>>();

    public ValidateResult<T> Validate(T source)
    {
      ValidateResult<T> result = source.Validate();
      foreach (var rule in m_rules)
      {
        result = rule(result);
      }

      return result;
    }

    internal void AddRule(Predicate<T> predicate, string errorMessage)
    {
      Func<ValidateResult<T>, ValidateResult<T>> rule = result =>
      {
        if (result is Success<T> success)
        {
          if (!predicate(success.Source))
            return new Failure<T>(success.Source, errorMessage);
        }

        return result;
      };
      m_rules.Add(rule);
    }
  }

Extended with validation rules:

  public static class Validation
  {
    public static ValidateResult<T> ValidateWith<T>(this T source, Validator<T> validator)
    {
      return validator.Validate(source);
    }


    public static Validator<T> NotNull<T, TMember>(this Validator<T> validator, Expression<Func<T, TMember>> expression, string errorMessage)
    {
      var getter = expression.Compile();
      Predicate<T> predicate = source => getter(source) != null;
      validator.AddRule(predicate, errorMessage);
      return validator;
    }

    public static Validator<T> IsTrue<T>(this Validator<T> validator, Expression<Func<T, bool>> expression, string errorMessage)
    {
      var predicate = new Predicate<T>(expression.Compile());
      validator.AddRule(predicate, errorMessage);
      return validator;
    }

    public static Validator<T> Match<T>(this Validator<T> validator, Expression<Func<T, string>> expression, string pattern, string errorMessage)
    {
      var getter = expression.Compile();
      Predicate<T> predicate = source => Regex.IsMatch(getter(source), pattern);
      validator.AddRule(predicate, errorMessage);
      return validator;
    }
  }

And the same use case:

  Validator<Person> validator = new Validator<Person>();

  validator
    .NotNull(p => p.LastName, "LastName is Null")
    .IsTrue(p => p.FirstName.Length > 3, "FirstName is too short")
    .Match(p => p.Address.Street, @"^Sesa(m|n)e Street$", "Street name is invalid");

  var result = Tester.ValidateWith(validator);

  if (result is Success<Person> success)
  {
    Console.WriteLine(success);
  }
  else if (result is Failure<Person> failure)
  {
    Console.WriteLine(failure);
  }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ nice! I've heard about ROP once here... but then forgot about it and spent like 2h today thinking about how to solve failures of preconditions and break the chain. \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 21:26
4
\$\begingroup\$

As developer consuming your API ..

Usability

I find this a verbose way of constructing validation rules.

var rules = ValidationRuleCollection
        .For<Person>()
        .Add(x =>
            ValidationRule
                .Require
                .NotNull(x))
        .Add(x =>
            ValidationRule
                .Require
                .NotNull(() => x.FirstName))
        .Add(x =>
            ValidationRule
                .Ensure
                .True(() => x.FirstName.Length > 3));

var (person, results) = default(Person).ValidateWith(rules);

I would like to able to call this like:

Tester.Require()
      .NotNull("I want to be able to provide my own error message")
      .NotNull(x => x.FirstName)
      .Ensure(x => x.FirstName.Length > 3)
      .Validate();

Extensibility

  • I would like to provide my own error messages and fallback to default messages if I don't specity any
  • I would like to be able to not only define pass/fail - true/false validations, but I would also like to provide a severity (error, warning, alert, ..)

General Issues

  • I feel your APIs are always well written, but also pretty complex/verbose. This is a small setback in intuitive use.
\$\endgroup\$
7
  • 1
    \$\begingroup\$ I think I could add a couple of shortcut extensions to reduce the verbosity; about the custom message... sorry, I fogot to mention it in the description :-( there are default ones that the user can override with Message. It's sometimes hard to find the balance between verbose and extendable and at the same time to avoid creating one hundred builders so that you can write Rule.For.This.Crazy.Property.NotNull.And.Whatever ;-) from now on, I'll call it a flood-API in contrast to just fluent-one :-P \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 18:22
  • 1
    \$\begingroup\$ The problem you are describing trying to find a balance is what i call making a sophisticated API. This means, simple and most common use cases should be very easy to write, while allowing for complex usages if the user should decide to require so. This is a very hard exercise to get right :) Indeed, if you would add shortcut extensions, this would help tremendously for the most common use cases. \$\endgroup\$
    – dfhwze
    Jun 22, 2019 at 18:25
  • 1
    \$\begingroup\$ From the available APIs I can add two more extensions and zip it to ValidationRuleCollection.For<Person>().Require((r, x) => r.NotNull(() => x)).Ensure((r, x) => r.NotNull(() => x.FirstName)). I'll see how I can rar it ;-] \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 18:38
  • \$\begingroup\$ Since I did not make a thorough review and no other answers are available yet, you can still edit the question if you want to. \$\endgroup\$
    – dfhwze
    Jun 22, 2019 at 18:49
  • \$\begingroup\$ If I add this new API to the question it'll invalidate your answer because they are very similar. \$\endgroup\$
    – t3chb0t
    Jun 22, 2019 at 18:51
2
\$\begingroup\$

(self-answer)


Simplified the API

You were right, the API was too verbose so I drastically simplified it. It now presents itself like that:

    [Fact]
    public void Simplified()
    {
        var rules =
            ValidationRuleCollection
                .For<Person>()
                .Reject(b => b.Null(x => x).Hard())
                .Reject(b => b.NullOrEmpty(x => x.FirstName))
                .Accept(b => b.Pattern(x => x.FirstName, "^cookie", RegexOptions.IgnoreCase))
                .Accept(b => b.When(x => x.FirstName.Length > 3));

        var results = default(Person).ValidateWith(rules);

        Assert.Equal(0, results.OfType<Information>().Count());
        Assert.Equal(1, results.OfType<Error>().Count());

        
        Assert.ThrowsAny<DynamicException>(() => default(Person).ValidateWith(rules).ThrowIfValidationFailed());
    }

I was not able to incorporate the ROP here this time, but the many other ideas where very helpful. Here's the summary:

Changes

There are only two main extensions Accept that enforces a rule and Reject that negates it (internally), so there is no need for other extensions prefixed with Not.

I also liked @Flater's idea from the other question by @Henrik Hansen where it was suggested picking something more general than IsTrue/IsFalse etc. I decided here to use When. All other extensions have only one overload now. Consequently I could rename Match to Pattern for RegEx.

The last extension, or rather a modifier is called Hard. This one yields a different rule, one that when failed aborts the validation. This also means that I now have two rules which are descendants of the abstract class ValidationRule<T, TContext>:

public class Hard<T, TContext> : ValidationRule<T, TContext>
{
    public Hard
    (
        [NotNull] Expression<ValidationPredicate<T, TContext>> predicate,
        [NotNull] Expression<MessageCallback<T, TContext>> message
    ) : base(predicate, message) { }

    protected override IValidationResult CreateResult(bool success, string expression, string message)
    {
        return
            success
                ? (IValidationResult)new Information(expression, message)
                : (IValidationResult)new Error(expression, message);
    }
}

public class Soft<T, TContext> : ValidationRule<T, TContext>
{
    public Soft
    (
        [NotNull] Expression<ValidationPredicate<T, TContext>> predicate,
        [NotNull] Expression<MessageCallback<T, TContext>> message
    ) : base(predicate, message) { }

    protected override IValidationResult CreateResult(bool success, string expression, string message)
    {
        return
            success
                ? (IValidationResult)new Information(expression, message)
                : (IValidationResult)new Warning(expression, message);
    }
}

When evaluated they return one of three possible results: Information, Warning, Error.

public class Information : ValidationResult
{
    public Information([NotNull] string expression, [NotNull] string message)
        : base(expression, message) { }
}

public class Warning : ValidationResult
{
    public Warning([NotNull] string expression, [NotNull] string message)
        : base(expression, message) { }
}

public class Error : ValidationResult
{
    public Error([NotNull] string expression, [NotNull] string message)
        : base(expression, message) { }
}

An internal API uses it to break the validation:

    public static ValidationResultCollection<T> ValidateWith<T, TContext>(this T obj, IImmutableList<IValidationRule<T, TContext>> rules)
    {
        return obj.ValidateWith(rules, default);
    }

    private static IEnumerable<IValidationResult> Evaluate<T, TContext>(this IImmutableList<IValidationRule<T, TContext>> rules, T obj, TContext context)
    {
        var result = default(IValidationResult);
        foreach (var rule in rules)
        {
            yield return result = rule.Evaluate(obj, context);
            if (result is Error) yield break;
        }
    }

There is also a new ValidationResultCollection that replaces the tuple I used previously:

public class ValidationResultCollection<T> : IEnumerable<IValidationResult>
{
    private readonly IImmutableList<IValidationResult> _results;

    public ValidationResultCollection(T value, IImmutableList<IValidationResult> results)
    {
        Value = value;
        _results = results;
    }

    public T Value { get; }

    public IEnumerator<IValidationResult> GetEnumerator() => _results.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

    public static implicit operator T(ValidationResultCollection<T> results) => results.Value;
}

I use it to chain extensions for throwing exception:

default(Person)
    .ValidateWith(rules) // <-- ValidationResultCollection
    .ThrowIfValidationFailed(); // autocast to T or throw

Generating messages internally still has to be improved, but as far as the main API is concerned, I'm happy with it.

End-users can add thier own extension to b. It is a ValidationRuleBuilder<T, TContext> that lets them modify the validation expression: (like I use it for

    public ValidationRuleBuilder<T, TContext> Predicate(Func<LambdaExpression, LambdaExpression> expression)
    {
        _predicate = expression(_predicate);
        return this;
    }
    

I use this too, e.g. for Reject, that Negates the expression:

    public static IImmutableList<IValidationRule<T, object>> Reject<T>
    (
        this IImmutableList<IValidationRule<T, object>> rules,
        Func<ValidationRuleBuilder<T, object>, ValidationRuleBuilder<T, object>> builder
    )
    {
        return rules.Add(builder(ValidationRule<T, object>.Ensure).Negate().Build());
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.