Skip to content

Commit

Permalink
Use code points for string length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Oct 19, 2024
1 parent cb32d92 commit 209150f
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/CommentCreation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record CommentCreation(
@Length(min = 1, max = Comment.TEXT_MAX_LENGTH)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/EmailActivation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record EmailActivation(
@NotBlank
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/EmailCreation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record EmailCreation(
@NotBlank
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/NewTokenCreation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record NewTokenCreation(
@NotBlank @Length(max = Email.EMAIL_MAX_LENGTH) @Schema(maxLength = Email.EMAIL_MAX_LENGTH)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/TokenCreation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record TokenCreation(
@NotBlank @Length(max = Email.EMAIL_MAX_LENGTH) @Schema(maxLength = Email.EMAIL_MAX_LENGTH) String identifier,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/fyreplace/api/data/UserCreation.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package app.fyreplace.api.data;

import app.fyreplace.api.data.validators.Length;
import app.fyreplace.api.data.validators.Regex;
import jakarta.validation.constraints.NotBlank;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

public record UserCreation(
@NotBlank
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/app/fyreplace/api/data/validators/Length.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.fyreplace.api.data.validators;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@SuppressWarnings("unused")
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
@Constraint(validatedBy = LengthValidator.class)
public @interface Length {
String message() default "does not meet length requirements";

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

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

int min() default 0;

int max() default Integer.MAX_VALUE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app.fyreplace.api.data.validators;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public final class LengthValidator implements ConstraintValidator<Length, String> {
private int min;
private int max;

@Override
public void initialize(final Length constraintAnnotation) {
min = constraintAnnotation.min();
max = constraintAnnotation.max();
}

@Override
public boolean isValid(final String value, final ConstraintValidatorContext context) {
final var count = value.codePointCount(0, value.length());
return count >= min && count <= max;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import app.fyreplace.api.data.Post;
import app.fyreplace.api.data.StoredFile;
import app.fyreplace.api.data.User;
import app.fyreplace.api.data.validators.Length;
import app.fyreplace.api.exceptions.ExplainedFailure;
import app.fyreplace.api.exceptions.ForbiddenException;
import app.fyreplace.api.services.ImageService;
Expand Down Expand Up @@ -37,7 +38,6 @@
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.hibernate.validator.constraints.Length;

@Path("posts/{id}/chapters")
public final class ChaptersEndpoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import app.fyreplace.api.data.StoredFile;
import app.fyreplace.api.data.User;
import app.fyreplace.api.data.UserCreation;
import app.fyreplace.api.data.validators.Length;
import app.fyreplace.api.emails.UserActivationEmail;
import app.fyreplace.api.exceptions.ConflictException;
import app.fyreplace.api.exceptions.ExplainedFailure;
Expand Down Expand Up @@ -49,7 +50,6 @@
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.hibernate.validator.constraints.Length;

@Path("users")
public final class UsersEndpoint {
Expand Down

0 comments on commit 209150f

Please sign in to comment.