Skip to content

Fix OverrideValidatorTest #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/test/java/com/networknt/schema/OverrideValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class OverrideValidatorTest {

Expand All @@ -37,9 +39,15 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
final String schema = "{\n" +
" \"$schema\":\n" +
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\",\n" +
" \"properties\": {\"mailaddress\": {\"type\": \"string\", \"format\": \"email\"}}\n" +
" \"properties\": {\n" +
" \"mailaddress\": {\"type\": \"string\", \"format\": \"email\"},\n" +
" \"timestamp\": {\"type\": \"string\", \"format\": \"date-time\"}\n" +
" }\n" +
"}";
final JsonNode targetNode = objectMapper.readTree("{\"mailaddress\": \"a-zA-Z0-9.!#$%&'*[email protected]\"}");
final JsonNode targetNode = objectMapper.readTree("{\n" +
" \"mailaddress\": \"a-zA-Z0-9.!#$%&'*[email protected]\",\n" +
" \"timestamp\": \"bad\"\n" +
"}");
// Use Default EmailValidator
final JsonMetaSchema validatorMetaSchema = JsonMetaSchema
.builder(URI, JsonMetaSchema.getV201909())
Expand All @@ -51,7 +59,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
Set<ValidationMessage> messages = validatorSchema.validate(targetNode, OutputFormat.DEFAULT, (executionContext, validationContext) -> {
executionContext.getExecutionConfig().setFormatAssertionsEnabled(true);
});
assertEquals(1, messages.size());

assertEquals(2, messages.size(), Arrays.toString(messages.toArray()));
assertTrue(messages.stream().anyMatch(it -> it.getInstanceLocation().getName(-1).equals("mailaddress")));
assertTrue(messages.stream().anyMatch(it -> it.getInstanceLocation().getName(-1).equals("timestamp")));

// Override EmailValidator
final JsonMetaSchema overrideValidatorMetaSchema = JsonMetaSchema
Expand All @@ -62,9 +73,10 @@ void overrideDefaultValidator() throws JsonProcessingException, IOException {
final JsonSchemaFactory overrideValidatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).metaSchema(overrideValidatorMetaSchema).build();
final JsonSchema overrideValidatorSchema = overrideValidatorFactory.getSchema(schema);

messages = overrideValidatorSchema.validate(targetNode);
assertEquals(0, messages.size());


messages = overrideValidatorSchema.validate(targetNode, executionContext -> {
executionContext.getExecutionConfig().setFormatAssertionsEnabled(true);
});
assertTrue(messages.stream().anyMatch(it -> it.getInstanceLocation().getName(-1).equals("timestamp")));
assertEquals(1, messages.size());
}
}
}