Skip to content

Instantly share code, notes, and snippets.

@Fodsuk
Last active October 13, 2016 11:15
Show Gist options
  • Save Fodsuk/175ff478c7bdaac83eed9774fff61b0c to your computer and use it in GitHub Desktop.
Save Fodsuk/175ff478c7bdaac83eed9774fff61b0c to your computer and use it in GitHub Desktop.
using FluentValidation;
using FluentValidation.Results;
using Vanquis.Digital.Services.WebAPI.Requests;
using Microsoft.Extensions.Options;
using Vanquis.Digital.Services.WebAPI.Helper;
namespace Vanquis.Digital.Services.WebAPI.ModelValidators
{
public class TransactionRequestValidator : AbstractValidator<TransactionsRequest>
{
public int MaxTransactionRangeInDays;
public TransactionRequestValidator(IOptions<AppSettings> appSettings)
{
MaxTransactionRangeInDays = appSettings.Value.MaxTransactionRangeInDays;
RuleFor(x => x.To).NotNull();
RuleFor(x => x.From).NotNull();
Custom(ValidDateRange);
}
public ValidationFailure ValidDateRange(TransactionsRequest request)
{
if (!request.To.HasValue || !request.From.HasValue) return null;
if (request.To.Value < request.From.Value) return new ValidationFailure("To", "To should be greater then From.");
var exceedRequestRange = (request.To.Value - request.From.Value).Days > MaxTransactionRangeInDays;
if (exceedRequestRange) return new ValidationFailure("To", $"Range requested exceeds limit. ({MaxTransactionRangeInDays} days)");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment