Skip to content

Commit

Permalink
Move to the advanced section and some re-wording
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Jan 26, 2023
1 parent 97c1c60 commit b3eb55e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
37 changes: 37 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,40 @@ RuleFor(x => x.Surname).Custom((x, context) =>
}
});
```

## Customizing the Validation Exception

If you use the `ValidateAndThrow` method to [throw an exception when validation fails](start.html#throwing-exceptions) FluentValidation will internally throw a `ValidationException`. You can customzie this behaviour so a different exception is thrown by overriding the `RaiseValidationException` in your validator.

This simplistic example wraps the default `ValidaitionException` in an `ArgumentException` instead:

```csharp
protected override void RaiseValidationException(ValidationContext<T> context, ValidationResult result)
{
var ex = new ValidationException(result.Errors);
throw new ArgumentException(ex.Message, ex);
}
```

This approach is useful if you always want to throw a specific custom exception type every time `ValidateAndThrow` is invoked.

As an alternative you could create your own extension method that calls `Validate` and then throws your own custom exception if there are validation errors.


```csharp
public static class FluentValidationExtensions
{
public static void ValidateAndThrowArgumentException<T>(this IValidator<T> validator, T instance)
{
var res = validator.Validate(instance);

if (!res.IsValid)
{
var ex = new ValidationException(res.Errors);
throw new ArgumentException(ex.Message, ex);
}
}
}
```

This approach is more useful if you only want to throw the custom exception when your specific method is invoked, rather than any time `ValidateAndThrow` is invoked.
43 changes: 1 addition & 42 deletions docs/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,48 +139,7 @@ validator.Validate(customer, options =>
});
```

# Customize exception thrown
If you want, you can customize which exceptions are thrown on validation errors. From the outset, you can define this behaviour in the base-class of your validators, or you can write your own extension-method. Both strategies builds on top of the existing code base.


1. Create your own extension method that calls the validation and throws exception if there are validation errors.
2. Create a subclass of `AbstractValidator`

for (1) the implementation could be

```
public static class FluentValidationExtensions
{
public static void ValidateAndThrowArgumentException<T>(this IValidator<T> validator, T instance)
{
var res = validator.Validate(instance);
if (!res.IsValid)
{
var ex = new ValidationException(res.Errors);
throw new ArgumentException(ex.Message, ex);
}
}
}
```

for (2) The implementation could be

```
public class ThrowArgumentExceptionValidator<T> : AbstractValidator<T>
{
protected override void RaiseValidationException(ValidationContext<T> context, ValidationResult result)
{
var ex = new ValidationException(result.Errors);
throw new ArgumentException(ex.Message, ex);
}
}
```

(1) is perhaps the least intrusive, as the validation implementations themselves are untouched. Only the explicit calls to the validation framework are affected. Hence the same rules can be applied in cases where an exception should be thrown and in other cases where they should not.

(2) Is more invasive in all validation implementations by requiring a new base-class. On The other hand, it may be a natural place for company specific customizations.

It is also possible to customize type of exception thrown, [which is covered in this section](advanced.html#customizing-the-validation-exception).

# Complex Properties

Expand Down

0 comments on commit b3eb55e

Please sign in to comment.