AuthMe / ConfigMe

A simple configuration management library for any Java project!
MIT License
37 stars 16 forks source link

[Feature] Property validation #354

Open gamerover98 opened 1 year ago

gamerover98 commented 1 year ago

Description

Some Java frameworks, such as Spring Boot, provide comprehensive Java Bean Validation support for checking the values of DTOs, Entities, and more.

public class PersonForm {
  @NotNull
  @Size(max=64)
  private String name;

  @Min(0)
  private int age;
}

The current version of ConfigMe lacks these capabilities and makes the data checking annoying.


Scenario

  1. A SettingHolder class with different properties such as:
    
    @Size(max=32)
    @Comment("The text of the title")
    public static final Property<String> TITLE_TEXT =
    newProperty("title.text", "-Default-");

@Size(min=1, max=10) @Comment("The size that the title will have") public static final Property TITLE_SIZE = newProperty("title.size", 7);


2. A bean class:
```JAVA
public class EntityBeanProperty implements ParameterizedType {

  private static final Type[] TYPE_ARGUMENTS = {
    Boolean.TYPE, // enabled or disabled
    Integer.TYPE, // number of entities
    ...
  };

  private boolean enabled;

  @Size(min=0, max=16)
  private int entities;

  // constructors...
  // getters/setters...
  // other stuff...

  @Override
  public Type[] getActualTypeArguments() {
      return TYPE_ARGUMENTS;
  }

  @Override
  public Type getRawType() {
      return EntityBeanProperty.class;
  }

  @Override
  public Type getOwnerType() {
      return null;
  }
}

Please note that this issue is a draft and may undergo changes.


Looking forward to your feedback and collaboration on this enhancement. Thank you 💯

ljacqu commented 1 year ago

Many years ago, I had the same idea in #14 and ultimately felt it was too broad for the benefits:

This is not a definitive no from my side but my current feeling. I'm currently rewriting the way property types are structured so this is anyway blocked for a while, giving us time to consider all aspects :smile:

(Btw, you probably don't want to implement ParameterizedType in that example, but just have a bean class and then use the BeanProperty constructor that takes a Class argument. ParameterizedType is a generic type declaration for something like List<String>, where the base type would be List.class and the type arguments in this example would be {String.class})