-
Notifications
You must be signed in to change notification settings - Fork 55
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
localDate.each(_.isAfter(LocalDate.now()) is true) does not work #81
Comments
Indeed Accord doesn't have any specific features for dates. You've actually run into three separate issues:
The second option (introducing the local variable) won't work because:
Finally, the third option will technically work but doesn't quite express what you mean as I understand it. What you want is to say "p.dateOfBirth has a value, the following expression has to be true: ...". You can do this in one of two ways: // Inline transform: simple, but may result in ugly descriptions:
p.dateOfBirth.map(_ isAfter LocalDate.now()).each is true
// ... which is why you should probably add a custom description:
p.dateOfBirth.map(_ isAfter LocalDate.now()).each as "Date of birth is in the future" is true The better solution would be to add proper validation rules, e.g.: import com.wix.accord._
import com.wix.accord.ViolationBuilder._
// See http://wix.github.io/accord/dsl.html#custom-combinators
def aFutureDate: Matcher[LocalDate] =
new NullSafeValidator(_ isAfter LocalDate.now(), _ -> "is not a future date")
implicit val myValidator = validator[Person] { p =>
// ...
p.dateOfBirth.each is aFutureDate
} I'm definitely amenable to adding matchers on dates (built-in JVM types; JODA support will probably have to go in its own module), pull requests are most welcome :-) |
Thank you for the in depth answer! And yes, I'd vote to adding Java date support (probably not so much Joda, I love it but it is now deprecated in favor of Java 8 native types). In the meantime I'm going to digest what you describe above. Can I suggest you add a few more complex examples to the Accord web site? For example,
Now that I read it, it makes perfect sense. I wouldn't not have gotten there on my own. (I guess what I'm saying is, more in-depth documentation on the use of |
As for date support, simply adding:
Would probably go a long way. |
Found a problem with macro expansion... When I try to use this:
I get a macro expansion exception:
So I end up replacing it with this, which just seems less... nice:
|
Can you tell me which Scala version you're using? I've had a bitch of a time getting code reprinting (the thing that failed, needed for automatic description generation) to work properly, and it's entirely possible I missed some subtle corner cases. |
We are using Scala 2.11.8. |
By the way... This is showing elsewhere, not just date related:
Also causes the same exception. I've filed this under #89. |
I've opened a separate issue (#90) for supporting native Java time/calendar functions, feedback is welcome. |
As I understand it, we're done here (all issues are either resolved or handled separately). @zbeckman any objection to my closing this issue? |
Confirmed that 0.7-SNAPSHOT is good as far as the map() issue. Close away! |
Thanks for the followup! |
It looks like Accord doesn't know about Java's date types:
Does not compile, so I thought I'd try this:
But this results in the following error:
I ended up doing this:
The text was updated successfully, but these errors were encountered: