A Validator, allowing to declare the permitted operations, for a json-patch.
Library Version | Java Version | Json-patch Dependencies |
---|---|---|
1.0.0 and later | 17+ | com.gravity9:json-patch-path:2.0.2 |
This library use Jakarta Validation, it doesn't support older javax.validation.
- com.gravity9:json-patch-path : for Json Patch
- jakarta.validation:jakarta.validation-api : as validation api
- org.junit.jupiter:junit-jupiter-api : test framework
- org.hibernate.validator:hibernate-validator : validation-api hibernate implementation for test
<dependency>
<groupId>fr.maif.json.validation</groupId>
<artifactId>json-patch-validator</artifactId>
<version>1.0.0</version>
</dependency>
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.github.fge.jsonpatch.JsonPatchOperation;
import fr.maif.json.patch.JsonPatchOperationWrapper;
import fr.maif.json.validation.Operation;
import fr.maif.json.validation.PermittedOperations;
@PermittedOperations(
value = {
@Operation(name = "add", path = "/test"),
@Operation(name = "remove", path = "/test"),
@Operation(name = "replace", path = "/element"),
}
)
public class EntityPatchOperationsDto extends JsonPatchOperationWrapper {
@JsonCreator
public EntityPatchOperationsDto(final List<JsonPatchOperation> ops) {
super(ops);
}
}
This will create a Class, with a List of JsonPatch Operation, with the permitted operations.
This class allow,
add
operation on the test
field,
remove
operations on the test
field
and replace
operations on the element
field.
Validation with an operation other than the above in the operation list, will result in a ConstraintViolation.
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;
@RestController
public class EntityController {
private final EntityService entityService;
private final Validator validator;
@PatchMapping(path = "/{id}", consumes = "application/json-patch+json")
public ResponseEntity<Object> patch(
@PathVariable("id") String id,
@RequestBody @Valid EntityPatchOperationsDto patchOperation) {
// Read the original entity to patch
Object original = entityService.getEntity(id);
// Patch the entity
Object patched = patch.apply(patchOperation.asJsonPatch());
// Optionally validate the patched entity
Set<ConstraintViolation<Object>> violations = validator.validate(patched);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
// Save the patched entity
entityService.save(patched);
// Return the patched entity
return ResponseEntity.ok(patched).build();
}
}