Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2662 from mrhmouse/better-error-message-when-more…
Browse files Browse the repository at this point in the history
…-than-one-fluent-validator-exists

Use a better error message when picking a fluent validator is ambiguous
  • Loading branch information
thecodejunkie committed Dec 29, 2016
2 parents 692196b + 5ae6d59 commit c91bfc6
Showing 1 changed file with 16 additions and 2 deletions.
Expand Up @@ -45,9 +45,23 @@ private IValidator GetValidatorInstance(Type type)
{
var fullType =
CreateValidatorType(type);
var available = this.validators
.Where(validator => fullType.GetTypeInfo().IsAssignableFrom(validator.GetType()))
.ToArray();

return this.validators
.SingleOrDefault(validator => fullType.GetTypeInfo().IsAssignableFrom(validator.GetType()));
if (available.Length > 1)
{
var names = string.Join(", ", available.Select(v => v.GetType().Name));
var message = string.Concat(
"Ambiguous choice between multiple validators for type ",
type.Name,
". The validators available are: ",
names);

throw new InvalidOperationException(message);
}

return available.FirstOrDefault();
}

private static Type CreateValidatorType(Type type)
Expand Down

0 comments on commit c91bfc6

Please sign in to comment.