-
Notifications
You must be signed in to change notification settings - Fork 330
Issue386: FailFast should not cause exception on if #485
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.networknt.schema; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class Issue386Test { | ||
protected JsonSchema getJsonSchemaFromPathV7(String schemaPath, boolean failFast) { | ||
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath); | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); | ||
SchemaValidatorsConfig config = new SchemaValidatorsConfig(); | ||
config.setFailFast(failFast); | ||
return factory.getSchema(schemaInputStream, config); | ||
} | ||
|
||
protected JsonNode getJsonNodeFromPath(String dataPath) throws Exception { | ||
InputStream dataInputStream = getClass().getResourceAsStream(dataPath); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode node = mapper.readTree(dataInputStream); | ||
return node; | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = { true, false } ) | ||
public void dataIsValid(boolean failFast) throws Exception { | ||
String schemaPath = "/schema/issue386-v7.json"; | ||
String dataPath = "/data/issue386.json"; | ||
JsonSchema schema = getJsonSchemaFromPathV7(schemaPath, failFast); | ||
JsonNode node = getJsonNodeFromPath(dataPath).get("valid"); | ||
node.forEach(testNode -> { | ||
Set<ValidationMessage> errors = schema.validate(testNode.get("data")); | ||
Assertions.assertEquals(0, errors.size(), "Expected no errors for " + testNode.get("data")); | ||
}); | ||
} | ||
|
||
@Test | ||
public void dataIsInvalidFailFast() throws Exception { | ||
String schemaPath = "/schema/issue386-v7.json"; | ||
String dataPath = "/data/issue386.json"; | ||
JsonSchema schema = getJsonSchemaFromPathV7(schemaPath, true); | ||
JsonNode node = getJsonNodeFromPath(dataPath).get("invalid"); | ||
node.forEach(testNode -> { | ||
try { | ||
schema.validate(testNode.get("data")); | ||
Assertions.fail(); | ||
} catch (JsonSchemaException e) { | ||
Assertions.assertEquals(testNode.get("expectedErrors").get(0).asText(), e.getMessage()); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void dataIsInvalidFailSlow() throws Exception { | ||
String schemaPath = "/schema/issue386-v7.json"; | ||
String dataPath = "/data/issue386.json"; | ||
JsonSchema schema = getJsonSchemaFromPathV7(schemaPath, false); | ||
JsonNode node = getJsonNodeFromPath(dataPath).get("invalid"); | ||
node.forEach(testNode -> { | ||
Set<ValidationMessage> errors = schema.validate(testNode.get("data")); | ||
List<String> errorMessages = errors.stream().map(x -> x.getMessage()).collect(Collectors.toList()); | ||
testNode.get("expectedErrors").forEach(expectedError -> { | ||
Assertions.assertTrue(errorMessages.contains(expectedError.asText())); | ||
}); | ||
}); | ||
} | ||
stevehu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have all of the test cases in https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else You could maybe make two arrays like so
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed this to have all the tests above in this 'fail fast' test. |
||
"valid": [ | ||
{ | ||
"data": { | ||
"street_address": "1600 Pennsylvania Avenue NW", | ||
"country": "United States of America", | ||
"postal_code": "20500" | ||
} | ||
}, | ||
{ | ||
"data": { | ||
"street_address": "1600 Pennsylvania Avenue NW", | ||
"postal_code": "20500" | ||
} | ||
}, | ||
{ | ||
"data": { | ||
"street_address": "24 Sussex Drive", | ||
"country": "Canada", | ||
"postal_code": "K1M 1M4" | ||
} | ||
}, | ||
{ | ||
"data": { | ||
"street_address": "Adriaan Goekooplaan", | ||
"country": "Netherlands", | ||
"postal_code": "2517 JX" | ||
} | ||
} | ||
], | ||
"invalid": [ | ||
{ | ||
"data": { | ||
"street_address": "24 Sussex Drive", | ||
"country": "Canada", | ||
"postal_code": "10000" | ||
}, | ||
"expectedErrors": ["$.postal_code: does not match the regex pattern [A-Z][0-9][A-Z] [0-9][A-Z][0-9]"] | ||
}, | ||
{ | ||
"description": "invalid through first then", | ||
"data": { | ||
"street_address": "1600 Pennsylvania Avenue NW", | ||
"postal_code": "K1M 1M4" | ||
}, | ||
"expectedErrors": ["$.postal_code: does not match the regex pattern [0-9]{5}(-[0-9]{4})?"] | ||
} | ||
] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.