码迷,mamicode.com
首页 > 其他好文 > 详细

一些有意思的库

时间:2017-05-31 22:15:30      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:custom   inner   please   tco   failure   idp   bst   ase   github   

FluentValidation

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;

一些有意思的库

标签:custom   inner   please   tco   failure   idp   bst   ase   github   

原文地址:http://www.cnblogs.com/s5689412/p/6925938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!