question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[proposal] Introduce `ArgumentNValidator` combinators

See original GitHub issue

The current Validator<T> requires an instance of type T to use the validate method. This must allow for the existence of T instance in an invalid state.

This Email example is a good example. It provides a factory method to prevent creation with invalid strings, but the constructor is exposed to allow users to instantiate it with invalid values.

However, modern general practice recommends that classes should not be instantiated with invalid values.

If the constructor of Email is designed to throw a runtime exception when an invalid value is passed, it is not possible to define a Validator<Email>.

So, how about making it possible to separate the type of the value received by the Validator from the type of the result?

For example, the following.

public interface EssentialValidator<A, R> {

  Validated<R> validate(A argument);

  default <R2> EssentialValidator<A, R2> map(Function<? super R, ? extends R2> f) {
      return a -> EssentialValidator.this.validate(a).map(f);
  }

  default <A2> EssentialValidator<A2, R> contramap(Function<? super A2, ? extends A> f) {
      return a2 -> EssentialValidator.this.validate(f.apply(a2));
  }

}
public final class ApplicativeValidator<T> implements EssentialValidator<T, T> {
   ...

With such an interface, it is possible to define EssentialValidator<String, Email> as follows, even if Email is designed to throw an exception in its constructor.

EssentialValidator<String, Email> validator = ValidatorBuilder.<String>of()
        .constraint(s -> s, "email", c -> c.notBlank().lessThanOrEqual(128).email())
        .build()
        .applicative()
        .map(Email::new);

This approach is to define validators of a small type, and then combine them to create a large-structure validator. However, YAVI is originally designed to create a large-structure validator by extracting parts of a large-structure object and writing constraint after constraint. Therefore, I am not sure if this proposal is really necessary as a use case of YAVI or not.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (28 by maintainers)

github_iconTop GitHub Comments

2reactions
makingcommented, Jun 2, 2021

Work in progress. Looks pretty cool.

image

1reaction
gakuzzzzcommented, Jun 3, 2021

I’m sure you’re right, I think split will be fine there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Combinator Pattern with Java 8 - Blog ·
We discussed primitives and combinators as building blocks. A validation use case illustrated how the pattern is applied to a specific domain.
Read more >
Scala Json Combinators - 2.1.1 - Play Framework
Reads[T] API now validates JSON by returning a monadic JsResult[T] being a JsSuccess[T] or a JsError aggregating all validation errors.
Read more >
Pattern matching using the is and switch expressions.
For more information, see the Pattern combinators section of the feature proposal note. Property pattern. You use a property pattern to match an ......
Read more >
How to Work Together : YC Startup Library | Y Combinator
There's something about the way we will have an argument that determines ... we can make a plan for figuring out how to...
Read more >
How Mathematicians Determine If an Argument Is a Valid Proof
validation of proof should be introduced in mathematics c. RESEARCH METHODS ... participants did include one mathematician who studies combinatorics and two.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found