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

Increase reusability of Validator [SPR-15314] #19877

Closed
spring-projects-issues opened this issue Mar 5, 2017 · 2 comments
Closed

Increase reusability of Validator [SPR-15314] #19877

spring-projects-issues opened this issue Mar 5, 2017 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Daniel Gulyas opened SPR-15314 and commented

Quick summary

Hi!

It would be really cool if the Validator implementations could be reused at the Service level hassle-free.

It would be cool because:

  • It's a common scenario that there's a second request source using the same dto (think webmvc / rest and mq), adding the second source involves extra work if it doesn't provide the binding in an other way, without using implementations of the Errors interface
  • Increases code reuse, since we use the same Validator on both Service-level and Controller-level
  • Would promote validation at the service-level a bit

Example scenario when that happens:

  1. There's a web-mvc project, controller <-dto-> service, at the controller-side the dto is validated with the Validator
  2. There's a business need to implement a secondary source of requests (let's say an mq)

Now there's a possibility to reuse the Validator created for the controller at the service level, but for me it seems to involves quite a hassle, it would be nice to make that convenient.

Proposal

  • Create an interface which implements the simplified Errors interface
    • Can be instantiated with the target object only
    • Doesn't necessary have to store all the rejected properties, tracking if there was any errors might be enough
  • Create an abstract class which implements Validator
    • Boolean validate(Object target) is implemented here

The namings for me seem to be a bit weak, they could handle suggestions I'm pretty sure.

For existing Validator implementations there's only need to extend this abstract class and it'll turn into a validator which is convenient to use at the service level, please see the attached java files, this might be a bit invasive although.

Follow-up

This is just an idea and i'm kinda new to Spring and to Java in general so the proposal will probably include foolish design mistakes, i looked it up and so far i haven't seen anything which would make reusing of these easy, please point it out if there's already some.

If that's cool to do and there's a consensus i could make a pr for this in the coming days.

Let me know what you think!


Attachments:

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
@jhoeller jhoeller modified the milestones: 5.x Backlog, General Backlog Aug 24, 2020
@jhoeller jhoeller modified the milestones: General Backlog, 6.1.x Jul 13, 2023
@jhoeller jhoeller self-assigned this Jul 18, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M4 Jul 18, 2023
@jhoeller
Copy link
Contributor

We are considering a default method on the Validator interface for such a purpose in 6.1, for simplified assertion-style invocations. We are not sure whether to reduce this to a boolean return value or rather throw an IllegalArgumentException with a rich message, but either way, we're going to make it easier to invoke a Validator without an Errors instance having to be manually built/provided.

@jhoeller
Copy link
Contributor

jhoeller commented Jul 19, 2023

After a bit of back and forth, I'm introducing a Validator.validateObject(Object) method with an Errors return value now which can easily be used for a Validator.validateObject(Object).hasErrors() check but also with a new failOnError method on the Errors interface: Validator.validateObject(Object).failOnError(IllegalStateException::new)). Underneath the covers, this is using a new SimpleErrors implementation which tracks registered errors in a lean fashion and flexibly accesses field values via getter methods or raw field access (while not providing any binding support).

@sbrannen sbrannen changed the title Increase reusability of Validator [SPR-15314] Increase reusability of Validator [SPR-15314] Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants