示例#1
0
        private IObjectValidator GetValidator(IList <Book> books = null, IList <Publisher> publishers = null)
        {
            var builder = new ValidationBuilder <Book>();

            builder.RuleFor(book => book.ISBN)
            .Must(ISBNUtils.IsValid)
            .WithMessage("ISBN is not valid");
            builder.RuleFor(book => book.ISBN)
            .Must(isbn => publishers == null ||
                  publishers.Any(publisher => publisher.Id == ISBNUtils.GetPublisherId(isbn)))
            .WithMessage("Publisher that owns such ISBN does not exist")
            .Must(isbn => books == null ||
                  !books.Any(book => book != this && book.ISBN == isbn))
            .WithMessage("ISBN should be unique")
            .AllWhen(book => ISBNUtils.IsValid(book.ISBN));
            builder.RuleFor(book => book.Name)
            .NotEmpty()
            .WithMessage("Name can't be empty");
            builder.RuleFor(book => book.AgeRestriction)
            .NotEmpty()
            .WithMessage("Age Restriction can't be empty")
            .Matches(@"^\d+\+$")
            .WithMessage("Age Restriction has wrong format");

            return(builder.Build(this));
        }
示例#2
0
 public void OnISBNChanged()
 {
     if (ISBNUtils.IsValid(ISBN))
     {
         PublisherId = ISBNUtils.GetPublisherId(ISBN);
     }
 }