Code Monkey home page Code Monkey logo

brief's Introduction

brief

Currently Scala 2 only

Maven Central

The validations are useless if programmers ignore them. We need to reduce an amount of boilerplate to adopt a typesafe validations for mass usage.

breif is an automated constructor generator for the case classes with a refined fields. The purpose of this micro-library is to reduce an adoption cost of a refined types.

brief is just a wrapper around powerful Refined library by Frank S. Thomas.

It will help if:

  1. I want typesafe fields validation for case classes, but I don't want to write boilerplate to its validation manually;
  2. Something went wrong and I don't want to "fail fast" on the first invalid field. I want to get all validation errors together;
  3. I want to have a failed field name in an every error message.

Usage

sbt

libraryDependencies += "com.github.poslegm" %% "brief" % "0.0.1-RC1-M1"

// for Scala 2.13
scalacOptions += "-Ymacro-annotations"
// for Scala 2.12
libraryDependencies += compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)

Public API of brief consists of the only one macro annotation @Validation! It creates constructor for the case class with refined fields and accumulates all validation errors to List[String].

Example

import brief.annotations.Validation
import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric._

@Validation case class Test(a: Int, b: Int Refined Positive, c: Int Refined Negative)
// MACRO GENERATED CODE START
object Test {
  def create(a: Int, b: Int, c: Int): Either[List[String] Refined NonEmpty, Test] =
    brief.util.either.product(
      brief.util.either.liftErrors(refineV[Positive](b)),
      brief.util.either.liftErrors(refineV[Negative](c))
    ).map { case (b, c) =>
      new Test(a = a, b = b, c = c)
    }
}
// MACRO GENERATED CODE END

Test.create(1, 2, -3) // Right(Test(1, 2, -3))
Test.create(1, -2, 3) // Left(List("For field Test.b: Predicate failed: (-2 > 0).", "For field Test.c: Predicate failed: (3 < 0)."))

Custom predicate example

import brief.annotations.Validation
import eu.timepit.refined._
import eu.timepit.refined.api.{Refined, Validate}

val russianPhoneNumberRegex = "\\+7[0-9]{10}".r
final case class RussianPhoneNumber()
implicit def phoneNumber: Validate.Plain[String, RussianPhoneNumber] =
  Validate.fromPredicate(
    x => russianPhoneNumberRegex.matches(x),
    x => s"($x has format +7XXXXXXXXXX)",
    RussianPhoneNumber()
  )

@Validation case class Call(
  source: String Refined RussianPhoneNumber,
  target: String Refined RussianPhoneNumber
)

Call.create("+71234567890", "+71112223344") // Right(Call(...))

Custom errors

It's possible to return your own exception from a create method instead of raw List[String] with errors. Just define your error datatype with a contract:

  1. It should be a class or a case class
  2. It should receive List[String] or List[String] Refined NonEmpty to the constructor

And then pass it to the annotation type parameter as @Validation[CustomError].

Example

final case class CallValidationError(msgs: List[String])
    extends Exception(s"call validation errors: ${msgs.mkString("; ")}")
    with NoStackTrace

@Validation[CallValidationError] case class Call(
  source: String Refined RussianPhoneNumber,
  target: String Refined RussianPhoneNumber
)

Call.create("+71234567890", "+71112") // Left(CallValidationError(...))

Constraints

Generic case classes

case class with a type parameters will not compile. Feel free to fix it!

@Validation case class Test[T](a: T) // <- doesn't compile

Type aliases

There are constraints for type aliases. Feel free to fix it!

// predicate aliases are OK
type PaE = Positive And Even
@Validation case class Test(x: Int Refined PaE) // <- works good :)

// full type aliases aren't OK
type Pos = Int Refined Positive
@Validation case class Test(x: Pos) // <- will not work :(

Supported Scala versions

Currently supported versions are 2.12 and 2.13. Scala 3 support not ready yet.

brief's People

Contributors

poslegm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

brief's Issues

Add field name to error message

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

Support generic case classes

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

Write documentation in README

  1. Motivation
  2. More complicated examples
  3. Installation guide (maven link, sbt copy-paste line)
  4. Future goals
  5. Current limitations, supported Scala versions

Publish to Maven Central

  • Add sbt-ci-release
  • Choose package and organization name. I don't want to have "com.github.poslegm" %% "brief" % "version" or "dev.chugunkov" %% "brief" % "version" in build.sbt
  • Follow steps from plugin README

Custom error types

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
}

Get rid of cats.data.Validated

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.

  1. Change external API returning type to Either[List[String] Refined NonEmpty, C]
  2. Use self-made Validated or other error accumulation technique inside
  3. Remove cats-core from deps

Support type aliases

  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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.