Last active
September 2, 2015 11:20
-
-
Save vbilopav/b2209c32aad3b9d134b3 to your computer and use it in GitHub Desktop.
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
// see more at: https://github.com/JeremySkinner/FluentValidation | |
using FluentValidation; | |
public class CustomerValidator: AbstractValidator<Customer> { | |
public CustomerValidator() { | |
RuleFor(customer => customer.Surname).NotEmpty(); | |
RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name"); | |
RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount); | |
RuleFor(customer => customer.Address).Length(20, 250); | |
RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode"); | |
} | |
private bool BeAValidPostcode(string postcode) { | |
// custom postcode validating logic goes here | |
} | |
} | |
Customer customer = new Customer(); | |
CustomerValidator validator = new CustomerValidator(); | |
ValidationResult results = validator.Validate(customer); | |
bool validationSucceeded = results.IsValid; | |
IList<ValidationFailure> failures = results.Errors; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment