24
24
import org .junit .jupiter .api .Test ;
25
25
26
26
import java .io .IOException ;
27
+ import java .util .Arrays ;
27
28
import java .util .Set ;
28
29
29
30
import static org .junit .jupiter .api .Assertions .assertEquals ;
31
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
30
32
31
33
class OverrideValidatorTest {
32
34
@@ -37,9 +39,15 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
37
39
final String schema = "{\n " +
38
40
" \" $schema\" :\n " +
39
41
" \" https://github.com/networknt/json-schema-validator/tests/schemas/example01\" ,\n " +
40
- " \" properties\" : {\" mailaddress\" : {\" type\" : \" string\" , \" format\" : \" email\" }}\n " +
42
+ " \" properties\" : {\n " +
43
+ " \" mailaddress\" : {\" type\" : \" string\" , \" format\" : \" email\" },\n " +
44
+ " \" timestamp\" : {\" type\" : \" string\" , \" format\" : \" date-time\" }\n " +
45
+ " }\n " +
41
46
"}" ;
42
- final JsonNode targetNode =
objectMapper .
readTree (
"{\" mailaddress\" : \" a-zA-Z0-9.!#$%&'*[email protected] \" }" );
47
+ final JsonNode targetNode = objectMapper .readTree ("{\n " +
48
+ " \" mailaddress\" : \" a-zA-Z0-9.!#$%&'*[email protected] \" ,\n " +
49
+ " \" timestamp\" : \" bad\" \n " +
50
+ "}" );
43
51
// Use Default EmailValidator
44
52
final JsonMetaSchema validatorMetaSchema = JsonMetaSchema
45
53
.builder (URI , JsonMetaSchema .getV201909 ())
@@ -51,7 +59,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
51
59
Set <ValidationMessage > messages = validatorSchema .validate (targetNode , OutputFormat .DEFAULT , (executionContext , validationContext ) -> {
52
60
executionContext .getExecutionConfig ().setFormatAssertionsEnabled (true );
53
61
});
54
- assertEquals (1 , messages .size ());
62
+
63
+ assertEquals (2 , messages .size (), Arrays .toString (messages .toArray ()));
64
+ assertTrue (messages .stream ().anyMatch (it -> it .getInstanceLocation ().getName (-1 ).equals ("mailaddress" )));
65
+ assertTrue (messages .stream ().anyMatch (it -> it .getInstanceLocation ().getName (-1 ).equals ("timestamp" )));
55
66
56
67
// Override EmailValidator
57
68
final JsonMetaSchema overrideValidatorMetaSchema = JsonMetaSchema
@@ -62,9 +73,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
62
73
final JsonSchemaFactory overrideValidatorFactory = JsonSchemaFactory .builder (JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V201909 )).metaSchema (overrideValidatorMetaSchema ).build ();
63
74
final JsonSchema overrideValidatorSchema = overrideValidatorFactory .getSchema (schema );
64
75
65
- messages = overrideValidatorSchema .validate (targetNode );
66
- assertEquals (0 , messages .size ());
67
-
68
-
76
+ messages = overrideValidatorSchema .validate (targetNode , executionContext -> {
77
+ executionContext .getExecutionConfig ().setFormatAssertionsEnabled (true );
78
+ });
79
+ assertTrue (messages .stream ().anyMatch (it -> it .getInstanceLocation ().getName (-1 ).equals ("timestamp" )));
80
+ assertEquals (1 , messages .size ());
69
81
}
70
- }
82
+ }
0 commit comments