Last active
October 13, 2016 11:15
-
-
Save Fodsuk/175ff478c7bdaac83eed9774fff61b0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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