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

Composable Constraints #63

Merged
merged 19 commits into from
Jul 7, 2023
Merged

Composable Constraints #63

merged 19 commits into from
Jul 7, 2023

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Jul 2, 2023

  • Now Generates Groups as a Set.of instead of list
  • add a constraint annotation for composing constraints (if an annotation has @Constraint, the generator will check the other annotation on the element for constraints)
  • will generate an annotation adapter class for composable constraints
  • rename processor
  • adds a new method to validation context to retrieve an annotation adapter( overriding the message and groups)

fixes #62

Given the annotation:

@Target({FIELD})
@Retention(SOURCE)
@Constraint
@Combining2(message = "sus")
@NotNull
public @interface Combining {

  String message();

  Class<?>[] groups() default {};
}

The following will be generated

@Generated
@AnnotationValidator(Combining.class)
public final class CombiningValidationAdapter implements ValidationAdapter<Object> {

  private final ValidationAdapter<Object> adapter;

  public CombiningValidationAdapter(ValidationContext ctx, Set<Class<?>> groups, Map<String, Object> attributes) {
    final var message = ctx.<Object>message(attributes).template();
    this.adapter =
        ctx.adapter(Combining2.class, groups, message, Map.of("message","sus", "groups",Set.of()))
            .andThen(ctx.adapter(NotNull.class, groups, message, Map.of("message","{avaje.NotNull.message}")));
  }

  @Override
  public boolean validate(Object value, ValidationRequest req, String propertyName) {

    return adapter.validate(value, req);
  }

}

the message and group annotations of the composed constraints are ignored

@rob-bygrave
Copy link
Contributor

Does Constraint have to be in validator module rather than in validator-constraints module?
This is still in draft (or completed now)?

@SentryMan SentryMan marked this pull request as ready for review July 2, 2023 21:47
@SentryMan SentryMan changed the title Composable Constraints (WIP) Composable Constraints Jul 2, 2023
@SentryMan
Copy link
Collaborator Author

I'm good with this now

@SentryMan SentryMan mentioned this pull request Jul 4, 2023
@rbygrave rbygrave merged commit 455c946 into avaje:main Jul 7, 2023
@SentryMan SentryMan deleted the composite branch July 7, 2023 14:03
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.

Composable Constraints
3 participants