instance centric validation

Mar 19, 2010 at 3:38 PM
Edited Mar 19, 2010 at 4:05 PM

Check(c => c.LastName, "Contact Last Name").Required();

All validations documented in SpecExpress are property centric. Well how to realize the following scenario:

We have the ValidationSave method. In that method we are checking if a specific transaction is running for example a year-end accounts in a accounting application, during that time you must not create new booking entries. As result I would like to give a ValidationResult return, which is not bound to a specific property. Its just a generic message, which belogs to a specific service method.

Can you write a example code using Customer Rule or Customer Validator, to be able to get an Idea how to implement this requirement?

Kind regards, Marinko

Mar 19, 2010 at 9:07 PM


Validation rules on the object as a whole is type of rule is something that I use extensively. It's actually no different than a property rule:


I created some documentation that provides some examples at


public class CreditRequestSpecification: Validates<CreditRequest>
    public CreditRequestSpecification()
        Check(c => c.FirstName).Required();
        Check(c => c.LastName).Required();
        Check(c => c.Address).Required().Specification<AddressSpecification>();

        //Check Credit Score - example of "propertyless rule"
        Check(c => c, "Credit Score").Required().Expect( (contact,c) => CreditService.GetCreditScore(c) > 650, "Credit Score too low");

Hope this helps.




Mar 19, 2010 at 9:33 PM

Hi Alan

It's exactly I was looking for. Great to see, that you have already extended your documentation. Well job done :-)

Is there a way in

Check(c => c, "Credit Score").Required().Expect(ValidScore, "Credit Score tool low");

private bool ValidScore(CreditRequest request, string name)
    return CreditService.GetCreditScore(request) > 650;    

to use the Custom RuleValidator directly without to have defined an extension? Syntax were like

Check(c => c, "Credit Score").Required().Expect<ScoreRuleValidator>();

Kind regards,


Mar 19, 2010 at 10:11 PM

There is not a way to use a Custom Rule Validator without an Extension class. You have an interesting idea, but if you've already done the hard work by defining the Custom RuleValidator class, it's not much more to create the extension class. Also, you get the added readability of the Fluent Interface.


Check(c => c, "Credit Score").Required().IsCreditScoreGreaterThen(650);



Just curious as to why you're interested in this syntax? Did you find the documentation incomplete or difficult? We're committed to improving and expanding the documentation.


Mar 19, 2010 at 10:26 PM

Your documentation is very well written. Thanks a lot. The only reason I'm asking for that syntax is to not have to much Validation Rules in one class. I would like to have for each rule a separate class beacause of testing purpose and single responsibility principe. The specification class would just contain Check and Warn lines and not more. The RuleValidator class would also contain the localized message respectevely the ValidationResult.

But you are right. In this case I would always create an Extension. :-)