Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:34:29+00:00 2026-06-14T03:34:29+00:00

Can I add more than one validator to an object? For example: public interface

  • 0

Can I add more than one validator to an object? For example:

public interface IFoo
{
    int Id { get; set; }
    string Name { get; set; }
}

public interface IBar
{
    string Stuff { get; set; }
}

public class FooValidator : AbstractValidator<IFoo>
{
    public FooValidator ()
    {
        RuleFor(x => x.Id).NotEmpty().GreaterThan(0);
    }
}

public class BarValidator : AbstractValidator<IBar>
{
    public BarValidator()
    {
        RuleFor(x => x.Stuff).Length(5, 30);
    }
}

public class FooBar : IFoo, IBar
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Stuff { get; set; }
}

public class FooBarValidator : AbstractValidator<FooBar>
{
    public FooBarValidator()
    {
        RuleFor(x => x)
            .SetValidator(new FooValidator())
            .SetValidator(new BarValidator());
    }
}

Running the test.

FooBarValidator validator = new FooBarValidator();
validator.ShouldHaveValidationErrorFor(x => x.Id, 0);

I get an InvalidOperationException:

Property name could not be automatically determined for expression x => x. Please specify either a custom property name by calling ‘WithName’.

Is there any way to implement this or am I trying to use FluentValidation in a way that it’s not meant to be used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:34:30+00:00Added an answer on June 14, 2026 at 3:34 am

    RuleFor is trying to create a property-level rule. You can additionally use the AddRule function to add a general-purpose rule.

    Using this, I created a composite rule proof of concept. It takes in a set of other validators and runs them. The yield break code came straight from FluentValidator‘s DelegateValidator. I wasn’t sure what to do with it so I grabbed that from the source. I didn’t trace its full purpose, but everything seems to work as is 🙂

    Code

    public interface IFoo
    {
        int Id { get; set; }
        string Name { get; set; }
    }
    
    public interface IBar
    {
        string Stuff { get; set; }
    }
    
    public class FooValidator : AbstractValidator<IFoo>
    {
        public FooValidator()
        {
            RuleFor(x => x.Id).NotEmpty().GreaterThan(0);
        }
    }
    
    public class BarValidator : AbstractValidator<IBar>
    {
        public BarValidator()
        {
            RuleFor(x => x.Stuff).Length(5, 30);
        }
    }
    
    public class FooBar : IFoo, IBar
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Stuff { get; set; }
    }
    
    public class CompositeValidatorRule : IValidationRule
    {
        private IValidator[] _validators;
    
        public CompositeValidatorRule(params IValidator[] validators)
        {
            _validators = validators;
        }
    
        #region IValidationRule Members
        public string RuleSet
        {
            get; set;
        }
    
        public IEnumerable<ServiceStack.FluentValidation.Results.ValidationFailure> Validate(ValidationContext context)
        {
            var ret = new List<ServiceStack.FluentValidation.Results.ValidationFailure>();
    
            foreach(var v in _validators)
            {
                ret.AddRange(v.Validate(context).Errors);
            }
    
            return ret;
        }
    
        public IEnumerable<ServiceStack.FluentValidation.Validators.IPropertyValidator> Validators
        {
            get { yield break; }
        }
        #endregion
    }
    
    public class FooBarValidator : AbstractValidator<FooBar>
    {
        public FooBarValidator()
        {
            AddRule(new CompositeValidatorRule(new FooValidator(), new BarValidator()));
        }
    }
    

    Base Test Case:

        [TestMethod]
        public void TestValidator()
        {
            FooBarValidator validator = new FooBarValidator();
            var result = validator.Validate(new FooBar());
    
        }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that in IE9 I can add more than one image in a
how can I add more than one key to a json file? NSArray *keys
I know that I can add more than one project to a solution, but
I tried to add more than one twitter and facebook accounts from my page
I want to add check for more than one values in where clause. Db_RecordExists
Is it possible to add more than one instances of Static HTML: iframe tabs
How to add more than one parameter in Twig path? Say you have this
Can I add more permit to a semaphore in Java? Semaphore s = new
I know one can add event listener for window.error. However when working with Iframes,
I have table to collect Contacts information if contact have more than one contact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.