Skip to content
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

Custom Validations #15

Merged
merged 3 commits into from
Dec 8, 2021
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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<javax-validation.version>2.0.1.Final</javax-validation.version>
<modelmapper.version>2.3.4</modelmapper.version>
<swagger.version>3.0.0</swagger.version>
<commons.validator.version>1.7</commons.validator.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -68,6 +69,18 @@
<version>${javax-validation.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>${commons-validator.version}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.graduationproject.studentinformationsystem.common.model.dto.request;

import com.graduationproject.studentinformationsystem.common.util.validation.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
public abstract class SisBasePersonalInfoRequest {

@TCNO
@NotNull
protected Long tcNo;

@NotNull
@Name
protected String name;

@NotNull
@Surname
protected String surname;

@Email
@NotNull
protected String email;

@NotNull
@PhoneNumber
protected Long phoneNumber;

// protected Byte[] profilePhoto; // TODO: Added Profile Photo

// @URL
// protected String profilePhotoUrl; // TODO: Added Profile Photo URL

@NotNull
@DateTimeFormat
protected Date birthday;

@Address
@NotNull
protected String address;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = AddressValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Address {
Class<?>[] groups() default {};

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

String message() default " length must be between minLength and maxLength";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class AddressValidation implements ConstraintValidator<Address, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int maxLength = 256;
int minLength = 1;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be between " + minLength + " and " + maxLength).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(maxLength, value.toString().length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = OnlyNumberValidation.class)
@Constraint(validatedBy = DepartmentIDValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface OnlyNumber {
public @interface DepartmentID {
Class<?>[] groups() default {};

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

String message() default "Must Be Only Number";
String message() default "length must be ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class DepartmentIDValidation implements ConstraintValidator<DepartmentID, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int length = 5;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be " + length).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(length, value.toString().length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = NameValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
Class<?>[] groups() default {};

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

String message() default " length must be between minLength and maxLength";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class NameValidation implements ConstraintValidator<Name, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int maxLength = 100;
int minLength = 3;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be between " + minLength + " and " + maxLength).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(maxLength, value.toString().length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = NumberValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Number {
Class<?>[] groups() default {};

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

int length() default 0;

String message() default "length must be ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class NumberValidation implements ConstraintValidator<Number, Object> {

private int length;

public void initialize(Number constraintAnnotation) {
this.length = constraintAnnotation.length();
}

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be " + length).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(length, value.toString().length());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = PhoneNumberValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface PhoneNumber {
Class<?>[] groups() default {};

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

String message() default "length must be ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class PhoneNumberValidation implements ConstraintValidator<PhoneNumber, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int length = 10;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be " + length).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(length, value.toString().length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = SurnameValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Surname {
Class<?>[] groups() default {};

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

String message() default " length must be between minLength and maxLength";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class SurnameValidation implements ConstraintValidator<Surname, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int maxLength = 100;
int minLength = 3;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be between " + minLength + " and " + maxLength).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(maxLength, value.toString().length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = TCNOValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TCNO {
Class<?>[] groups() default {};

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

String message() default "length must be ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.graduationproject.studentinformationsystem.common.util.validation;

import org.apache.commons.lang3.math.NumberUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class TCNOValidation implements ConstraintValidator<TCNO, Object> {

public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null) return true;
int length = 11;

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("length must be " + length).addConstraintViolation();

return NumberUtils.isCreatable(value.toString()) && Objects.equals(length, value.toString().length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = OnlyNumberValidation.class)
@Constraint(validatedBy = URLValidation.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface URL {
Expand Down
Loading