BAEL-6801, Applied review comments (#14567)

* BAEL-6801
Validate string length with Java validation annotations

* Bael-6801, applied Review comments

* Bael-6801, applied Review comments, deleting the springboot implementation
This commit is contained in:
parthiv39731
2023-08-15 22:47:51 +05:30
committed by GitHub
parent 06f473f5e6
commit 674b3d3453
13 changed files with 241 additions and 394 deletions
@@ -1,12 +0,0 @@
package com.baeldung.listvalidation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringJobApplicationDemoApp {
public static void main(String[] args) {
SpringApplication.run(SpringJobApplicationDemoApp.class, args);
}
}
@@ -1,49 +0,0 @@
package com.baeldung.listvalidation.controller;
import com.baeldung.listvalidation.domain.JobAspirant;
import com.baeldung.listvalidation.groups.AllLevels;
import com.baeldung.listvalidation.groups.Junior;
import com.baeldung.listvalidation.groups.MidSenior;
import com.baeldung.listvalidation.groups.Senior;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ApplyJobController {
@PostMapping("/applyLevelJunior")
public ResponseEntity<String> applyLevelJunior(@Validated({Junior.class, AllLevels.class}) @RequestBody JobAspirant user) {
return ResponseEntity.ok("Application submitted successfully");
}
@PostMapping("/applyLevelMidSenior")
public ResponseEntity<String> applyLevelMidSenior(@Validated({MidSenior.class, AllLevels.class}) @RequestBody JobAspirant user) {
return ResponseEntity.ok("Application submitted successfully");
}
@PostMapping("/applyLevelSenior")
public ResponseEntity<String> applyLevelSenior(@Validated({Senior.class, AllLevels.class}) @RequestBody JobAspirant user) {
return ResponseEntity.ok("Application submitted successfully");
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
}
@@ -1,107 +0,0 @@
package com.baeldung.listvalidation.domain;
import com.baeldung.listvalidation.groups.AllLevels;
import com.baeldung.listvalidation.groups.Junior;
import com.baeldung.listvalidation.groups.MidSenior;
import com.baeldung.listvalidation.groups.Senior;
import org.springframework.lang.Nullable;
import javax.validation.constraints.*;
import java.util.Date;
public class JobAspirant {
@Size.List({
@Size(min = 5, message = "Name should have at least 5 characters", groups = AllLevels.class),
@Size(max = 20, message = "Name should have at most 20 characters", groups = AllLevels.class)
})
@Pattern.List({
@Pattern(regexp = "^[\\p{Alpha} ]*$", message = "Name should contain only alphabets and space", groups = AllLevels.class),
@Pattern(regexp = "^[^\\s].*$", message = "Name should not start with space", groups = AllLevels.class),
@Pattern(regexp = "^.*[^\\s]$", message = "Name should not end with space", groups = AllLevels.class),
@Pattern(regexp = "^((?! ).)*$", message = "Name should not contain consecutive spaces", groups = AllLevels.class),
@Pattern(regexp = "^[^a-z].*$", message = "Name should not start with a lower case character", groups = AllLevels.class)
})
private String name;
public Integer getExperience() {
return experience;
}
public void setExperience(Integer experience) {
this.experience = experience;
}
@Min.List({
@Min(value = 15, message = "Years of experience cannot be less than 15 Years", groups = Senior.class),
@Min(value = 10, message = "Years of experience cannot be less than 10 Years", groups = MidSenior.class),
@Min(value = 5, message = "Years of experience cannot be less than 5 Years", groups = Junior.class)
})
@Max.List({
@Max(value = 20, message = "Years of experience cannot be more than 20 Years", groups = Senior.class),
@Max(value = 15, message = "Years of experience cannot be more than 15 Years", groups = MidSenior.class),
@Max(value = 10, message = "Years of experience cannot be more than 10 Years", groups = Junior.class)
})
private Integer experience;
@AssertTrue.List({
@AssertTrue(message = "Terms and Conditions consent missing for Senior Level Job Application", groups = Senior.class),
@AssertTrue(message = "Terms and Conditions consent missing for Mid-Senior Level Job Application", groups = MidSenior.class),
@AssertTrue(message = "Terms and Conditions consent missing for Junior Level Job Application", groups = Junior.class)
})
@Nullable
private Boolean agreement;
@Nullable
@Future.List({
@Future(message = "Active passport is mandatory for Senior Level Job Application", groups = Senior.class),
@Future(message = "Active passport is mandatory for Mid-Senior Level Job Application", groups = MidSenior.class),
@Future(message = "Active passport is mandatory for Junior Level Job Application", groups = Junior.class)
})
private Date passportExpiryDate;
@Pattern.List({
@Pattern(regexp = "^(Senior)$", message = "Job level should be Senior"
,flags = Pattern.Flag.CASE_INSENSITIVE, groups = Senior.class),
@Pattern(regexp = "^(MidSenior)$", message = "Job level should be MidSenior"
,flags = Pattern.Flag.CASE_INSENSITIVE, groups = MidSenior.class),
@Pattern(regexp = "^(Junior)$", message = "Job level should be Junior"
,flags = Pattern.Flag.CASE_INSENSITIVE, groups = Junior.class)
})
// @Pattern(regexp = "^(Senior|MidSenior|Junior)$", message = "Job level should be Senior, MidSenior or Junior"
// ,flags = Pattern.Flag.CASE_INSENSITIVE, groups = AllLevels.class)
private String jobLevel;
public String getJobLevel() {
return jobLevel;
}
public String getName() {
return name;
}
public Boolean getAgreement() {
return agreement;
}
public Date getPassportExpiryDate() {
return passportExpiryDate;
}
public void setAgreement(Boolean agreement) {
this.agreement = agreement;
}
public void setName(String name) {
this.name = name;
}
public void setJobLevel(String jobLevel) {
this.jobLevel = jobLevel;
}
public void setPassportExpiryDate(Date passportExpiryDate) {
this.passportExpiryDate = passportExpiryDate;
}
}
@@ -1,4 +0,0 @@
package com.baeldung.listvalidation.groups;
public interface AllLevels {
}
@@ -1,4 +0,0 @@
package com.baeldung.listvalidation.groups;
public interface Junior {
}
@@ -1,4 +0,0 @@
package com.baeldung.listvalidation.groups;
public interface MidSenior {
}
@@ -1,4 +0,0 @@
package com.baeldung.listvalidation.groups;
public interface Senior {
}