Skip to content

Instantly share code, notes, and snippets.

Collection<Throwable> exceptions =
facade.validator().validate(
Validation.Config.forJsonObject(objectHandler.getValue())
//By calling this method the validation will be performed on the entire document,
//otherwise the validation will stop at the first exception thrown
.withCompleteValidation()
);
facade.validator().registerCheck(
//Checking whether a value in any field marked as required
//(e.g.: @JsonProperty(value = "answer", required = true)) is null
Check.forAll().checkMandatory(),
//Checking whether a string value in any field is empty
Check.forAllStringValues().execute(pathValidationContext -> {
if (pathValidationContext.getValue() != null && pathValidationContext.getValue().trim().equals("")) {
pathValidationContext.rejectValue("IS_EMPTY", "is empty");
}
})
//Searching for the first occurrence by path suffix and convert it
Map<String, Object> sportAsMap = finderAndConverter.findFirstForPathEndsWith("sport");
ObjectHandler.Finder finderAndConverter = objectHandler.newValueFinderAndConverter();
//Searching for the first occurrence by path suffix
Sport sport = finder.findFirstForPathEndsWith("sport");
String option2OfSportQuestion = finder.findFirstForPathEndsWith(Path.of("sport", "q1", "options[1]"));
Question questionOne = finder.findForPathEquals(Path.of("quiz", "sport", "q1"));
ObjectHandler.Finder finder = objectHandler.newValueFinder();
//Searching for the first occurrence by path suffix
ObjectHandler sportOH = finder.findFirstForPathEndsWith("sport");
//Retrieving the path of the sport object ("quiz.sport")
String sportPath = sportOH.getPath();
//Retrieving the value of the sport object
Sport sport = sportOH.getValue();
ObjectHandler option2OfSportQuestionOH = finder.findFirstForPathEndsWith(Path.of("sport", "q1", "options[1]"));
ObjectHandler.Finder finder = objectHandler.newFinder();
Facade facade = Facade.create();
//Loading the JSON object
ObjectHandler objectHandler = facade.newObjectHandler(
ObjectHandlerTest.class.getClassLoader().getResourceAsStream("quiz.json"),
Root.class
);
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"