Skip to content

Commit

Permalink
* validation: @pattern supports @pattern(ignoreCase = true)
Browse files Browse the repository at this point in the history
Signed-off-by: neo <[email protected]>
  • Loading branch information
neowu committed Sep 30, 2024
1 parent d97ff1a commit b16ec78
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### 9.1.3 (9/26/2024 - )

* validation: @Pattern supports @Pattern(ignoreCase = true)

### 9.1.2 (8/9/2024 - 9/26/2024)

* search: loading from json into search request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
public @interface Pattern {
String value();

boolean ignoreCase() default false;

String message() default "field must match /{pattern}/, value={value}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;

class KafkaMessages {
// key -> message, messages with same key will be processed in same thread
final Map<String, KafkaMessage> ordered = new HashMap<>();
final List<KafkaMessage> unordered = new ArrayList<>();
final String topic;
Expand All @@ -23,6 +24,7 @@ class KafkaMessages {
void addOrdered(ConsumerRecord<String, byte[]> record) {
var message = new KafkaMessage(record);
if (message.key != null) {
// only ensure message processing order by key, be aware of kafka ensures by partition, in practice, we only need key level ordering
KafkaMessage root = ordered.get(message.key);
if (root != null) root.addSubsequent(message);
else ordered.put(message.key, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Collection<KafkaMessages> poll() {
ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofSeconds(30));
if (records.isEmpty()) return null;

// topic -> messages, use linked hash map to keep message in same order as polled from kafka
Map<String, KafkaMessages> messageMappings = new LinkedHashMap<>();
for (ConsumerRecord<String, byte[]> record : records) {
String topic = record.topic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ private void buildStringValidation(CodeBuilder builder, Field field, String path
if (pattern != null) {
String patternFieldName = field.getName() + "Pattern" + (index++);
String patternVariable = variable(pattern.value());
this.builder.addField("private final java.util.regex.Pattern {} = java.util.regex.Pattern.compile({});", patternFieldName, patternVariable);
int flags = pattern.ignoreCase() ? java.util.regex.Pattern.CASE_INSENSITIVE : 0;
this.builder.addField("private final java.util.regex.Pattern {} = java.util.regex.Pattern.compile({}, {});", patternFieldName, patternVariable, flags);
builder.indent(2).append("if (!this.{}.matcher(bean.{}).matches()) errors.add({}, {}, java.util.Map.of(\"value\", bean.{}, \"pattern\", {}));\n",
patternFieldName, field.getName(), pathLiteral, variable(pattern.message()),
field.getName(), patternVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void createBeanValidator() {
void valid() {
var bean = new Bean();
bean.field1 = "abc-def";
bean.field4 = "Abc-Def";

var errors = new ValidationErrors();
validator.validate(bean, errors, false);
Expand Down Expand Up @@ -54,5 +55,8 @@ static class Bean {

@Pattern(value = "[a-z0-9]+", message = "field3 must be [a-z0-9]+")
public String field3;

@Pattern(value = "[a-z-]+", ignoreCase = true)
public String field4;
}
}

0 comments on commit b16ec78

Please sign in to comment.