|
| 1 | +@startuml |
| 2 | +title error-utils: validation framework workflow |
| 3 | + |
| 4 | +autonumber |
| 5 | +hide footbox |
| 6 | + |
| 7 | +actor Caller |
| 8 | +participant "Validator" as Entry |
| 9 | +participant "ObjectValidator<T>" as ObjValidator |
| 10 | +participant "ValidationCollector" as Collector |
| 11 | +participant "ValidationChain" as Chain |
| 12 | +participant "ValidationStep<T>\nor inline lambda" as Step |
| 13 | +participant "ValidationFailure" as Failure |
| 14 | +participant "ValidationResult" as Result |
| 15 | +participant "BusinessException\n(default)" as BusinessEx |
| 16 | + |
| 17 | +note over Entry, Chain |
| 18 | +Validation module is Spring-free. |
| 19 | +Default start mode = COLLECT_ALL. |
| 20 | +end note |
| 21 | + |
| 22 | +Caller -> Entry: forObject(target) |
| 23 | +Entry -> Chain: start() |
| 24 | +Chain --> Entry: new chain(mode=COLLECT_ALL, failures=[]) |
| 25 | +Entry -> ObjValidator: new ObjectValidator(target, chain) |
| 26 | +ObjValidator -> Collector: new ValidationCollector(chain) |
| 27 | +Entry --> Caller: ObjectValidator<T> |
| 28 | + |
| 29 | +alt switch to fail-fast |
| 30 | + Caller -> ObjValidator: failFast() |
| 31 | + ObjValidator -> Chain: failFast() |
| 32 | + Chain --> ObjValidator: new chain view(mode=FAIL_FAST,\nshared failures list) |
| 33 | + ObjValidator --> Caller: new ObjectValidator(target, chainView) |
| 34 | +else keep/default collect-all |
| 35 | + Caller -> ObjValidator: collectAll() or keep default |
| 36 | + ObjValidator -> Chain: collectAll() |
| 37 | + Chain --> ObjValidator: new chain view(mode=COLLECT_ALL,\nshared failures list) |
| 38 | + ObjValidator --> Caller: new ObjectValidator(target, chainView) |
| 39 | +end |
| 40 | + |
| 41 | +loop each validation step |
| 42 | + alt typed reusable step |
| 43 | + Caller -> ObjValidator: step(ValidationStep) |
| 44 | + ObjValidator -> Step: validate(target, collector) |
| 45 | + else collector-only step |
| 46 | + Caller -> ObjValidator: step(collector -> ...) |
| 47 | + ObjValidator -> Step: accept(collector) |
| 48 | + else direct check/require |
| 49 | + Caller -> ObjValidator: check(...) / require(...) |
| 50 | + ObjValidator -> Collector: check(...) / require(...) |
| 51 | + end |
| 52 | + |
| 53 | + alt step uses collector |
| 54 | + Step -> Collector: check(...) / require(...) |
| 55 | + end |
| 56 | + |
| 57 | + Collector -> Chain: check(valid, failure)\nor check(predicate, failure) |
| 58 | + Chain -> Chain: shouldSkipEvaluation()? |
| 59 | + |
| 60 | + alt FAIL_FAST and a previous failure exists |
| 61 | + Chain --> Collector: skip evaluation |
| 62 | + else current validation passed |
| 63 | + Chain --> Collector: no failure added |
| 64 | + else current validation failed |
| 65 | + Collector -> Failure: ValidationFailure.of(errorCode, message)\n.field(...)\n.rejectedValue(...)\n.violationCode(...)\n.metadata(...)\n.exceptionFactory(...) |
| 66 | + Collector -> Chain: add failure |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +alt caller asks for result |
| 71 | + Caller -> ObjValidator: toResult() |
| 72 | + ObjValidator -> Collector: toResult() |
| 73 | + Collector -> Chain: toResult() |
| 74 | + Chain -> Chain: snapshot failures |
| 75 | + Chain -> Failure: toFieldViolation() for each failure |
| 76 | + Chain -> Result: new ValidationResult(mode,\nfailures, violations) |
| 77 | + Result --> Caller: immutable result |
| 78 | + |
| 79 | + note over Result |
| 80 | + Result exposes: |
| 81 | + - isValid() |
| 82 | + - failures() |
| 83 | + - violations() |
| 84 | + - metadata() merged from all failures |
| 85 | + - firstFailure() |
| 86 | + end note |
| 87 | +else caller wants exception |
| 88 | + Caller -> ObjValidator: throwIfInvalid()\nor throwIfInvalid(customFactory) |
| 89 | + ObjValidator -> Collector: throwIfInvalid(...) |
| 90 | + Collector -> Chain: throwIfInvalid(...) |
| 91 | + Chain -> Chain: result = toResult() |
| 92 | + |
| 93 | + alt result is valid |
| 94 | + Chain --> Caller: return normally |
| 95 | + else first failure has exceptionFactory |
| 96 | + Chain -> Failure: firstFailure.exceptionFactory().apply(firstFailure) |
| 97 | + Failure --> Chain: RuntimeException |
| 98 | + Chain --> Caller: throw custom exception |
| 99 | + else custom terminal factory provided |
| 100 | + Chain -> Result: customFactory.apply(result) |
| 101 | + Result --> Chain: RuntimeException |
| 102 | + Chain --> Caller: throw custom exception |
| 103 | + else default behavior |
| 104 | + Chain -> Result: firstFailure(), metadata(), violations() |
| 105 | + Chain -> BusinessEx: new BusinessException(\nfirstFailure.errorCode(),\nfirstFailure.message(),\nnull,\nresult.metadata(),\nresult.violations()) |
| 106 | + BusinessEx --> Caller: throw BusinessException |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +note over Caller |
| 111 | +FAIL_FAST stops recording after first failure. |
| 112 | +COLLECT_ALL keeps all failures in one result/exception. |
| 113 | +end note |
| 114 | + |
| 115 | +@enduml |
0 commit comments