Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertThat Addition Nullable Value #172

Closed
gdycus opened this issue Sep 6, 2017 · 7 comments
Closed

AssertThat Addition Nullable Value #172

gdycus opened this issue Sep 6, 2017 · 7 comments
Labels

Comments

@gdycus
Copy link

gdycus commented Sep 6, 2017

The Children property can be null. If it is null this doesn't evaluate properly. I'm not sure but I assume if Adults is 1 and Children is null then Adults + Children = null. Is there a way to handle this scenario?

[AssertThat("(Adults + Children) <= MaxPeople", ErrorMessage = "Cannot exceed {MaxPeople} people")]

@gdycus
Copy link
Author

gdycus commented Sep 16, 2017

Does anyone have any suggestions? Thanks!

@jwaliszko
Copy link
Owner

jwaliszko commented Sep 16, 2017

It is one of the null-related traps.

Take a look at the following expression: 1 + NInt where NInt is of Nullable<int> type.

When evaluated by EA parser, it results in null (EA server-side parser follows C# rules in that matter).

image

image

Such an expression is then passed to browser. At client-side, there is no separate parser built by EA. Instead, eval() method is used (see implementation outline). The problem is JavaScript follows different rules when computing such an expression. Evaluation results in 1 this time.

image

As a workaround I propose to be explicit of how you treats nulls. The alternative expression below is supposed to work coherently at both sides:

((Adults == null ? 0 : Adults) + (Children == null ? 0 : Children)) <= MaxPeople

@gdycus
Copy link
Author

gdycus commented Sep 16, 2017

I'm getting a parser error

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ComponentModel.DataAnnotations.ValidationException: AssertThatValidator: validation applied to Adults field failed. ---> ExpressiveAnnotations.Analysis.ParseErrorException: Parse error on line 1, column 29: ... ? 0 : Children)) <= MaxPeople ... ^--- Argument types must match.

[AssertThat("(Adults + (Children == null ? 0 : Children)) <= MaxPeople", ErrorMessage = "Cannot exceed {MaxPeople} people")]
public short Adults { get; set; }
[AssertThat("(Adults + (Children == null ? 0 : Children)) <= MaxPeople", ErrorMessage = "Cannot exceed {MaxPeople} people")]
public short? Children { get; set; }
public int MaxPeople { get; set; }

@jwaliszko
Copy link
Owner

jwaliszko commented Sep 16, 2017

Indeed, implicit types conversion has been missing for operands of conditional operator... The fixed implementation is provided with the changeset fa765b3. Will be shipped within next version.

@gdycus
Copy link
Author

gdycus commented Sep 17, 2017 via email

@jwaliszko
Copy link
Owner

Most likely it should be released somewhere this week.

@jwaliszko
Copy link
Owner

jwaliszko commented Sep 20, 2017

Released with v2.9.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants