Created
June 28, 2016 21:31
-
-
Save inzi/e8124e110085b51b40e10c590529f443 to your computer and use it in GitHub Desktop.
FluentValidationNotShowingErrors
This file contains hidden or 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
namespace rms.Models.Validators | |
{ | |
public class SourceCodeEditorValidator : AbstractValidator<sourceCode> | |
{ | |
public SourceCodeEditorValidator() | |
{ | |
RuleFor(x => x.sourceCodeName) | |
.NotEmpty().WithMessage("A code is required!") | |
.Length(2, 16).WithMessage("The code must be between 3 and 16 characters in length, please.") | |
.Matches(@"^[a-zA-Z0-9]*$").WithMessage("Numbers and letters only, please!"); | |
// For different clients, we can put all messaging into a class that has defaults in JSON form | |
// then, AMs can override JSON values to customize messages, etc. for the client. | |
When(z => z.sourceCodeExp.HasValue == true, () => | |
{ | |
RuleFor(x => x.sourceCodeExp) | |
.GreaterThan(DateTime.Now) | |
.WithMessage("Must be date in the future!"); | |
}); | |
RuleFor(x => x.sourceCodeDescription).NotNull().WithMessage("Please describe this discount code in detail in more than 5 and under 200 characters"); | |
When(x => !String.IsNullOrEmpty(x.sourceCodeDescription), () => | |
{ | |
When(x => x.sourceCodeDescription.Length > 0, () => | |
{ | |
RuleFor(x => x.sourceCodeDescription).Length(5, 200) | |
.WithMessage("Please describe this discount code in detail in more than 5 and under 200 characters"); | |
RuleFor(x => x.sourceCodeDescription).Matches(@"^(?!.*</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>).*$") | |
.WithMessage("Please avoid using HTML tags! "); | |
RuleFor(x => x.sourceCodeDescription).Matches(@"^[a-zA-Z0-9,.\-\/\\!@#$%^&*()?' ]*$") | |
.WithMessage("Only human readable descriptions allowed, please!"); | |
}); | |
} | |
); | |
RuleFor(x => x.sourceCodeSelectionids) | |
.Must(x => x.Count > 0) | |
.WithMessage("You much choose at least one selection!"); | |
RuleFor(x => x.selectionsAndTimePeriods).SetCollectionValidator | |
( | |
new SelectionForSourceCodeValidator() | |
); | |
} | |
private bool BeDateInTheFuture(DateTime date) | |
{ | |
return date > DateTime.Now; | |
} | |
public class SelectionForSourceCodeValidator : AbstractValidator<SelectionForSourceCode> | |
{ | |
public SelectionForSourceCodeValidator() | |
{ | |
RuleFor(x => x.SelectionTimePeriods).SetCollectionValidator(new SelectionTimePeriodValidator()); | |
} | |
} | |
public class SelectionTimePeriodValidator : AbstractValidator<SelectionTimePeriod> | |
{ | |
public SelectionTimePeriodValidator() | |
{ | |
When(i => i.selected == true, () => | |
{ | |
// 1 - percentage, 2 - discount, 3 - fixed | |
When(x => x.sourceCodeDiscountTypeID == 1, () => | |
{ | |
// percentage | |
RuleFor(x => x.sourceCodeDiscountAmount).GreaterThanOrEqualTo(decimal.Parse(".01")).WithMessage("Some percent off is required!"); | |
RuleFor(x => x.sourceCodeDiscountAmount).LessThanOrEqualTo(decimal.Parse("100")).WithMessage("Discount is more than the value!"); | |
}); | |
When(x => x.sourceCodeDiscountTypeID == 2, () => | |
{ | |
//discount | |
RuleFor(x => x.sourceCodeDiscountAmount).GreaterThanOrEqualTo(0).WithMessage("Some Amount more than 0 is required!"); | |
RuleFor(x => x.sourceCodeDiscountAmount - x.maxDiscount).GreaterThanOrEqualTo(0).WithMessage("Amount should be less than actual price!"); | |
}); | |
When(x => x.sourceCodeDiscountTypeID == 3, () => | |
{ | |
//fixed | |
RuleFor(x => x.sourceCodeDiscountAmount).GreaterThanOrEqualTo(0).WithMessage("Some Amount more than 0 is required!"); | |
RuleFor(x => x.sourceCodeDiscountAmount - x.maxDiscount).GreaterThan(0).WithMessage("Amount should be less than actual price!"); | |
}); | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment