7

I was trying to use FluentValidation library in F Sharp sample. But I got stuck as I can't even turn a simple C Sharp code to F Sharp code.

But then I thought that this wonderful library is just trying to bring functional bits of programming side to CSharp, so instead of using this I should create my own library in FSharp only. That will be easy and appropriate way.

So, I need a opinion in that, which way will be better. And if someone can create FSharp sample for this, that will be great. It is just for learning purpose as I mostly use fluent library in C#. And I like to go with them in F#.

3
  • One possible approach, instead of rewriting from scratch, would be to write a F# DSL on top of the library, along the line of what FsUnit did for NUnit.
    – Mathias
    Mar 24, 2013 at 5:45
  • @Mathias I was trying that, but I am still taking baby step in FSharp, so all lambda expression become alien for me in FSharp interactive. If you can give sample for FluentValidation code in FSharp that will be great.
    – kunjee
    Mar 24, 2013 at 5:51
  • 2
    Ramon Snir's answer shows a pragmatic way of working with the FluentValidation library in F# (I'd probably change the Ignore extension to End). Mauricio Scheffer has some great F# (and C#) based validation examples: Validating with applicative functors in F# Mar 24, 2013 at 10:49

1 Answer 1

10

F# supports fluent DSLs, and there are several F# libraries with a fluent API. F#'s type system is a bit different from C#'s, and most of the differences pop-up with fluent APIs, but still, this works:

#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
        { }
5
  • 1
    As well as the new/then constructor syntax you could also just reference the validator via the base keyword, see Fluent validation in F#. Note as shown above that the use of Quotations is no longer required as F# 3 supports LINQ expressions. Mar 24, 2013 at 11:12
  • @PhillipTrelford You are of course correct, I based my code on an old script of mine, which was more complex and thus required the new/then. Edited to simplify for the specific scenario. Yes - no need for quotations, at last!
    – Ramon Snir
    Mar 24, 2013 at 11:29
  • @PhillipTrelford - wait, are you telling me that e.g. fun customer -> customer.Surname is implicitly captured as a LINQ expression in base.RuleFor(fun customer -> customer.Surname)? Mar 24, 2013 at 15:04
  • 3
    @StephenSwensen absolutely in F# 3 LINQ expressions work implicitly as per C# 3 onwards :) It also means you can use other C# libraries based on LINQ expressions like Moq Mar 24, 2013 at 15:18
  • @PhillipTrelford - neat! unfortunately I haven't found the time yet to really explore all of the new F# 3.0 features, happy to learn this. Mar 24, 2013 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.