Skip to content

Add @UUID validator #99

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

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions blackbox-test/src/test/java/example/avaje/uuid/AUuid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example.avaje.uuid;

import io.avaje.validation.constraints.UUID;
import jakarta.validation.Valid;

@Valid
public record AUuid(
@UUID
String str,
@UUID
CharSequence charSequence
) {
}
70 changes: 70 additions & 0 deletions blackbox-test/src/test/java/example/avaje/uuid/AUuidTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package example.avaje.uuid;

import io.avaje.validation.ConstraintViolation;
import io.avaje.validation.ConstraintViolationException;
import io.avaje.validation.Validator;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Locale;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

class AUuidTest {

final Validator validator = Validator.builder().addLocales(Locale.GERMAN).build();

@Test
void valid() {
var value = new AUuid(UUID.randomUUID().toString(), UUID.randomUUID().toString());
validator.validate(value);
}

@Test
void validNull() {
var value = new AUuid(null, null);
validator.validate(value);
}

@Test
void asString() {
var violation = one(new AUuid("Not", UUID.randomUUID().toString()));
assertThat(violation.message()).isEqualTo("must be a valid UUID");
}

@Test
void asStringDE() {
var violation = one(new AUuid("Not", UUID.randomUUID().toString()), Locale.GERMAN);
assertThat(violation.message()).isEqualTo("muss eine gültige UUID sein");
}

@Test
void asCharSequence() {
var violation = one(new AUuid(UUID.randomUUID().toString(), "Not"));
assertThat(violation.message()).isEqualTo("must be a valid UUID");
}

@Test
void asCharSequenceDE() {
var violation = one(new AUuid(UUID.randomUUID().toString(), "Not"), Locale.GERMAN);
assertThat(violation.message()).isEqualTo("muss eine gültige UUID sein");
}

ConstraintViolation one(Object any) {
return one(any, Locale.ENGLISH);
}

ConstraintViolation one(Object any, Locale locale) {
try {
validator.validate(any, locale);
fail("not expected");
return null;
} catch (ConstraintViolationException e) {
var violations = new ArrayList<>(e.violations());
assertThat(violations).hasSize(1);
return violations.get(0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.avaje.validation.constraints;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;

/**
* The annotated element must be a String validated to be a valid UUID.
*
* <p>Supported types are:
*
* <ul>
* <li>{@code String}
* <li>{@code CharSequence}
* </ul>
*/
@Constraint
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(UUID.List.class)
public @interface UUID {
String message() default "{avaje.UUID.message}";

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

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface List {
UUID[] value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface Handler {
"PositiveOrZero",
"Negative",
"NegativeOrZero",
"UUID"
};
for (final String key : keys) {
handlers.put("io.avaje.validation.constraints." + key, commonHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private BasicAdapters() {}
request ->
switch (request.annotationType().getSimpleName()) {
case "Email" -> new EmailAdapter(request);
case "UUID" -> new UuidAdapter(request);
case "Null" -> new NullableAdapter(request, true);
case "NotNull", "NonNull" -> new NullableAdapter(request, false);
case "AssertTrue" -> new AssertBooleanAdapter(request, Boolean.TRUE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.avaje.validation.core.adapters;

import io.avaje.validation.adapter.AbstractConstraintAdapter;
import io.avaje.validation.adapter.ValidationContext.AdapterCreateRequest;

import java.util.UUID;

final class UuidAdapter extends AbstractConstraintAdapter {

UuidAdapter(AdapterCreateRequest request) {
super(request);
}

@Override
protected boolean isValid(Object value) {
if (value == null) {
return true;
}
try {
UUID.fromString(String.valueOf(value));
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}