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

Enable user defined Validations #449

Open
thanhbv opened this issue Dec 2, 2022 · 4 comments
Open

Enable user defined Validations #449

thanhbv opened this issue Dec 2, 2022 · 4 comments

Comments

@thanhbv
Copy link

thanhbv commented Dec 2, 2022

Use case

I want to use zio.schema.annotation.validate annotation to validate a field of a case class.

Problem

To do so, I have to provide an instance of Validation:

final case class Validation[A](bool: Bool[Predicate[A]])

ex, Validation(Bool.Leaf(myPredicate))
But zio-schema does not provide a way to create custom Predicate (and so custom Validation)

My question

Can zio-schema use zio.prelude.Validation instead of zio.prelude.Validation?
(zio-schema is already depended on zio-prelude)

Relates

If we permit user to define custom validation then issues like #251 will automatically be resolved.

@thanhbv
Copy link
Author

thanhbv commented Dec 2, 2022

Ah. I see. We cannot use zio.prelude.Validation because its a different concept than zio.schema.validation.Validation

But, can we remove the sealed modifier for trait Predicate[A] so users can define custom Validation?

@jdegoes
Copy link
Member

jdegoes commented Dec 7, 2022

Because we want to be able to serialize validations, a better approach is to define a new type of predicate which we can add to validation inside this library (rather than support user-defined validation).

What sort of validation did you want to add?

@jdegoes
Copy link
Member

jdegoes commented May 9, 2023

Another possibility is to allow conversion of ZIO Schema Validation into ZIO Prelude Validation (which is possible).

@williamareynolds
Copy link

williamareynolds commented Jun 6, 2023

@jdegoes

This issue is affecting us right now as well. Trying to set up a Password validation and we're having to remap all the errors to hide the password in messages.

a better approach is to define a new type of predicate which we can add to validation inside this library (rather than support user-defined validation).

Do you mean adding some Validation.fromPredicate[T](predicate: T => Bool): Chunk[ValidationError]? This is what I've typically seen in other libraries, and it's usually possible to add custom messaging to it as well.

I see that the goal is to serialize validations. In this case, I doubt it would be possible to create a custom validation...

Another possibility is to allow conversion of ZIO Schema Validation into ZIO Prelude Validation (which is possible).

I'd be happy to make this happen. We actually started validating things with zio-prelude's Validation type and found it to be quite handy.

Ideally for us, it becomes possible to both create our own instance of Validation, either by use of combinators or constructors, and it becomes possible to control the error messaging which is produced from a failing validator. I'd be happy to work on this if we can get a clear vision of what we all want this to come out looking like.

I can work on this if there's some outcome where failures are customized (which seems like it would be possible to serialize if the values are strings).

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

3 participants