jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

Class methods in RequiredIf Expression #146

Closed gigjacker closed 7 years ago

gigjacker commented 7 years ago

Hi - I'm perfectly happy to boil this down to short reproducible example if need be but I think this will probably be quite easy for you to see what I'm doing wrong.

Model

public class MyModel
{
     [RequiredIf("IsMyEnumValueCondition()"]
     public MyEnum MyEnumValue { get; set; }

     [RequiredIf("IsMyEnumValueCondition() && My2ndEnumValueIn(My2ndEnum.Opened, My2ndEnum.InProgress")]
     public My2ndEnum My2ndEnumValue { get; set; }

     public bool IsMyEnumValueCondition()
     {
            return (this.MyEnumValue == MyEnum.First ||
                 this.MyEnumValue == MyEnum.Second);
     }

     public bool My2ndEnumValueIn(params My2ndEnum[] my2ndEnumList)
     {
            return my2ndEnumList.Any(x => x == My2ndEnumValue);
     }
}

Enums

public enum MyEnum
{
     First,
     Second, 
     Third
}

public enum My2ndEnum
{
    Opened,
    InProgress,
    Closed
}

I'm getting an InvalidToken exception thrown. Is this sort of thing just not allowed or am I missing something?

Cheers, .pd.

jwaliszko commented 7 years ago

Hi, I see no error in your solution. It should work. Maybe there is a bug in EA. Do you have a complete stack trace? I cannot help, nor fix, anything now since my notebook has crashed. I'm looking for a new one. It can take up to 2 weeks.

jwaliszko commented 7 years ago

OK, I think I actually see the error - there is no closing bracket in My2ndEnumValueIn() function invocation, at the end of your expression, this should work:

[RequiredIf(@"IsMyEnumValueCondition() 
              && My2ndEnumValueIn(My2ndEnum.Opened, My2ndEnum.InProgress)")]

BTW: the exception details should denote which attribute has the broken expression, and point out the error position (line, column) within it.

gigjacker commented 7 years ago

Hi - thanks for getting to back to me. I'll put together a small reproducer.
... And what happened was it works just fine, so I'm gonna have to go find out what silly, tiny error I have in the actual code. Sorry, guys.

Hi - this is just for anybody else having this problem. In the end it was that the nuget package hadn't been updated. I was on EA 2.6.8. After updating to 2.9.2, the problem went away.