Skip to content

Commit 90327a4

Browse files
refactor: Rename helpers for validation module (#167)
1 parent 4436827 commit 90327a4

File tree

10 files changed

+51
-47
lines changed

10 files changed

+51
-47
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ on:
99
- 'powertools-logging/**'
1010
- 'powertools-sqs/**'
1111
- 'powertools-tracing/**'
12+
- 'powertools-validation/**'
13+
- 'powertools-parameters/**'
1214
- 'pom.xml'
1315
push:
1416
branches:

docs/content/utilities/validation.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Validate standalone function is used within the Lambda handler, or any other met
101101
You can also gracefully handle schema validation errors by catching `ValidationException`.
102102

103103
```java
104-
import static software.amazon.lambda.powertools.validation.Validator.*;
104+
import static software.amazon.lambda.powertools.validation.ValidationUtils.*;
105105

106106
public class MyFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
107107

@@ -289,7 +289,7 @@ public class XMLFunction extends BaseFunction {
289289
Once your function is created, you need to add it to powertools:
290290

291291
```java
292-
ValidatorConfig.get().addFunction(new XMLFunction());
292+
ValidationConfig.get().addFunction(new XMLFunction());
293293
```
294294

295295
You can then use it to do your validation:
@@ -317,16 +317,16 @@ public class MyXMLEventHandler implements RequestHandler<MyEventWithXML, String>
317317

318318
## Change the schema version
319319
By default, powertools-validation is configured with [V7](https://json-schema.org/draft-07/json-schema-release-notes.html).
320-
You can use the `ValidatorConfig` to change that behaviour:
320+
You can use the `ValidationConfig` to change that behaviour:
321321

322322
```java
323-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
323+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
324324
```
325325

326326
## Advanced ObjectMapper settings
327-
If you need to configure the Jackson ObjectMapper, you can use the `ValidatorConfig`:
327+
If you need to configure the Jackson ObjectMapper, you can use the `ValidationConfig`:
328328

329329
```java
330-
ObjectMapper objectMapper= ValidatorConfig.get().getObjectMapper();
330+
ObjectMapper objectMapper= ValidationConfig.get().getObjectMapper();
331331
// update (de)serializationConfig or other properties
332332
```

example/HelloWorldFunction/src/main/java/helloworld/AppValidation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
66
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
77
import software.amazon.lambda.powertools.validation.Validation;
8-
import software.amazon.lambda.powertools.validation.Validator;
98

109
import java.io.BufferedReader;
1110
import java.io.IOException;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
* Use this if you need to customize some part of the JSON Schema validation
3232
* (eg. specification version, Jackson ObjectMapper, or adding functions to JMESPath)
3333
*/
34-
public class ValidatorConfig {
35-
private ValidatorConfig() {
34+
public class ValidationConfig {
35+
private ValidationConfig() {
3636
}
3737

3838
private static class ConfigHolder {
39-
private final static ValidatorConfig instance = new ValidatorConfig();
39+
private final static ValidationConfig instance = new ValidationConfig();
4040
}
4141

42-
public static ValidatorConfig get() {
42+
public static ValidationConfig get() {
4343
return ConfigHolder.instance;
4444
}
4545

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/Validator.java renamed to powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
/**
3131
* Validation utility, used to manually validate Json against Json Schema
3232
*/
33-
public class Validator {
33+
public class ValidationUtils {
3434
private static final String CLASSPATH = "classpath:";
3535

3636
private static final ConcurrentHashMap<String, JsonSchema> schemas = new ConcurrentHashMap<>();
3737

38+
private ValidationUtils() {
39+
}
40+
3841
/**
3942
* Validate part of a json object against a json schema
4043
*
@@ -60,8 +63,8 @@ public static void validate(Object obj, JsonSchema jsonSchema, String envelope)
6063
}
6164
JsonNode subNode;
6265
try {
63-
JsonNode jsonNode = ValidatorConfig.get().getObjectMapper().valueToTree(obj);
64-
Expression<JsonNode> expression = ValidatorConfig.get().getJmesPath().compile(envelope);
66+
JsonNode jsonNode = ValidationConfig.get().getObjectMapper().valueToTree(obj);
67+
Expression<JsonNode> expression = ValidationConfig.get().getJmesPath().compile(envelope);
6568
subNode = expression.search(jsonNode);
6669
} catch (Exception e) {
6770
throw new ValidationException("Cannot find envelope <"+envelope+"> in the object <"+obj+">", e);
@@ -103,7 +106,7 @@ public static void validate(Object obj, String jsonSchema) throws ValidationExce
103106
public static void validate(Object obj, JsonSchema jsonSchema) throws ValidationException {
104107
JsonNode jsonNode;
105108
try {
106-
jsonNode = ValidatorConfig.get().getObjectMapper().valueToTree(obj);
109+
jsonNode = ValidationConfig.get().getObjectMapper().valueToTree(obj);
107110
} catch (Exception e) {
108111
throw new ValidationException("Object <"+obj+"> is not valid against the schema provided", e);
109112
}
@@ -132,7 +135,7 @@ public static void validate(String json, String jsonSchema) throws ValidationExc
132135
public static void validate(String json, JsonSchema jsonSchema) throws ValidationException {
133136
JsonNode jsonNode;
134137
try {
135-
jsonNode = ValidatorConfig.get().getObjectMapper().readTree(json);
138+
jsonNode = ValidationConfig.get().getObjectMapper().readTree(json);
136139
} catch (Exception e) {
137140
throw new ValidationException("Json <"+json+"> is not valid against the schema provided", e);
138141
}
@@ -161,7 +164,7 @@ public static void validate(Map<String, Object> map, String jsonSchema) throws V
161164
public static void validate(Map<String, Object> map, JsonSchema jsonSchema) throws ValidationException {
162165
JsonNode jsonNode;
163166
try {
164-
jsonNode = ValidatorConfig.get().getObjectMapper().valueToTree(map);
167+
jsonNode = ValidationConfig.get().getObjectMapper().valueToTree(map);
165168
} catch (Exception e) {
166169
throw new ValidationException("Map <"+map+"> cannot be converted to json for validation", e);
167170
}
@@ -194,7 +197,7 @@ public static void validate(JsonNode jsonNode, JsonSchema jsonSchema) throws Val
194197
if (!validationMessages.isEmpty()) {
195198
String message;
196199
try {
197-
message = ValidatorConfig.get().getObjectMapper().writeValueAsString(new ValidationErrors(validationMessages));
200+
message = ValidationConfig.get().getObjectMapper().writeValueAsString(new ValidationErrors(validationMessages));
198201
} catch (JsonProcessingException e) {
199202
message = validationMessages.stream().map(ValidationMessage::getMessage).collect(Collectors.joining(", "));
200203
}
@@ -233,13 +236,13 @@ public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
233236
if (schemaStream == null) {
234237
throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
235238
}
236-
jsonSchema = ValidatorConfig.get().getFactory().getSchema(schemaStream);
239+
jsonSchema = ValidationConfig.get().getFactory().getSchema(schemaStream);
237240
} else {
238-
jsonSchema = ValidatorConfig.get().getFactory().getSchema(schema);
241+
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
239242
}
240243

241244
if (validateSchema) {
242-
String version = ValidatorConfig.get().getSchemaVersion().toString();
245+
String version = ValidationConfig.get().getSchemaVersion().toString();
243246
try {
244247
validate(jsonSchema.getSchemaNode(),
245248
getJsonSchema("classpath:/schemas/meta_schema_" + version));

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/internal/ValidationAspect.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import org.aspectj.lang.annotation.Aspect;
2121
import org.aspectj.lang.annotation.Pointcut;
2222
import software.amazon.lambda.powertools.validation.Validation;
23-
import software.amazon.lambda.powertools.validation.ValidatorConfig;
23+
import software.amazon.lambda.powertools.validation.ValidationConfig;
2424

2525
import static com.networknt.schema.SpecVersion.VersionFlag.V201909;
2626
import static java.nio.charset.StandardCharsets.UTF_8;
2727
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.isHandlerMethod;
2828
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.placedOnRequestHandler;
29-
import static software.amazon.lambda.powertools.validation.Validator.getJsonSchema;
30-
import static software.amazon.lambda.powertools.validation.Validator.validate;
29+
import static software.amazon.lambda.powertools.validation.ValidationUtils.getJsonSchema;
30+
import static software.amazon.lambda.powertools.validation.ValidationUtils.validate;
3131
import static software.amazon.lambda.powertools.validation.jmespath.Base64Function.decode;
3232
import static software.amazon.lambda.powertools.validation.jmespath.Base64GZipFunction.decompress;
3333

@@ -48,7 +48,7 @@ public Object around(ProceedingJoinPoint pjp,
4848
boolean validationNeeded = false;
4949

5050
if (validation.schemaVersion() != V201909) {
51-
ValidatorConfig.get().setSchemaVersion(validation.schemaVersion());
51+
ValidationConfig.get().setSchemaVersion(validation.schemaVersion());
5252
}
5353

5454
if (isHandlerMethod(pjp)

powertools-validation/src/test/java/software/amazon/lambda/powertools/validation/Base64FunctionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class Base64FunctionTest {
2626

2727
@Test
2828
public void testPowertoolsBase64() throws IOException {
29-
JsonNode event = ValidatorConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/custom_event.json"));
30-
Expression<JsonNode> expression = ValidatorConfig.get().getJmesPath().compile("basket.powertools_base64(hiddenProduct)");
29+
JsonNode event = ValidationConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/custom_event.json"));
30+
Expression<JsonNode> expression = ValidationConfig.get().getJmesPath().compile("basket.powertools_base64(hiddenProduct)");
3131
JsonNode result = expression.search(event);
3232
assertThat(result.getNodeType()).isEqualTo(JsonNodeType.STRING);
3333
assertThat(result.asText()).isEqualTo("{\n" +

powertools-validation/src/test/java/software/amazon/lambda/powertools/validation/Base64GZipFunctionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class Base64GZipFunctionTest {
2626

2727
@Test
2828
public void testPowertoolsGzip() throws IOException {
29-
JsonNode event = ValidatorConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/custom_event_gzip.json"));
30-
Expression<JsonNode> expression = ValidatorConfig.get().getJmesPath().compile("basket.powertools_base64_gzip(hiddenProduct)");
29+
JsonNode event = ValidationConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/custom_event_gzip.json"));
30+
Expression<JsonNode> expression = ValidationConfig.get().getJmesPath().compile("basket.powertools_base64_gzip(hiddenProduct)");
3131
JsonNode result = expression.search(event);
3232
assertThat(result.getNodeType()).isEqualTo(JsonNodeType.STRING);
3333
assertThat(result.asText()).isEqualTo("{ \"id\": 43242, \"name\": \"FooBar XY\", \"price\": 258}");

powertools-validation/src/test/java/software/amazon/lambda/powertools/validation/ValidatorTest.java renamed to powertools-validation/src/test/java/software/amazon/lambda/powertools/validation/ValidationUtilsTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,71 @@
1414
import java.util.Map;
1515

1616
import static org.assertj.core.api.Assertions.*;
17-
import static software.amazon.lambda.powertools.validation.Validator.getJsonSchema;
18-
import static software.amazon.lambda.powertools.validation.Validator.validate;
17+
import static software.amazon.lambda.powertools.validation.ValidationUtils.getJsonSchema;
18+
import static software.amazon.lambda.powertools.validation.ValidationUtils.validate;
1919

20-
public class ValidatorTest {
20+
public class ValidationUtilsTest {
2121

2222
private JsonSchema schema = getJsonSchema("classpath:/schema_v7.json");
2323

2424
@BeforeEach
2525
public void setup() {
26-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
26+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
2727
}
2828

2929
@Test
3030
public void testLoadSchemaV7OK() {
31-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
31+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
3232
JsonSchema jsonSchema = getJsonSchema("classpath:/schema_v7.json", true);
3333
assertThat(jsonSchema).isNotNull();
3434
assertThat(jsonSchema.getCurrentUri()).asString().isEqualTo("http://example.com/product.json");
3535
}
3636

3737
@Test
3838
public void testLoadSchemaV7KO() {
39-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
39+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
4040
assertThatThrownBy(() -> getJsonSchema("classpath:/schema_v7_ko.json", true))
4141
.isInstanceOf(IllegalArgumentException.class)
4242
.hasMessage("The schema classpath:/schema_v7_ko.json is not valid, it does not respect the specification V7");
4343
}
4444

4545
@Test
4646
public void testLoadMetaSchema_NoValidation() {
47-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V201909);
47+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V201909);
4848
getJsonSchema("classpath:/schemas/meta_schema_V201909", false);
4949
}
5050

5151
@Test
5252
public void testLoadMetaSchemaV2019() {
53-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V201909);
53+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V201909);
5454
JsonSchema jsonSchema = getJsonSchema("classpath:/schemas/meta_schema_V201909", true);
5555
assertThat(jsonSchema).isNotNull();
5656
}
5757

5858
@Test
5959
public void testLoadMetaSchemaV7() {
60-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
60+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
6161
JsonSchema jsonSchema = getJsonSchema("classpath:/schemas/meta_schema_V7", true);
6262
assertThat(jsonSchema).isNotNull();
6363
}
6464

6565
@Test
6666
public void testLoadMetaSchemaV6() {
67-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V6);
67+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V6);
6868
JsonSchema jsonSchema = getJsonSchema("classpath:/schemas/meta_schema_V6", true);
6969
assertThat(jsonSchema).isNotNull();
7070
}
7171

7272
@Test
7373
public void testLoadMetaSchemaV4() {
74-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
74+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
7575
JsonSchema jsonSchema = getJsonSchema("classpath:/schemas/meta_schema_V4", true);
7676
assertThat(jsonSchema).isNotNull();
7777
}
7878

7979
@Test
8080
public void testLoadSchemaV4OK() {
81-
ValidatorConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
81+
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V4);
8282
JsonSchema jsonSchema = getJsonSchema("classpath:/schema_v4.json", true);
8383
assertThat(jsonSchema).isNotNull();
8484
}
@@ -92,14 +92,14 @@ public void testLoadSchemaNotFound() {
9292

9393
@Test
9494
public void testValidateJsonNodeOK() throws IOException {
95-
JsonNode node = ValidatorConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/json_ok.json"));
95+
JsonNode node = ValidationConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/json_ok.json"));
9696

9797
validate(node, schema);
9898
}
9999

100100
@Test
101101
public void testValidateJsonNodeKO() throws IOException {
102-
JsonNode node = ValidatorConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/json_ko.json"));
102+
JsonNode node = ValidationConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/json_ko.json"));
103103

104104
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> validate(node, schema));
105105
}

powertools-validation/src/test/java/software/amazon/lambda/powertools/validation/internal/ValidationAspectTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.mockito.Mock;
2424
import org.mockito.MockitoAnnotations;
2525
import software.amazon.lambda.powertools.validation.ValidationException;
26-
import software.amazon.lambda.powertools.validation.ValidatorConfig;
26+
import software.amazon.lambda.powertools.validation.ValidationConfig;
2727
import software.amazon.lambda.powertools.validation.handlers.*;
2828
import software.amazon.lambda.powertools.validation.model.MyCustomEvent;
2929

@@ -92,23 +92,23 @@ public void validate_inputKO_schemaInString_shouldThrowValidationException() {
9292

9393
@Test
9494
public void validate_SQS() throws IOException {
95-
SQSEvent event = ValidatorConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/sqs.json"), SQSEvent.class);
95+
SQSEvent event = ValidationConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/sqs.json"), SQSEvent.class);
9696

9797
SQSHandler handler = new SQSHandler();
9898
assertThat(handler.handleRequest(event, context)).isEqualTo("OK");
9999
}
100100

101101
@Test
102102
public void validate_Kinesis() throws IOException {
103-
KinesisEvent event = ValidatorConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/kinesis.json"), KinesisEvent.class);
103+
KinesisEvent event = ValidationConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/kinesis.json"), KinesisEvent.class);
104104

105105
KinesisHandler handler = new KinesisHandler();
106106
assertThat(handler.handleRequest(event, context)).isEqualTo("OK");
107107
}
108108

109109
@Test
110110
public void validate_CustomObject() throws IOException {
111-
MyCustomEvent event = ValidatorConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/custom_event.json"), MyCustomEvent.class);
111+
MyCustomEvent event = ValidationConfig.get().getObjectMapper().readValue(this.getClass().getResourceAsStream("/custom_event.json"), MyCustomEvent.class);
112112

113113
MyCustomEventHandler handler = new MyCustomEventHandler();
114114
assertThat(handler.handleRequest(event, context)).isEqualTo("OK");

0 commit comments

Comments
 (0)