use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method batchUpdate.
@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates, @ValidatorParam RestLiDataValidator validator) {
Map<Integer, UpdateResponse> results = new HashMap<Integer, UpdateResponse>();
Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
Integer key = entry.getKey();
PatchRequest<ValidationDemo> patch = entry.getValue();
ValidationResult result = validator.validateInput(patch);
if (result.isValid()) {
results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString()));
}
}
return new BatchUpdateResult<Integer, ValidationDemo>(results, errors);
}
use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method get.
@RestMethod.Get
public ValidationDemo get(final Integer key, @ValidatorParam RestLiDataValidator validator) {
// Generate an entity that does not conform to the data schema
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.BARBAR);
ValidationDemo validationDemo = new ValidationDemo().setStringA("stringA is readOnly").setUnionFieldWithInlineRecord(union);
// Validate the entity
ValidationResult result = validator.validateOutput(validationDemo);
check(!result.isValid());
String errorMessages = result.getMessages().toString();
check(errorMessages.contains("/stringA :: length of \"stringA is readOnly\" is out of range 1...10"));
check(errorMessages.contains("/stringB :: field is required but not found"));
// Fix the entity
validationDemo.setStringA("abcd").setStringB("stringB");
// Validate the entity again
result = validator.validateOutput(validationDemo);
check(result.isValid());
return validationDemo;
}
Aggregations