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
{ }