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

Workaround for returning a validation errors details back to browser? #1161

Open
dkulichkin opened this issue Sep 20, 2019 · 1 comment
Open

Comments

@dkulichkin
Copy link

Currently validation in Finch is based on predicate functions, causing further a NonValid error to be thrown without a chance to add details to it in order to help people to correct it...
https://finagle.github.io/finch/user-guide.html#validation

One might of course throw his own typed exceptions from these boolean preicates but that would be a misusing of the initially supposed way. Is there any more elegant solutions for approaching that? Thanks.

@dkulichkin dkulichkin changed the title Workaround for returning a validation error details back to browser? Workaround for returning a validation error's details back to browser? Sep 20, 2019
@dkulichkin dkulichkin changed the title Workaround for returning a validation error's details back to browser? Workaround for returning a validation errors details back to browser? Sep 20, 2019
@rpless
Copy link
Collaborator

rpless commented Sep 20, 2019

Hi @dkulichkin. One approach is to use Cats' Validated. I had opened this awhile back to see about getting it into Finch as operators on Endpoint. This would let you do something like this:

path[String].validate({ str: String => 
  if (str.isEmpty) Validated.invalid(Errors(NonEmptyList.of(Error.NotValid(item, "Empty Strings are not allowed"))))
  else Validated.valied(str)
})

Then returning the errors in the body of the response becomes a matter of formatting it the Exception Encoder. This approach is also nice because you can accumulate errors.

Even though we didn't move forward with that PR, its still possible to have this behavior with an implicit class that wraps Endpoint[A]. Although this does come at the cost of having to include another import when you want to use it.

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