forked from networknt/json-schema-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- implement property names in terms of full schema validation - add test case for complex property name - added test case for networknt#342 - fix networknt#375 tests to expect standard messages - closes networknt#396 - closes networknt#342
- Loading branch information
1 parent
dfd63aa
commit d61d006
Showing
8 changed files
with
171 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.networknt.schema; | ||
|
||
import java.io.InputStream; | ||
import java.util.Set; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class Issue342Test { | ||
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); | ||
return factory.getSchema(schemaContent); | ||
} | ||
|
||
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode node = mapper.readTree(content); | ||
return node; | ||
} | ||
|
||
@Test | ||
public void propertyNameEnumShouldFailV7() throws Exception { | ||
String schemaPath = "/schema/issue342-v7.json"; | ||
String dataPath = "/data/issue342.json"; | ||
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath); | ||
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream); | ||
InputStream dataInputStream = getClass().getResourceAsStream(dataPath); | ||
JsonNode node = getJsonNodeFromStreamContent(dataInputStream); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assert.assertEquals(1, errors.size()); | ||
final ValidationMessage error = errors.iterator().next(); | ||
Assert.assertEquals("$.z", error.getPath()); | ||
Assert.assertEquals("Property name $.z is not valid for validation: does not have a value in the enumeration [a, b, c]", error.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.networknt.schema; | ||
|
||
import java.io.InputStream; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class Issue396Test { | ||
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); | ||
return factory.getSchema(schemaContent); | ||
} | ||
|
||
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode node = mapper.readTree(content); | ||
return node; | ||
} | ||
|
||
@Test | ||
public void testComplexPropertyNamesV7() throws Exception { | ||
String schemaPath = "/schema/issue396-v7.json"; | ||
String dataPath = "/data/issue396.json"; | ||
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath); | ||
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream); | ||
InputStream dataInputStream = getClass().getResourceAsStream(dataPath); | ||
JsonNode node = getJsonNodeFromStreamContent(dataInputStream); | ||
|
||
final Set<String> invalidPaths = new HashSet<>(); | ||
node.fields().forEachRemaining(entry -> { | ||
if (!entry.getValue().asBoolean()) | ||
invalidPaths.add("$." + entry.getKey()); | ||
}); | ||
|
||
Set<ValidationMessage> errors = schema.validate(node); | ||
final Set<String> failedPaths = errors.stream().map(ValidationMessage::getPath).collect(Collectors.toSet()); | ||
Assert.assertEquals(failedPaths, invalidPaths); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"a": "ok", | ||
"z": "nope" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"veryveryverylongallowedname": true, | ||
"a": true, | ||
"x": true, | ||
"z": false, | ||
"abc": false, | ||
"w": false, | ||
"ww": false, | ||
"WW": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"propertyNames": { | ||
"enum": [ | ||
"a", | ||
"b", | ||
"c" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"type": "object", | ||
"propertyNames": { | ||
"anyOf": [ | ||
{ | ||
"minLength": 10 | ||
}, | ||
{ | ||
"$ref": "#/definitions/props" | ||
}, | ||
{ | ||
"oneOf": [ | ||
{ | ||
"enum": [ | ||
"z", | ||
"a", | ||
"b" | ||
] | ||
}, | ||
{ | ||
"$ref": "#/definitions/xyz" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"definitions": { | ||
"props": { | ||
"minLength": 2, | ||
"pattern": "[A-Z]" | ||
}, | ||
"xyz": { | ||
"enum": [ | ||
"x", | ||
"y", | ||
"z" | ||
] | ||
} | ||
} | ||
} |