Context Aware configuration with unique or not-null property | Community
Skip to main content
Kamal_Kishor
Community Advisor
Community Advisor
July 29, 2024
Solved

Context Aware configuration with unique or not-null property

  • July 29, 2024
  • 2 replies
  • 1189 views

Is it possible to specify a CA configuration as required or Not-Null or Unique.

I am exploring for a use-case where I don't want my config collection which has a key-value pair properties to be blank or duplicate. Is there is an OOTB specifier which I have missed?

 

Sample configs.

I want to avoid cases such as:-

  • "Key1": "Val1"
  • " ": "Val2"
  • "Key1": "Val3" 
@Configuration(label = "Sample CA Config", description = "Sample CA config", collection=true)
public @interface SampleConfig {
@Property(order = 1,
label = "Key,
description = "Key Name")
String key();

@Property(order = 2,
label = "Value",
description = "Value property")
String value();
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi @kamal_kishor 
You can write a custom validator.

https://wcm.io/caconfig/editor/validation.html 

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 29, 2024

Hi @kamal_kishor 
You can write a custom validator.

https://wcm.io/caconfig/editor/validation.html 

Arun Patidar
EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
July 29, 2024

Hi, 

You could use a default value after each property, something like this:

 

 

(label="Just some Property") String myProperty() default "someValue";

 

 

But it looks like you are more interested in validation, which is not possible using the out of the box with CA properties, you don't have such options (required, non-null, unique) for a property, check here: https://sling.apache.org/apidocs/sling9/org/apache/sling/caconfig/annotation/Property.html.

Another alternative is to keep the key/value pair in the same property, separated by a special character. While this approach can make the property harder to read, it allows you to avoid the dependency between prop1 and prop2. For example:

 

 

(label="Just some Property") String myKeyAndValue() default "key1:value1;key2:value2";

 

 

Although I understand your concern, please keep in mind that these configurations will be managed by a developer or someone with the expertise to set them correctly, not by an author. As long as you stay within this realm, you should be safe and not have to worry too much about it. Please check this thread for more information: https://cqdump.joerghoh.de/2018/04/23/content-and-configuration/ 

 

Hope this helps

Esteban Bustamante
Kamal_Kishor
Community Advisor
Community Advisor
July 29, 2024

yes, I was hoping to know if there was something as part of Configuration or Property itself. Thank you.