0

Sorry, If this question seems too broad, but I'm a little unsure how I should do this task.

I have some objects:

public class A
{
    public B b {get;set;}
    public C[] cList {get;set;}
}

public class B
{
    public string val1 {get;set;}
}

public class C
{
    public string val2 {get;set;}
}

And a set of validators:

public class AValidator : IValidator<A>
{
    IEnumerable<ValidationError> Validate(A obj){...}
}

public class BValidator : IValidator<B>
{
    IEnumerable<ValidationError> Validate(B obj){...}
}

public class CValidator : IValidator<C>
{
    IEnumerable<ValidationError> Validate(C obj){...}
}

Error looks like this:

public class ValidationError
{
    public int Code {get;set;}
    public string Message {get;set;}
}

I perform validation like this:

_validator.Validate(new A()).ThrowIfInvalid();

But here is the problem, how to reference invalid property and Serialize/Deserialize this reference to use it on client?

For example, if new A().c[2].val2 is invalid, I should be able to obtain this path on server and use it on client for my view (obtain PropertyInfo, and object).

Is there some extensions/libraries in C# which provide serializable references for properties? For example, like XPath in XML.

shA.t
  • 16,580
  • 5
  • 54
  • 111
eocron
  • 6,885
  • 1
  • 21
  • 50
  • 1
    You can use reflection to [get property value by name](https://stackoverflow.com/q/1196991/1997232). Otherwise your question is offtopic (asking for a library). – Sinatr Sep 04 '17 at 10:11
  • Why would it be invalid if you would validate it with correct object. You need design patterns to structure your classes. Check out Abstract factory, factory and bridge to check which will suit your scenario.http://www.dofactory.com/net/design-patterns – Amit Kumar Singh Sep 04 '17 at 10:20
  • I meant invalid in my business case. For example if name of person was provided as "sh*thead", I should rise validation error. Either way it is not part of the question. – eocron Sep 04 '17 at 10:39
  • Create a get; set; and define validation which you call and throw some custom business error – Kristóf Tóth Sep 04 '17 at 10:49
  • Umm, what is it supposed to mean? How it is related to question? Im not asking of how I should implement getter and setter, Im asking how I should refrence property of some object. – eocron Sep 04 '17 at 10:54

1 Answers1

0

Extend class ValidationError with PropertyPath property and prepend it on each parent validator

public class AValidator : IValidator<A>
{
    IEnumerable<ValidationError> Validate(A obj)
    {
        if(obj.b == null)
            yield return new ValidationError {Message = "b is null", PropertyPath = "a.b"};
        else
        { 
            var bresults = new BValidator().Validate(obj.b);
            foreach(var result in bresults)
            {
                result.PropertyPath = "a.b." + result.PropertyPath;
                yield return result;
            }
        }
...
            var cresults = new BValidator().Validate(obj.c[i]);
            foreach(var result in cresults)
            {
                result.PropertyPath = "a.c[i]." + result.PropertyPath;
                yield return result;
            }
    }        
}
ASpirin
  • 3,601
  • 1
  • 23
  • 32