Tùng Trần
Tùng Trần

Reputation: 11

I want to create an annotation that inherits from the @Size annotation, but it seems that it's not working

I created an annotation named OptimizedName to validate the user name and expected it to inherit from @Size, while still allowing the override of the min and max attributes. However, it's not working. Did I use @AliasFor correctly? I am using spring framework

I create @OptimizeName like this:

package com.foodey.server.validation.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@Documented
@NotBlank
@Size
public @interface OptimizedName {

  // @AliasFor(annotation = Size.class, attribute = "message")
  // String message() default "Name must be between {min} and {max} characters";

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

  // Class<? extends Payload>[] payload() default {};

  @AliasFor(annotation = Size.class, attribute = "min")
  int minLength() default 3;

  @AliasFor(annotation = Size.class, attribute = "max")
  int maxLength() default 50;
}

I use it in RegistrationRequest object like this:

package com.foodey.server.auth.dto;

import com.foodey.server.validation.annotation.OptimizedName;
import com.foodey.server.validation.annotation.Password;
import com.foodey.server.validation.annotation.PhoneNumber;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class RegistrationRequest {

  @PhoneNumber private String phoneNumber;

  @Password private String password;

  @OptimizedName(minLength = 5, maxLength = 50)
  private String name;
}

But when i run, it's not working. But if i create @OptimizedName like this, It's work, but this is hardcode and i don't want it. I don't want to create a validation class for this.

package com.foodey.server.validation.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@Documented
@NotBlank
@Size(min = 3, max = 50)
public @interface OptimizedName {

  // @AliasFor(annotation = Size.class, attribute = "message")
  // String message() default "Name must be between {min} and {max} characters";

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

  // Class<? extends Payload>[] payload() default {};

  // @AliasFor(annotation = Size.class, attribute = "min")
  // int minLength() default 3;

  // @AliasFor(annotation = Size.class, attribute = "max")
  // int maxLength() default 50;
}

Upvotes: 1

Views: 93

Answers (1)

Rachkovan Oleg
Rachkovan Oleg

Reputation: 106

I suppose the problem is in mixing annotation processors (spring and jee). @AliasFor annotation comes from spring and likely cannot interact with jee @Size. However as I am informed jee has its own equivalent.

@OverridesAttribute(constraint = Size.class, name = "min")

Upvotes: 1

Related Questions