strategy to fire polymorphic rules
See original GitHub issueI’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:
- Created 3 years ago
- Comments:13 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Found it,
data
used aHashMap
in the API, that it fails on that type. It I change it to aMap
, works fine.@krzemin Yes, the second approach is fine. It does find the concrete rules. The following test now passes.
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.