question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write rule for validating collections?

See original GitHub issue
case class Customer(id: Int, name: String)

case class CustomerCollection(customers: Vector[Customer])

implicit val customerValidator: AsyncValidator[CustomerCollection] =
    AsyncValidator[CustomerCollection]
.async.rule(_.customers, ....)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
krzemincommented, Mar 23, 2018

That’s the point - you don’t need to write rules for collections manually.

import octopus.dsl._
import octopus.syntax._

case class Customer(id: Int, name: String)
case class CustomerCollection(customers: Vector[Customer])

implicit val cutsomerValidator: Validator[Customer] = Validator[Customer]
  .rule(_.name, (_: String).trim.nonEmpty, "name must not be empty")

val customers = CustomerCollection(Vector(Customer(1, "C1"), Customer(2, "   "), Customer(3, "")))
customers.validate
// invalid:
//  customers[1].name: name must not be empty
//  customers[2].name: name must not be empty

Octopus will derive rules for collection automatically!

However sometimes you may want to additionally restrict collection itself. In this case you can define your own rules and compose with derived ones:

implicit val customerCollectionValidator: Validator[CustomerCollection] = Validator[CustomerCollection]
  .derived
  .rule(_.customers, (_: Vector[Customer]).length % 2 == 0, "number of customers must be even")

customers.validate
// invalid:
//  customers[1].name: name must not be empty
//  customers[2].name: name must not be empty
//  customers: number of customers must be even

Intentionally this should work equally well for asynchronous validators.

0reactions
piximecommented, Sep 9, 2021

It seems that works better like this:

    implicit val customerCollectionValidator: Validator[CustomerCollection] = Validator[CustomerCollection]
      .rule(_.customers, (_: Vector[Customer]).length % 2 == 0, "number of customers must be even")
      .composeDerived
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a debt collection validation notice?
If you do dispute the debt in writing within 30 days the debt collector must stop collection until it provides you verification of...
Read more >
Collections — FluentValidation documentation
You can use the RuleForEach method to apply the same rule to multiple items in a collection: public class Person { public List<string>...
Read more >
Debt Validation Letters: How to Use Them to Crush ... - YouTube
Debt Validation Letters: How to Use Them to Crush Debt Collection (UPDATE: new rules in description) · Chapters. View all · Chapters ·...
Read more >
New rules for 2022 (get the FREE debt validation demand form)
NEW RULES for DEBT VALIDATION took effect November 30, 2021, and they include a NEW FORM LETTER to send to debt collectors to...
Read more >
7 Defining Validation and Business Rules Declaratively
One benefit of using declarative validation (versus writing your own validation) is that the validation framework takes care of the complexities of batching ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found