Skip to content

Commit

Permalink
♻️ refactor: refactor codebase #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 3, 2024
1 parent 04e8820 commit dd1311a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.clarify4j.common.annotation;

import org.clarify4j.config.validator.EmailValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = EmailValidator.class)
@Documented
public @interface IsEmail {
String message() default "Invalid email";

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

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

boolean disabled() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.clarify4j.config.validator;

import org.clarify4j.common.annotation.IsEmail;
import org.unify4j.common.Regex4j;
import org.unify4j.common.String4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class EmailValidator implements ConstraintValidator<IsEmail, String> {
protected boolean disabled;

@Override
public void initialize(IsEmail url) {
this.disabled = url.disabled();
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (this.disabled) {
return true;
}
if (String4j.isEmpty(value)) {
return false;
}
return Regex4j.isEmail(value);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.clarify4j.common.annotation.IsIP;
import org.unify4j.common.Regex4j;
import org.unify4j.common.String4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
Expand All @@ -19,6 +20,9 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
if (this.disabled) {
return true;
}
if (String4j.isEmpty(value)) {
return false;
}
return Regex4j.isIp(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
if (this.disabled) {
return true;
}
if (String4j.isEmpty(value)) {
return false;
}
return String4j.isNumeric(value) && Regex4j.isNumeric(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.clarify4j.common.annotation.IsTime;
import org.unify4j.common.Regex4j;
import org.unify4j.common.String4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
Expand All @@ -19,6 +20,9 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
if (this.disabled) {
return true;
}
if (String4j.isEmpty(value)) {
return false;
}
return Regex4j.isTime24HFully(value) && Regex4j.isTime12HFully(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.clarify4j.common.annotation.IsUrl;
import org.unify4j.common.Regex4j;
import org.unify4j.common.String4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
Expand All @@ -15,10 +16,13 @@ public void initialize(IsUrl url) {
}

@Override
public boolean isValid(String url, ConstraintValidatorContext context) {
public boolean isValid(String value, ConstraintValidatorContext context) {
if (this.disabled) {
return true;
}
return Regex4j.isURL(url);
if (String4j.isEmpty(value)) {
return false;
}
return Regex4j.isURL(value);
}
}

0 comments on commit dd1311a

Please sign in to comment.