F #은 유창한 DSL을 지원하며 유창한 API를 갖춘 여러 F # 라이브러리가 있습니다. F #의의 형식 시스템은 C#을에서 약간 다르다 '의, 그리고 차이의 대부분은 팝업 유창하게 API를 사용하지만, 여전히,이 작품 :
#r @"C:\Users\Ramon\Downloads\FluentValidation\FluentValidation\FluentValidation.dll"
open System
open FluentValidation
type Customer =
{ Surname : string
Forename : string
Company : string
Discout : int
Address : string
Postcode : string
Discount : int
HasDiscount : bool }
type IRuleBuilder<'T,'Property> with
member __.Ignore =()
type CustomerValidator =
inherit AbstractValidator<Customer>
new() =
let beAValidPostcode postcode = true
base.RuleFor(fun customer -> customer.Surname).NotEmpty().Ignore
base.RuleFor(fun customer -> customer.Forename).NotEmpty().WithMessage("Please specify a first name").Ignore
base.RuleFor(fun customer -> customer.Company).NotNull().Ignore
base.RuleFor(fun customer -> customer.Discount).NotEqual(0).When(fun customer -> customer.HasDiscount).Ignore
base.RuleFor(fun customer -> customer.Address).Length(20, 250).Ignore
base.RuleFor(fun customer -> customer.Postcode).Must(beAValidPostcode).WithMessage("Please specify a valid postcode").Ignore
{ }
한 가지 가능한 방법 대신에 처음부터 다시 작성하는 F 번호를 작성하는 것 FnUnit이 NUnit을 위해 한 행을 따라 라이브러리 맨 위에있는 DSL. – Mathias
@Mathias 나는 그것을 시도했지만 아직 FSharp에서 아기 단계를 밟고 있기 때문에 모든 람다식이 FSharp 대화 형 식에서 나에게 외계인이된다. FSharp에서 FluentValidation 코드의 샘플을 제공 할 수 있다면 좋을 것입니다. – kunjee
Ramon Snir의 대답은 F #의 FluentValidation 라이브러리로 작업하는 실용적인 방법을 보여줍니다 (아마도 Ignore Extension을 End로 변경했을 것입니다). Mauricio Scheffer는 훌륭한 F # (및 C#) 기반 유효성 검증 예제를 제공합니다. [F #의 적용 가능한 펑터로 검증] (http://bugsquash.blogspot.co.uk/2011/08/validating-with-applicative-functors-in.html)) –