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

How to validate a collection of primitives? #29

Open
rfuerst87 opened this issue Oct 31, 2019 · 3 comments
Open

How to validate a collection of primitives? #29

rfuerst87 opened this issue Oct 31, 2019 · 3 comments

Comments

@rfuerst87
Copy link

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...

@krzemin
Copy link
Owner

krzemin commented Oct 31, 2019

Hi @rfuerst87.

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:

  1. 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.
  2. 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.

Hope it helps.

@rfuerst87
Copy link
Author

Hi @krzemin

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.

@krzemin
Copy link
Owner

krzemin commented Apr 11, 2020

Sure, sounds good 👍 PRs welcome.

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

No branches or pull requests

2 participants