Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a validation helper for future use.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/ca/bcit/infosys/comp4911/helper/ValidationHelper.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 ca.bcit.infosys.comp4911.helper; | ||
|
||
import org.json.JSONArray; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.ValidatorFactory; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Set; | ||
|
||
/** | ||
* Validation Helper | ||
*/ | ||
public class ValidationHelper { | ||
|
||
private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
|
||
public static Validator getValidator() { | ||
return factory.getValidator(); | ||
} | ||
|
||
public static boolean validateEntity(Object object){ | ||
Set<ConstraintViolation<Object>> constraintViolations = ValidationHelper.getValidator().validate(object); | ||
if (constraintViolations.size() > 0) { | ||
JSONArray jsonArray = new JSONArray(constraintViolations.toArray()); | ||
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(jsonArray.toString()).build()); | ||
} | ||
|
||
return true; | ||
} | ||
} |