You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm stuck trying to validate the following case class:
final case class Post(
tags: Seq[String]
)
For instance I must ensure no element in tags contains an empty string. For case classes it's obvious to just bring a corresponding validator into scope. But how does this work for primitive types? I cannot just write a Validator[String] because then this validator would be applied to all Strings, right? Or maybe I'm completely on the wrong track...
The text was updated successfully, but these errors were encountered:
Yes, your assumption about defining validator on String is correct - it would be applied to all strings, to that's not the best idea.
But you can do the following:
define separate value class case class Tag(value: String) extends AnyVal and define a validator for Tag type (usign ruleVC) specifying that the value must not be empty.
define a rule for the Post type .rule(_.tags.forall(_.nonEmpty), "...") that checks all the sequence values at once.
With 1) you will have precise error messages, specifying which element of collection was empty, while with 2) your message will not be so detailed.
Thank you for your quick reply. Would it be possible to extend the rule api in a way, that you can explicitly pass a validator for a selector? Something like
val tagValidator = Validator[String].rule(...)
implicit val postValidator = Validator[Post].rule(_.tags, tagValidator, "...")
IMHO this would make a nice feature for octopus. That way you can have detailed error messages without having to introduce value classes for primitive types.
I'm stuck trying to validate the following case class:
For instance I must ensure no element in
tags
contains an empty string. For case classes it's obvious to just bring a corresponding validator into scope. But how does this work for primitive types? I cannot just write aValidator[String]
because then this validator would be applied to allString
s, right? Or maybe I'm completely on the wrong track...The text was updated successfully, but these errors were encountered: