poslegm / brief Goto Github PK
View Code? Open in Web Editor NEWAutomated case classes validation using refinement types and macros
License: MIT License
Automated case classes validation using refinement types and macros
License: MIT License
meh...
"com.github.poslegm" %% "brief" % "version"
or "dev.chugunkov" %% "brief" % "version"
in build.sbt
@Validation case class(a: String Refined IPv4 Or IPv6) // <- doesn't compile
It fails here
It should works like:
@Validation
case class Test(a: Int, b: Int Refined Positive)
Test.create(1, -2) // Validated.Invalid(NonEmptyChain("For field Test.b: Predicate failed: (-2 > 0)."))
It can be done here
It should works like:
@Validation case class Test(a: Int Refined Positive)
Test.create(-2) // Left(List("bla bla"))
final case class CustomException(msgs: List[String] Refined NonEmpty)
extends Exception(s"validation errors on CustomTest creation: ${msgs.mkString("[", ",", "]")}")
with NoStackTrace
@Validation(CustomException(_)) case class CustomTest(a: Int Refined Positive)
Test.create(-2) // Left(CustomException("validation errors on CustomTest creation: [bla bla]"))
Ideally accept custom lambda into macro annotation:
@scala.annotation.compileTimeOnly("enable macro paradise to expand macro annotations")
class Validation[E](
mapErrors: List[String] Refined NonEmpty => E = (errors: List[String] Refined NonEmpty) => errors
) extends scala.annotation.StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro ValidationMacros.apply
}
It will be an entertaining adventure
Validated
is ok for proof of concept but it requires extra dependency on cats-core
. There is no need to use it in external API.
Either[List[String] Refined NonEmpty, C]
Validated
or other error accumulation technique insidecats-core
from deps@Validation case class Test[T](a: T) // <- doesn't compile
Type parameters with their constraints should be propagated to generated create
method parameters and argument types.
-deprecation
, etc
As many as possible
test("work with type alias") {
type PositiveInt = Int Refined Positive
@Validation case class Test(a: PositiveInt)
// compilation fails
assertEquals(Test.create(2), Validated.valid(Test(2)))
assertEquals(
Test.create(-2),
Validated.invalidNec[String, Test]("For field Test.ref: Predicate failed: (-2 > 0).")
)
}
create
argument has type PositiveInt
instead of int
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.