Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error message for Assertion #1218

Open
SvenW opened this issue Oct 23, 2023 · 2 comments
Open

Custom error message for Assertion #1218

SvenW opened this issue Oct 23, 2023 · 2 comments

Comments

@SvenW
Copy link

SvenW commented Oct 23, 2023

When adding a new type e.g:

  object Name extends Subtype[String] {
    override def assertion          = assert {
      hasLength(greaterThanOrEqualTo(1))
    }

It would be nice to be able to set a custom error message that's a bit more user friendly if the assertion fails. Right now this assertion would give us did not satisfy hasLength(greaterThanOrEqualTo(1)), which isn't that user friendly if returned to an actual user. It's possible to remap the error if we use make, e.g Name.make("my name") but that's a bit to easy to forget.
Would it make sense to be able to set a custom error message on an Assertion?

@guizmaii
Copy link
Member

guizmaii commented Oct 23, 2023

Alternatively, you can use assertCustom

  object Name extends Subtype[String] {
    override def assertion = assertCustom { str =>
      if (str.trim.size >= 1) Right(()) else Left(AssertionError.Failure("This is an error"))
    }
  }

See https://github.com/zio/zio-prelude/blob/series/2.x/core-tests/shared/src/test/scala-2.12-2.13/zio/prelude/NewtypeSpecTypes212.scala#L7-L9

Note that this is in Scala2.
In Scala3, the syntax is a bit different: https://github.com/zio/zio-prelude/blob/series/2.x/core-tests/shared/src/test/scala-3/zio/prelude/NewtypeSpecTypes.scala#L90-L96

@SvenW
Copy link
Author

SvenW commented Oct 23, 2023

Alternatively, you can use assertCustom

  object Name extends Subtype[String] {
    override def assertion = assertCustom { str =>
      if (str.trim.size >= 1) Right(()) else Left(AssertionError.Failure("This is an error"))
    }
  }

See https://github.com/zio/zio-prelude/blob/series/2.x/core-tests/shared/src/test/scala-2.12-2.13/zio/prelude/NewtypeSpecTypes212.scala#L7-L9

Note that this is in Scala2. In Scala3, the syntax is a bit different: https://github.com/zio/zio-prelude/blob/series/2.x/core-tests/shared/src/test/scala-3/zio/prelude/NewtypeSpecTypes.scala#L90-L96

Thanks. I settled on this for now

  object Name extends Subtype[String] {
    override def assertion          = assertCustom { str =>
      hasLength(greaterThanOrEqualTo(1))(str).fold(_ => Left(AssertionError.Failure("can't be empty")), _ => Right(()))
    }
```. Would however still be nice to be able to just define an error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants