diff --git a/README.md b/README.md index 6137dcd..ddba26a 100644 --- a/README.md +++ b/README.md @@ -129,13 +129,38 @@ ``` Required -- value - Class that implements `Cleanser` +- value - Class that implements `Cleanser` or `ObjectAwareCleanser` -**Note:** The class supplied to `@CustomSanitizer` must implement the -`Cleanser` interface and expose a public no-argument constructor. If the -class does not implement `Cleanser`, the field is silently skipped. If the -class cannot be instantiated reflectively, an `IllegalStateException` is -thrown. +**Note:** The class supplied to `@CustomSanitizer` must implement either the +`Cleanser` or `ObjectAwareCleanser` interface and expose a public no-argument +constructor. If the class implements neither interface, the field is +silently skipped. If the class cannot be instantiated reflectively, an +`IllegalStateException` is thrown. + +### Object-aware custom sanitizers ### + +`Cleanser` only sees the annotated field's own value. For rules that depend +on a sibling field on the same object, implement `ObjectAwareCleanser` +instead: + +```java +public class DiscountCodeCleanser implements ObjectAwareCleanser { + + @Override + public String cleanse(Object owner, String value) { + Order order = (Order) owner; + return "VIP".equals(order.getTier()) ? value.toUpperCase() : value; + } + + @Override + public String description() { + return "Normalizes discount codes for VIP tier orders"; + } +} +``` + +`owner` is the object instance that owns the annotated field, so the +cleanser can read sibling fields to decide how to cleanse the value. ## Error handling ##