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

Introduce Applicative Validation #121

Merged
merged 23 commits into from
May 14, 2021
Merged

Introduce Applicative Validation #121

merged 23 commits into from
May 14, 2021

Conversation

making
Copy link
Owner

@making making commented May 12, 2021

This pr introduces an implementation similar to Vavr's Validation or Scalaz's Validation control class that is an applicative functor and facilitates accumulating errors.

ApplicativeValidator which can be obtained by Validator.applicative() has validate method and it returns Validation object as follows:

Validator<Email> emailValidator = ValidatorBuilder.of(Email.class)
                                                    .constraint(...)
                                                    .build()
                                                    .prefixed("email");
Validator<PhoneNumber> phoneNumberValidator = ValidatorBuilder.of(PhoneNumber.class)
                                                    .constraint(...)
                                                    .build()
                                                    .prefixed("phoneNumber");

Validation<ConstraintViolation, ContactInfo> validation = emailValidator.applicative().validate(email)
                                                                            .compose(phoneNumberValidator.applicative().validate(phoneNumber))
                                                                            .apply(ContactInfo:new);

// or

Validation<ConstraintViolation, ContactInfo> validation = Validations.compose(emailValidator.applicative().validate(email), phoneNumberValidator.applicative().validate(phoneNumber)))
                                                                            .apply(ContactInfo:new);


HttpStatus status = validation.fold(v -> HttpStatus.BAD_REQUEST, c -> HttpStatus.OK);

This API makes it possible to make different validation results composable.
It's more powerful than using ValidatorBuilder.nest() method for reusing a value object class.

closes gh-119

@making
Copy link
Owner Author

making commented May 13, 2021

If it sounds natural, I'd like to change as follows:

  • applicative() -> composable()
  • ApplicativeValidator -> ComposableValidator
Validation<ConstraintViolation, ContactInfo> validation = Validations.compose(emailValidator.composable().validate(email), phoneNumberValidator.composable().validate(phoneNumber)))
                                                                            .apply(ContactInfo:new);

@making
Copy link
Owner Author

making commented May 13, 2021

review comments in Japanese from @gakuzzzz
https://gist.github.com/gakuzzzz/6acd31a9c7d1756ae6c2745442720a79

@making
Copy link
Owner Author

making commented May 13, 2021

If it sounds natural, I'd like to change as follows:

  • applicative() -> composable()
  • ApplicativeValidator -> ComposableValidator
Validation<ConstraintViolations, ContactInfo> validation = Validations.compose(emailValidator.composable().validate(email), phoneNumberValidator.composable().validate(phoneNumber)))
                                                                            .apply(ContactInfo:new)
                                                                            .mapError(ConstraintViolations::concat);;

From @gakuzzzz 's feed back in https://gist.github.com/gakuzzzz/6acd31a9c7d1756ae6c2745442720a79#gistcomment-3741084 , I'm going to go with applicative at this moment.

@making making merged commit fdb05fb into develop May 14, 2021
@making making deleted the validation branch May 14, 2021 15:13
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

Successfully merging this pull request may close these issues.

Support Validation using Applicative Functor
1 participant