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.

strategy to fire polymorphic rules

See original GitHub issue

I’m evaluating Octopus and am unable to model a couple of use cases, so I’m wondering if I’m just “doing it wrong”.

  • An ability to define a rule on an trait of abstract type and derive the rules across the type hierarchy. I don’t think is supported, or at least the shapes example doesn’t exercise it.
  • Map field names to errors. I think I’m just missing something, as the documentation explicitly suggests that this case should work by default.

All four of the following tests fail:

class OctopusValidationTest extends BaseUnitTest {

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

  trait HasName {
    val name: String
  }
  sealed trait Pet extends HasName
  object Pet {
    implicit val nameIsRequiredAndCantStartWithZ: Validator[Pet] = Validator[Pet]
      .rule(_.name.nonEmpty, "is required")
      .rule(_.name.startsWith("Z"), "can't start with Z")
  }

  case class Dog(name: String) extends Pet
  object Dog {
    implicit val allDogsMustBeNamedRooney: Validator[Dog] = Validator[Dog]
      .rule(_.name == "Rooney", "is not Rooney")
  }

  case class Cat(name: String) extends Pet
  object Cat {
    implicit val allCatNamesLessThanFive: Validator[Cat] = Validator[Cat]
      .rule(_.name.length < 5, "is too long")
  }

  def validate[T <: Pet: Validator](pet: T) = pet.validate.toFieldErrMapping

  // this test fails because the error is ("","is not Rooney")
  test("dogs name is not rooney") {
    val notRooney = Dog("Bart")
    val errors = validate(notRooney)
    errors should have size 1
    errors should contain theSameElementsAs List(
      ("name", "is not Rooney")
    )
  }

  // this test fails because the error is ("","is too long")
  test("cats name is too long") {
    val rumpelstiltskin = Cat("Rumpelstiltskin")
    val errors = validate(rumpelstiltskin)
    errors should have size 1
    errors should contain theSameElementsAs List(
      ("name", "is too long")
    )
  }

  // this only tests concrete rule, how do we trigger the pet rule?
  test("pets name isEmpty and not Rooney") {
    val emptyDog = Dog("")
    validate(emptyDog) should have size 2
  }

  // this only tests concrete rule, how do we trigger the pet rule?
  test("cats name is too long and starts with Z") {
    val zumpelstiltskin = Cat("Zumpelstiltskin")
    validate(zumpelstiltskin) should have size 2
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andyczerwonkacommented, Nov 2, 2020

Found it, data used a HashMap in the API, that it fails on that type. It I change it to a Map, works fine.

1reaction
andyczerwonkacommented, Nov 1, 2020

@krzemin Yes, the second approach is fine. It does find the concrete rules. The following test now passes.

  test("a family with a few pets") {
    val failingPets = List(Cat("Zumpelstiltskin"), Dog(""))
    val family = Family("", Dog("Rooney"), failingPets)
    val errors = validate(family)
    errors should have size 5
    errors should contain theSameElementsAs List(
      ("otherPets[0].name", "can't start with Z"),
      ("otherPets[0].name", "is too long"),
      ("otherPets[1].name", "is required"),
      ("otherPets[1].name", "is not Rooney"),
      ("name", "is required")
    )
  }

It would be nice to be able to construct the message using the object for each rule. I think I saw a feature request around that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Practical Guide to Polymorph - DnD 5e - RPGBOT
Character optimization guide to DnD 5e's Polymorph spell. ... Giant Fire Beetle MM : The attack is a bit higher than you want...
Read more >
Polymorphic Rules & Regulations - Wowpedia - Fandom
Rule #5: Do not polymorph anything that is halfway through a portal - This has yet to not cause an explosion. This is...
Read more >
Polymorphism vs Strategy pattern - java - Stack Overflow
The purpose of the Strategy pattern is to promote the use of composition (has-a) over inheritance (is-a). Instead of your class inheriting ...
Read more >
Polymorphic Universes — Coq 8.16.1 documentation
In case the constant is universe polymorphic, we allow this rule to fire only when unifying the universes results in instantiating a so-called...
Read more >
ROY Reclaims Its Crown: New Ways To Increase Polymorphic ...
The potential of the strategy has been demonstrated by using it to produce new polymorphs of the benchmark compound ROY as single crystals ......
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