-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from sayedxali/feature/input-validation
Feature/input validation - Close #6
- Loading branch information
Showing
15 changed files
with
200 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/seyed/ali/timeentryservice/annotation/OptionalField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.seyed.ali.timeentryservice.annotation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotation to mark fields as optional for validation purposes. | ||
* Fields annotated with {@code @OptionalField} are considered optional, | ||
* allowing users to omit these fields in their input without triggering validation errors. | ||
* <p> | ||
* This annotation should be used in conjunction with a custom validator to enforce the optional behavior. | ||
* </p> | ||
* | ||
* @author [Seyed Ali] | ||
* @since 0.1 | ||
*/ | ||
@Target({ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = OptionalFieldValidator.class) | ||
public @interface OptionalField { | ||
|
||
/** | ||
* Returns the error message template. | ||
* | ||
* @return the error message template | ||
*/ | ||
String message() default "Field is optional"; | ||
|
||
/** | ||
* Returns the groups the constraint belongs to. | ||
* | ||
* @return the groups the constraint belongs to | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* Returns the payload associated with the constraint. | ||
* | ||
* @return the payload associated with the constraint | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} | ||
|
33 changes: 33 additions & 0 deletions
33
src/main/java/com/seyed/ali/timeentryservice/annotation/OptionalFieldValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.seyed.ali.timeentryservice.annotation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
/** | ||
* Custom validator for the {@link OptionalField} annotation. | ||
* <p> | ||
* This validator checks whether a field annotated with {@code @OptionalField} is optional or not. | ||
* The field is considered optional if its value is null or an empty string (""). | ||
* </p> | ||
* | ||
* @author [Seyed Ali] | ||
* @since 0.1 | ||
*/ | ||
public class OptionalFieldValidator implements ConstraintValidator<OptionalField, Object> { | ||
|
||
/** | ||
* Checks whether the given value is valid. | ||
* The value is considered valid if it is null or an empty string (""). | ||
* | ||
* @param value The value to validate | ||
* @param context The constraint validator context | ||
* @return {@code true}, indicating that the field is optional and considered valid. | ||
* Otherwise, it returns {@code false}, indicating that the field has a value and should be validated according to other constraints | ||
*/ | ||
@Override | ||
public boolean isValid(Object value, ConstraintValidatorContext context) { | ||
return value == null || (value instanceof String && ((String) value).isEmpty()); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 15 additions & 10 deletions
25
src/main/java/com/seyed/ali/timeentryservice/model/dto/TimeBillingDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package com.seyed.ali.timeentryservice.model.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import com.seyed.ali.timeentryservice.model.domain.TimeEntry; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.DecimalMax; | ||
import jakarta.validation.constraints.DecimalMin; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Data | ||
/** | ||
* DTO for {@link TimeEntry} | ||
*/ | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class TimeBillingDTO { | ||
|
||
private boolean billable; | ||
private BigDecimal hourlyRate; | ||
public record TimeBillingDTO( | ||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "A flag determining this time entry is billable", example = "true") | ||
boolean billable, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "The hourly rate in BigDecimal format", example = "10.0") | ||
@DecimalMin(value = "0.0", inclusive = false, message = "hourlyRate must be greater than 0") | ||
@DecimalMax(value = "999.99", message = "hourlyRate must be less than 1000") | ||
BigDecimal hourlyRate | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/seyed/ali/timeentryservice/model/dto/TimeEntryDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
package com.seyed.ali.timeentryservice.model.dto; | ||
|
||
import com.seyed.ali.timeentryservice.annotation.OptionalField; | ||
import com.seyed.ali.timeentryservice.model.domain.TimeEntry; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.*; | ||
|
||
/** | ||
* DTO for {@link TimeEntry} | ||
*/ | ||
@Schema(description = "Time Entry Data Transfer Object") | ||
public record TimeEntryDTO( | ||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "Unique identifier for the time entry", example = "12345") | ||
@Size(max = 36, message = "timeEntryId must be maximum 36 characters") | ||
String timeEntryId, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, description = "Start time of the time entry in the format yyyy-MM-dd HH:mm", example = "2024-05-12 08:00:00") | ||
@NotBlank(message = "startTime is mandatory") @Pattern(regexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", message = "startTime must be in the format yyyy-MM-dd HH:mm:ss") | ||
String startTime, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, description = "End time of the time entry in the format yyyy-MM-dd HH:mm", example = "2024-05-12 10:00:00") | ||
@NotBlank(message = "endTime is mandatory") | ||
@Pattern(regexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", message = "endTime must be in the format yyyy-MM-dd HH:mm:ss") | ||
String endTime, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "A flag determining this time entry is billable", example = "true") | ||
boolean billable, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "The hourly rate in BigDecimal format", example = "10.0") | ||
@DecimalMin(value = "0.0", inclusive = false, message = "hourlyRate must be greater than 0") | ||
@DecimalMax(value = "999.99", message = "hourlyRate must be less than 1000") | ||
@OptionalField | ||
String hourlyRate, | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.AUTO, description = "Duration of the time entry in the format HH:mm:ss", example = "02:00:00") | ||
@Pattern(regexp = "^\\d{2}:\\d{2}:\\d{2}$", message = "duration must be in the format HH:mm:ss") | ||
String duration | ||
) { | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/java/com/seyed/ali/timeentryservice/model/dto/TimeSegmentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 0 additions & 80 deletions
80
src/main/java/com/seyed/ali/timeentryservice/util/TimeEntryServiceUtility.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.