2424import org .junit .jupiter .api .Test ;
2525
2626import java .io .IOException ;
27+ import java .util .Arrays ;
2728import java .util .Set ;
2829
2930import static org .junit .jupiter .api .Assertions .assertEquals ;
31+ import static org .junit .jupiter .api .Assertions .assertTrue ;
3032
3133class OverrideValidatorTest {
3234
@@ -37,9 +39,15 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
3739 final String schema = "{\n " +
3840 " \" $schema\" :\n " +
3941 " \" 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 " +
4146 "}" ;
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+ "}" );
4351 // Use Default EmailValidator
4452 final JsonMetaSchema validatorMetaSchema = JsonMetaSchema
4553 .builder (URI , JsonMetaSchema .getV201909 ())
@@ -51,7 +59,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
5159 Set <ValidationMessage > messages = validatorSchema .validate (targetNode , OutputFormat .DEFAULT , (executionContext , validationContext ) -> {
5260 executionContext .getExecutionConfig ().setFormatAssertionsEnabled (true );
5361 });
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" )));
5566
5667 // Override EmailValidator
5768 final JsonMetaSchema overrideValidatorMetaSchema = JsonMetaSchema
@@ -62,9 +73,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
6273 final JsonSchemaFactory overrideValidatorFactory = JsonSchemaFactory .builder (JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V201909 )).metaSchema (overrideValidatorMetaSchema ).build ();
6374 final JsonSchema overrideValidatorSchema = overrideValidatorFactory .getSchema (schema );
6475
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 ());
6981 }
70- }
82+ }
0 commit comments