Skip to content

Repair Custom Adapters/Class Constraints #108

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

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Aug 3, 2023

Fix broken adapter generation and introduces the concept of class constraints.

given a class:

@Valid
@PassingSkill
public record Tarnished(int vigor, int endurance) {}

the generator will look through the class level annotations and add to the adapter:

@Generated
public final class TarnishedValidationAdapter implements ValidationAdapter<Tarnished> {

  private final ValidationAdapter<Tarnished> tarnishedValidationAdapter;

  public TarnishedValidationAdapter(ValidationContext ctx) {
    this.tarnishedValidationAdapter = 
        ctx.<Tarnished>adapter(PassingSkill.class, Map.of("message","put these foolish ambitions to rest", "groups",Set.of()));
  }

  @Override
  public boolean validate(Tarnished value, ValidationRequest request, String field) {
    if (field != null) {
      request.pushPath(field);
    }

    tarnishedValidationAdapter.validate(value, request, field);

    if (field != null) {
      request.popPath();
    }

    return true;
  }
}

fixes #107

@sonatype-lift
Copy link

sonatype-lift bot commented Aug 3, 2023

Sonatype Lift is retiring

Sonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console.
We are extremely grateful and thank you for your support over the years.

📖 Read about the impacts and timeline

@rbygrave
Copy link
Contributor

rbygrave commented Aug 3, 2023

Yeah, looks good.

WRT 107, I wonder about cases where:

  • class level validation should only run if all the validation for that instance has already passed (do we need to expose a boolean flag to indicate this). e.g the field are invalid and therefore there is no point in running cross-field validation?
  • does that mean class level validation should ALWAYS run last?

but yeah, I think we merge this in. Not sure why the last build failed.

@rbygrave
Copy link
Contributor

rbygrave commented Aug 3, 2023

The AShip2ValidationAdapter generated code is missing a NotEmpty validator for some reason

@SentryMan
Copy link
Collaborator Author

figured that out, but ran into something else

@rbygrave rbygrave merged commit f3f6a80 into avaje:main Aug 3, 2023
@SentryMan SentryMan deleted the class-constraints branch August 3, 2023 04:04
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.

"Cross-field" validation support ?
2 participants