Skip to content

Instantly share code, notes, and snippets.

View JayGhb's full-sized avatar
🏠
Working from home

Jason Manoloudis JayGhb

🏠
Working from home
  • https://datawise.ai/
  • Greece
View GitHub Profile
@JayGhb
JayGhb / ValidationUnitTests.cs
Created August 9, 2023 10:34
ValidationUnitTests
public class CreateCustomerCommandValidatorUnitTests
{
private CreateCustomerCommandValidator validator;
public CreateCustomerCommandValidatorUnitTests()
{
validator = new CreateCustomerCommandValidator();
}
[Fact]
@JayGhb
JayGhb / ValidationExceptionHandling.cs
Last active August 10, 2023 11:53
Validation Exceptions handling to combine with existing exception handling
if (exception is ValidationException validationException)
{
string[] validationErrorMessages = validationException.Errors.Select(x => x.ErrorMessage).ToArray();
ValidationProblemDetails problemDetails = new ValidationProblemDetails
{
Instance = req.Path,
Status = StatusCodes.Status400BadRequest,
Title = "One or more validation errors occurred.",
Detail = $"{string.Join('-', validationErrorMessages)}"
@JayGhb
JayGhb / ValidationBehavior.cs
Created May 1, 2023 11:20
Simple validation behavior using FluentValidation and MediatR
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public ValidatorBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators ?? throw new ArgumentNullException(nameof(validators));
}