19
19
import org .aspectj .lang .annotation .Around ;
20
20
import org .aspectj .lang .annotation .Aspect ;
21
21
import org .aspectj .lang .annotation .Pointcut ;
22
+ import org .slf4j .Logger ;
23
+ import org .slf4j .LoggerFactory ;
22
24
import software .amazon .lambda .powertools .validation .Validation ;
23
25
import software .amazon .lambda .powertools .validation .ValidationConfig ;
24
26
36
38
*/
37
39
@ Aspect
38
40
public class ValidationAspect {
41
+ private static final Logger LOG = LoggerFactory .getLogger (ValidationAspect .class );
42
+
43
+
39
44
@ SuppressWarnings ({"EmptyMethod" })
40
45
@ Pointcut ("@annotation(validation)" )
41
46
public void callAt (Validation validation ) {
@@ -59,7 +64,9 @@ && placedOnRequestHandler(pjp)) {
59
64
JsonSchema inboundJsonSchema = getJsonSchema (validation .inboundSchema (), true );
60
65
61
66
Object obj = pjp .getArgs ()[0 ];
62
- if (obj instanceof APIGatewayProxyRequestEvent ) {
67
+ if (validation .envelope () != null && !validation .envelope ().isEmpty ()) {
68
+ validate (obj , inboundJsonSchema , validation .envelope ());
69
+ } else if (obj instanceof APIGatewayProxyRequestEvent ) {
63
70
APIGatewayProxyRequestEvent event = (APIGatewayProxyRequestEvent ) obj ;
64
71
validate (event .getBody (), inboundJsonSchema );
65
72
} else if (obj instanceof APIGatewayV2HTTPEvent ) {
@@ -105,7 +112,7 @@ && placedOnRequestHandler(pjp)) {
105
112
KinesisAnalyticsStreamsInputPreprocessingEvent event = (KinesisAnalyticsStreamsInputPreprocessingEvent ) obj ;
106
113
event .getRecords ().forEach (record -> validate (decode (record .getData ()), inboundJsonSchema ));
107
114
} else {
108
- validate ( obj , inboundJsonSchema , validation . envelope ());
115
+ LOG . warn ( "Unhandled event type {}, please use the 'envelope' parameter to specify what to validate" , obj . getClass (). getName ());
109
116
}
110
117
}
111
118
}
@@ -115,7 +122,9 @@ && placedOnRequestHandler(pjp)) {
115
122
if (validationNeeded && !validation .outboundSchema ().isEmpty ()) {
116
123
JsonSchema outboundJsonSchema = getJsonSchema (validation .outboundSchema (), true );
117
124
118
- if (result instanceof APIGatewayProxyResponseEvent ) {
125
+ if (validation .envelope () != null && !validation .envelope ().isEmpty ()) {
126
+ validate (result , outboundJsonSchema , validation .envelope ());
127
+ } else if (result instanceof APIGatewayProxyResponseEvent ) {
119
128
APIGatewayProxyResponseEvent response = (APIGatewayProxyResponseEvent ) result ;
120
129
validate (response .getBody (), outboundJsonSchema );
121
130
} else if (result instanceof APIGatewayV2HTTPResponse ) {
@@ -131,7 +140,7 @@ && placedOnRequestHandler(pjp)) {
131
140
KinesisAnalyticsInputPreprocessingResponse response = (KinesisAnalyticsInputPreprocessingResponse ) result ;
132
141
response .getRecords ().forEach (record -> validate (decode (record .getData ()), outboundJsonSchema ));
133
142
} else {
134
- validate ( result , outboundJsonSchema , validation . envelope ());
143
+ LOG . warn ( "Unhandled response type {}, please use the 'envelope' parameter to specify what to validate" , result . getClass (). getName ());
135
144
}
136
145
}
137
146
0 commit comments